Saving log in details as cookies

Up until very recently the code belwo was working fine, but since Thursday it stopped working, am not sure if its maybe something of the code is now redundant, but I havent changed this code for months and for some reason its stopped working.

function onDataReceived(data)
{
if (data != 0){
//alert (data)
var stuff = data;
var split = stuff.split("/");
var obj = {ID: split[0], EMAIL: email, NAME: split[1], COMPANY: split[2]};
$.post("save_Session.php", obj) .done(function(data) {
location.href='result.php';
})
};

The split data isnt being posted and saved

<?php
session_start();
$_SESSION["user"]=$_POST["ID"];
$_SESSION["email"]=$_POST["EMAIL"];
$_SESSION["Name"]=$_POST["NAME"];
$_SESSION["company"]=$_POST["COMPANY"];
?>

Are you sure the data is coming back to you in the same format? (That might not be relevant if you also control whatever is sending that data to you, of course).

Ye I echo’d out data, and the info I need is fine, and isnt equalt to 0, what I also did was echo out the value of the cookies on the result page and they came back as empty. When up until last week, all was good, and nothing has been changed

I have just manually put values to send to save_Session and when I echo them out in result, nothing appears…

    if (data != 0){
//alert (data)
var stuff = data;
var split = stuff.split("/");
var obj = {ID: '4', EMAIL: 'info@accend4web.co.uk', NAME: 'lee', COMPANY: 'accend'};
$.post("save_Session.php", obj) .done(function(data) {
location.href='result.php';
})
};

Ah, I misread the original post and presumed that the email value was going through, but not the stuff in split[].

I know you’ll have checked, but if it’s hosted, has anything changed there, any updates / upgrades that might have kicked it off?

I’ll have to check that, but went a step further and manually entered the values directly to the cookies as below -

<?php
session_start();
$_SESSION["user"]="4";
$_SESSION["email"]="info@accend4web.co.uk";
$_SESSION["Name"]="lee";
$_SESSION["company"]="accend";
?>

Then on the result page, still nothing

echo ($userID." - user; ");
echo ($userEmail." - email; ");
echo ($userName." - name; ");
echo ($userCompany." - company; ");

So maybe now its not the post function, but the cookies being called, umm

I did wonder whether you can do what you want to do - whether your post call to save_Session.php just starts a new session on the server, stores the vars for that session, and then closes down again, losing the session vars as that session is now closed. But as you said it was working before, I figured it must be OK. My head says it shouldn’t work, though.

(sorry, deleted the comment you replied to).

Seems all good

<?php
error_reporting(E_ALL);
ini_set('display_errors','Off');
session_start();
$userID=$_SESSION["user"];
$userEmail=$_SESSION["email"];
$userName=$_SESSION["Name"];
$userCompany=$_SESSION["company"];

echo ($userID." - user; ");
echo ($userEmail." - email; ");
echo ($userName." - name; ");
echo ($userCompany." - company; ");

/*if (!empty($userID)){}else{header('Location: logIn.php');};
if (!empty($userEmail)){}else{header('Location: logIn.php');};
if (!empty($userName)){}else{header('Location: logIn.php');};
if (!empty($userCompany)){}else{header('Location: logIn.php');};*/
?>

Right ok, I’ll look into this then, as youve been spot on before…

Still though the cookies even when manually entered values, are still not outputting on the result page

That’s what I mean though - whether you manually enter them or get them from your original code, it’s the method of posting to ‘save_Session.php’ that I think shouldn’t work because I think it’ll just go into a separate server session. Even if you hard-coded the values into save_Session.php, if I’m correct it wouldn’t work either. I just don’t understand why it worked before.

But I stress that most of my thoughts on how the client->server stuff works isn’t with PHP, and isn’t on a web server platform that most would have worked with, but I think it worked pretty much the same as any other in this regard.

So what your saying is the storing of the values in save_session and then calling them from there is the problem, and my thinking was that I post them there first and then once posted I then allow the person to continue as the result.php page checks that the values are fulfilled before allowing them to continue, it was the steps to get to the page of hotels if you know what I mean.

So should I just use ajax to save the values as cookies, rather than posting them to save_Session, but I need those values in a different part of the site.

So something along the lines of this

setcookie($cookie_name, $cookie_value, time()+3600, "/");

//cookie check function
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

And use the if !isset else to send the user to result.php

Something like that - save_sesssion probably works just fine, but it runs in a new session and whatever it stores dies as soon as it’s finished, at which point it returns to your calling code and then redirects to result.php. I guess if you think about it, that must be right - when you use the same method to do an Ajax call to retrieve something from a database, you have to pass the data back to the calling code explicitly.

Saving them as cookies or session variables would do it, as long as you do it within the same server connection, I think.

Sorry droopsnoot, I’m not currently on your wave length, as am trying a few things and cant get those values into the other pages.

So the ajax is right is it, where its posting the values, but with them not collecting in save_Session, I’m not sure how to get around it, so that I can call them when I need to as in on the result page to detect that the user has logged in

No, they do collect in Save_session, it’s just that as soon as save_session stops running (at the close PHP symbol) they are lost because the session for that operation has gone. The .post operation (if I’m right, and I might not be) opens a connection to the server, runs your PHP, then closes the connection, so the session vars are gone.

Your JS code needs to store the values locally somehow as you said in #12, though I don’t know enough JS to know if that specific code will work.

1 Like

I see, ok thank you, i’ll give it a go and see if I can sort this out.

Thanks droopsnoot

1 Like

As an experiment I’m doing this below, and i’m not gettng the value back in the result page.

if (data != 0){
var stuff = data;
var split = stuff.split("/");
var obj = {ID: split[0], EMAIL: email, NAME: split[1], COMPANY: split[2]};
setcookie($cookie_ID, split[0]);
location.href='result.php';
};

<?php
if(!isset($_SESSION[$cookie_ID])) {
echo "Cookie named '" . $cookie_ID . "' is not set!";
} else {
echo "Cookie '" . $cookie_ID . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_ID];
}
?>

I don’t know javascript, but is setcookie() correct? That comes up as a PHP function for me, and suggests you should use document.cookie, also you seem to have a PHP variable in there as well. And then at the start of your PHP code, you check for isset($_SESSION...) rather than $_COOKIE, is that correct?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.