codeigniter: Validation form without redirect - codeigniter

Codeigniter form validation is nice but it is very troubled because it redirect to another url to validate the form. Because sometimes we have the segment in the URL. Being redirect will lose the segment. And in other case user type directly to the validation URL. The example in my case mywebsite.com/class/login_validation.
So how to validation without a redirect or prevent user to type directly to the validation URL.
My view
<?php
echo validation_errors();
echo form_open('login_validation');
echo form_label('Username');
echo form_input('username',$this->input->post('username'));
echo form_label('Passowrd');
echo form_password('password',$this->input->post('password'));
echo form_submit('submit','Login');
echo form_close();
?>
My controller
function login() {
$this->load->helper('form');
$this->load->view('page/login');
}
function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'User Name', 'required|xss_clean|callback_check_user');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if ($this->form_validation->run()) {
$newdata = array('username' => $this->input->post('usertname'),
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
redirect('dashboard');
} else {
$this->load->view('page/login');
}
}

$newdata = array('username' => $this->input->post('usertname')
please re-check this line username instead of usertname

The easiest way to prevent direct entry of the URL is to test the return of $this->input->method(); == 'post' If true then it isn't a typed in URL.
If you need data to persist across a redirect, use session user data.

Related

after login display client first name in the top of my header page in codeigniter

i have registration page after user register goes to login page,i want to display a user first name in the top pf my header page when we click username i want a dropdown logout after click on logout user shuold be logout..
here is my controller code:
public function login()
{
$data['error'] ="Invalid Login";
$this->load->view('auth/header');
if($this->input->post())
{
$user = $this->UserModel->login($this->input->post());
if(count($user)>0)
{
$array = array(
'client_id' => $user['client_id'],
'first_name' => $user['first_name'],
'client_type_id'=>$user['client_type_id'],
'email' => $user['email'],
'password' => $user['password'],
);
$this->session->set_userdata('userdata',$array);
if($user['client_id'] == $user['client_id'])
{
redirect(base_url('dashboard/dashboard'));
}
}
else
{
$data["error_message"]="Invalid User Name and Password combination";
}
logout
function logout()
{
$this->session->unset_userdata(userdata);
$this->session->sess_destroy();
redirect('Auth/login','refresh');
}
view:
<a href="javascript:;" data-toggle="dropdown" class="right_color">
<?php
echo $this->session->userdata('userdata');
?>
<!-- <img src="<?php echo base_url();?>images/admin.jpg" alt="admin-pic">-->
</a>
Logout
Your current session is in a nested array format
array(userdata=>array(your session))
in your controller replace your session set code
to this
$this->session->set_userdata($array);
and in your view
echo $this->session->userdata('first_name');
You are setting array of data in session. You are trying to print array of data. It will throw array to string conversion error. Change this following and try :-
For creating session:-
$this->session->set_userdata($array);
Now you print:-
<?php
echo $this->session->userdata('client_id');
echo $this->session->userdata('first_name');
echo $this->session->userdata('email');
?>
For more about Session refer this link https://www.codeigniter.com/user_guide/libraries/sessions.html

Codeigniter HMVC multiple urls loading same form what is best practice to handle this scenario

I'm currently using HMVC with code igniter (i'm new to this), and I am trying to get a better understanding as how to best structure the urls for seo and best practice.
In an example I have a module Users (with mvc folders), one of the views is a login form:
<?php
echo form_open('user/submit');
echo validation_errors();
echo "<p>Email: ";
echo form_input('email');
echo "</p>";
echo "<p>Password: ";
echo form_password('password');
echo "</p>";
echo "<p>";
echo form_submit('login_submit', 'login');
echo "</p>";
echo form_close();
?>
and the controller contains methods:
function login() {
$data['view_file'] = "login_form";
$this->load->module('template');
$this->template->generic($data);
}
function submit(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if ($this->form_validation->run($this) == FALSE){
$this->login();
} else {
echo "Success";
}
}
Now to access the form you go to the url:
http://somesite/users/login but when you click submit and the login has failed the user is redirected back to url: http://somesite/users/submit, how would this affect seo as the login form is accessible at both urls and what is best practice for this scenario?
Thanks
I notice sth wrong with your description. when "login has failed the user is redirected back to url: http://somesite/users/submit", according to your script, user is redirected to url: http://somesite/users/login".
BTW, for SEO friendly, URL like http://site/controller/action are recommended.

codeigniter: using uri segment in model

I am using uri segment to delete info in my database:
anchor('site/delete_note/'.$row->id, 'Delete')
Model:
function delete_note()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
}
It works fine, but I want to do the same for updating my info and can't get it work
So this is link in view:
anchor('site/edit_note/'.$row->id, 'Edit')
My controller:
function edit_note()
{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}
My model:
function edit_note($note_id)
{
$content = $this->input->post('content');
$data = array('content' => $content);
$this->db->where('id', $note_id);
$this->db->update('note', $data);
}
My view of edit_note:
<?php
echo form_open('site/edit_note');
echo form_textarea('content', set_value('content', 'Your message'));
echo form_submit('submit', 'Change');
echo anchor('site/members_area', 'Cancel');
echo validation_errors('<p class="error">'); ?>
Edit doesn't work as delete, when i am trying to get segment directly in edit model, as I used in delete model.
If I set $note_id to a number in my controller, instead of this '$this->uri->segment(3)', it updates my database. But if I use getting segment it doesn't work. I thought uri segments are available in controller as in model, but there is something I don't know.
Better yet, instead of manually reading the IDs via the segments, you could change your functions to be:
function delete_note($note_id)
and
function edit_note($note_id)
And remove the $note_id = $this->uri->segment(3); lines.
And as silly as it'll sound, the generated URL is definitely correct, right?
And last question, have you done anything with routes?
Edit
I've also noticed that in edit, you use this in your form:
echo form_open('site/edit_note');
So when the form submits, the URL it submits to is site/edit_note instead of site/edit_note/{SOME_ID}. So once you make your changes, and the form submits, there won't be a 3rd URL segment!
Well there are some logical errors in your code.
function edit_note()
{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
//// What is the use of loadig a view when you are editing
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}
Instead do it like this
function edit_note()
{
if($this->input->post()){
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}else{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
}
}
MOST IMPORTANT
And the other thing you should note that you are using anchor to access edit note but not actually submitting a form so it is not getting any post data to update.
In my view it's a 'bad' approach to use uri segments in your models... you should pass an id as a parameter from your controller functions ..
function delete_note()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
}
What if you want to re-use this delete method? e.g. deleting notes from an admin panel, via a cron job etc then the above relies upon the uri segment and you will need to create additional delete methods to do the job. Also, if you were to continue with the same you don't even need a model then .. just call these lines in your controllers if you know what I mean ...
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
so best is to change it to similar to your edit_note() model function.

