Welcome Guest, Not a member yet? Register   Sign In
Help with the Correct Approach for Setting Sessions and Using URL Redirect
#1

[eluser]downset[/eluser]
Hi,

I'm trying to implement the following login validation and session setting for a login form:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login2 extends CI_Controller {

public function index()
{

$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->library('form_validation');
$this->form_validation->set_rules("username", "Username", "required");
$this->form_validation->set_rules("password", "Password", "required");

  if($this->form_validation->run() == false){
  
  $headerData["title"] = "Login!";
  
  $data["error_message"] = "please enter you user name and password <br>";
  
  $this->load->view("view_header", $headerData);
  $this->load->view("view_login_page", $data);
  $this->load->view("view_footer");
  }
  else {
  echo "you are logged in!";
  
  //AK set error_reporting(0); to hide warning messages  
  //error_reporting(0);
  $this->session->set_userdata('username', $username);
  $this->session->set_userdata('password', $password);
  redirect("../");
  }
  
}

}

However I keep get the following error:

Quote:A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ci_basic_website/application/controllers/login2.php:25)

Filename: libraries/Session.php

Line Number: 672

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ci_basic_website/application/controllers/login2.php:25)

Filename: libraries/Session.php

Line Number: 672

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ci_basic_website/application/controllers/login2.php:25)

Filename: helpers/url_helper.php

Line Number: 546

I see in the CI documentation the following related to the URL helper (inc Redirect)

Quote:Note: In order for this function to work it must be used before anything is outputted to the browser since it utilizes server headers.

As I am loading a view on the first instance of loading /login2 if the input boxes for username and password don't contain values, is this causing the problem as the headers are being set on the initial load? If yes, can anyone please advise the best approach to submit the login form to itself, rather than submitting to a new page?

I am also running my test on MAMP, if that has any relevance. I have also checked that there is no white space or html before my main controller, as per instructions on some other topics, so this doesn't appear to be the cause of the errors.

I would appreciate any help the community can provide.

Thanks in advance! Smile
#2

[eluser]vrencianz[/eluser]
Code:
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("username", "Username", "required");
$this->form_validation->set_rules("password", "Password", "required");

if($this->form_validation->run() == false){
  $headerData["title"] = "Login!";

  $data["error_message"] = "please enter you user name and password <br>";

  $this->load->view("view_header", $headerData);
  $this->load->view("view_login_page", $data);
  $this->load->view("view_footer");
}
else {
  // echo "you are logged in!";

  //AK set error_reporting(0); to hide warning messages
  //error_reporting(0);
  
  $username = $this->input->post('username');
  $password = $this->input->post('password');
  
  $this->session->set_userdata('username', $username);
  $this->session->set_userdata('password', $password);
  redirect("../");
}

}

Try this one instead. Cheers!
#3

[eluser]boltsabre[/eluser]
Yeah, it's your echo that's causing the conflict.

Just a couple of things, your should use trim in your validation rules, get rid of white space

Code:
$this->form_validation->set_rules("username", "Username", "trim|required");

Otherwise if I enter a user name with a space at the start or end it wont be found in the DB when you make your model call.

You shouldn't store your password in the session, there is absolutely NO need to do this...ever!

Code:
redirect("../");

Wont work, CI is not based on the "tree" model as such when navigating to other controllers, you should be specifying an actual controller name, perhaps something like
Code:
redirect("logged_in");


You can also do this to minimise code and reduce the amount of processing power your consuming:
Code:
$this->session->set_userdata('username', $this->input->post('username'));
#4

[eluser]InsiteFX[/eluser]
or
Code:
redirect('/');
#5

[eluser]downset[/eluser]
Excellent, thanks for your help, I'm no longer getting the header error! also thanks to everyone for the tips regarding redirect and also trimming the input! Big Grin
#6

[eluser]phpfresher[/eluser]
From my quick view donot use echo or any html tag befor redirect or use flush




Theme © iAndrew 2016 - Forum software by © MyBB