0

I'm developing a login form using an ajax calling.

First of all, I have my HTML code that comes below this paragraph

<form id="login_form" name="login" method="POST"> <input id="login_email" name="login_email" type="text" placeholder="Email" /> <input id="login_password" name="login_password" type="password" placeholder="password" /> <input id="login_checkbox" name="login_remember" type="checkbox" /> <input type="submit" /> </form> 

After verifying with JQuery all the errors in the client, I do an ajax calling like the code below.

$.ajax({ url: '../ajax/login.php', cache: false, type: "POST", data: $( "#login_form" ).serialize(), dataType: 'json', success: function(data){ if (!data){ //Logged correctly! window.location = "../page/colabora"; } }); 

Then, login.php has been called... and here is the problem. I try to set a cookie like setcookie('usr', $value) with the id of the client logged but there's no cookie comming back.

Checking colabora.php, it doesn't bring any cookie as a callback response.

Where could be the problem?

Thanks in advice


UPDATE

I should have been more specific with the ajax calling.... My script takes the form comming bellow.

// Our custom secure way of starting a PHP session. $secure = false; // This stops JavaScript being able to access the session id. $httponly = true; // Forces sessions to only use cookies. if (ini_set('session.use_only_cookies', 1) === FALSE) { header("Location: ../page/serror?err=-2"); exit(); } // Gets current cookies params. $cookieParams = session_get_cookie_params(); session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); // Sets the session name to the one set above. session_name('sec_session_id'); session_start(); // Start the PHP session session_regenerate_id(); // regenerated the session, delete the old one. setcookie('usr', 'example_cookie'); 

UPDATE 2

After a lot of time and many time wasted trying to find a solution, I just decided to record an aux session param in the login.php and record the cookie in colabora.php when the user has been successfully conected.

4
  • session_start() for sure! Commented Nov 7, 2014 at 8:37
  • problem is at server side. ajax/login.php is your problem Commented Nov 7, 2014 at 8:39
  • Do you have some extra output before SETCOOKIE? Something in the error_log? Check if you have something like "headers already sent..." Commented Nov 7, 2014 at 8:41
  • I've updated my answer being more specific with my php code. I got a secure session session_start. I agree with Panoptic, but I don't find the issue. I've tried to debug everything but apparently, there's any problem. About outputs, I echo json_encode($error_msg); a variable error when there are errors and $success if everything's okay. Commented Nov 7, 2014 at 8:45

2 Answers 2

1

try to add:

session_write_close() 

from the PHP site:

It is a good idea to call session_write_close() before proceeding to a redirection using

header("Location: URL"); exit(); 

because it ensures the session is updated (in a file or into a database, depending on the handler you're using) BEFORE you redirect the visitor somewhere else.

Sign up to request clarification or add additional context in comments.

1 Comment

I just set the session_write_close between session_regenerate_id(); and setcookie('usr', 'example_cookie'); but still not working.
0

start the session using session_start() in your php code. Then it will surely set the cookie

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.