Stumped by CodeIgniter controllers found but not found

OK, I admit it. I'm a CI newbie. I'm doing something really basic wrong and I've been at it for a couple of hours and can't find my error.
I have a controller called login. It's in the resources folder in my controllers folder. It loads a view called login_form. But for the life of me it will not load if I go to domain.com/resources/login or domain.com/resources/index. But I can get to it via a route:
$route['engineering-resources/login'] = "resources/login";
Even when I get to it this way, my form action isn't found. Here is my form:
<?php
echo form_open('resources/login/validate_credentials'); //folder/controller/method. I think this is where my problem is
echo form_input('username', 'Username');
echo form_password('password', 'Password');
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Create Account');
echo form_close();
?>
The path is my resources folder in my controllers folder, and the controller is the login controller using the validate_credentials method. Here is the pertinent part of my login controller:
class Login extends Controller {
function index()
{
$data['title'] = 'Engineering Resources | Login';
$data['main_content'] = 'resources/login_form';
$this->load->view('templates/main.php', $data);
}
function validate_credentials()
{
$this->load->model('login/membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('resources/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
The index function works when I use the route, but not when I use the above domain.com paths. I assume that is why it cannot find the validate_credentials method. What am I doing wrong?
Here is main.php
<?php $this->load->view('includes/header.php'); ?>
<?php $this->load->view('includes/nav.php'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('includes/footer.php'); ?>
$route['engineering-resources/login'] = "resources/login";
This does not route to engineering-resources/login/validate_credentials to resources/login/validate_credentials
You should have something like this:
// not tested
$route['engineering-resources/login/(\S*)'] = "resources/login/$1";
One more thing is that if you are using routes, you should use the routes from your views too..
echo form_open('engineering-resources/login/validate_credentials');

Codeigniter Form validation problem

Please please please can someone help me
$this->load->library('form_validation');
$this->load->helper('cookie');
$data = array();
if($_POST) {
// Set validation rules including additional validation for uniqueness
$this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
$this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
$this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');
// Run the validation and take action
if($this->form_validation->run()) {
echo 'valid;
}
}
else{
echo 'problem';
}
Form validation is coming back with no errors can cany one see why?
Is it actually echoing 'valid'? (you're missing an apostrophe there, btw)
The code you show will only echo 'problem' when $_POST is false, not when validation fails.
Without knowing more, it may be as simple as:
// Run the validation and take action
if($this->form_validation->run()) {
echo('valid');
} else {
echo('invalid');
}
Try like this without checking if $_POST is set - not really needed:
//validation rules here
//
if ($this->form_validation->run() == TRUE) {
//do whatever that shall be run on succeed
} else {
$this->load->view('form'); //load the form
}
Read more about the controller part here

Resources