1

I've reviewed a lot of posts on this subject already, but my code doesnt quite seem to be setting any cookies.

so my initial ajax call is something like this:

$("#loginForm").submit(function(event) {
    event.preventDefault();
    var uid = document.getElementById("userLogin").value;
    var pid = document.getElementById("passLogin").value;
    var url = "../_scripts/loginScript.php";

     $.ajax({
            type: "POST",
            url: url,
            datatype : "script",            
            data: {user: uid, pass: pid},
            success: function(msg) {
                if( strcmp(msg,"passwords are equal") )
                    location.reload();
                else                        
                    $('#error1').html(msg).show(100);       
            }
    });
});

where i wrote the strcmp() function in a different js library.

after doing a bunch of hashing and salting stuff my php looks something like:

<?php
    if($hashPass == $pass)
    {
        echo "passwords are equal";
        $cookieInfo = $_POST['user'].",".$clearance;
        setcookie("loggedIn", $cookeInfo,0,"/");
        exit;
    }
    else
    {
        echo "Invalid password";
        exit;
    }
?>

when i test it out, the "passwords are equal" message comes back and the page gets reloaded... but my cookie never gets set when i check my browser.

I'm not sure what I'm doing wrong... I've used this code before when I used a form to submit directly to a php file and it works fines, but now that I'm using ajax the cookie doesnt seem to get set.

Is it because I'm not setting the cookie after a header("Location: ..."); call..?

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
nyduss
  • 137
  • 2
  • 13

2 Answers2

2

You have a typo in your setcookie() statement. As a result, no data is available for the cookie.

setcookie("loggedIn", $cookeInfo,0,"/");

should be

setcookie("loggedIn", $cookieInfo,0,"/"); // cookie != cooke

Additionally, you should remove the echo statement to avoid sending output before headers. All header information (including cookie-setting) must be sent before other types of output.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • wow i cant believe i didnt see that. thanks for pointing that out. and it would it not make sense to just move the echo statement to the line below the setcookie..? – nyduss May 09 '13 at 18:52
  • Yes, you can move the echo below `setcookie()`. That will allow all headers to be sent before data. – George Cummins May 09 '13 at 18:52
  • ok I moved the echo to below the setcookie() call and it works great now, thanks so much for the help. The only problem I am having now though is that my strcmp() funtion no longer works and the page doesn't reload... got any suggestions for that? do you know if setting the cookie some how change the data type of msg? – nyduss May 09 '13 at 21:12
  • @user2367497 Javascript doesn't have `strcmp().` Maybe try [this](http://stackoverflow.com/questions/1179366/is-there-a-javascript-strcmp). – George Cummins May 09 '13 at 21:15
  • 1
    lol no, strcmp() is a function i created myself. I got it working now though thanks for all the help. – nyduss May 09 '13 at 21:29
  • Ah, I understand now. You're welcome, and enjoy! – George Cummins May 10 '13 at 01:55
0

A few things here. Echoing data before calling setcookie() will throw an error because PHP thinks headers have been sent. Also you have a typo in your code. Your setting the cookie value to $cookieInfo and your using $cookeInfo as parameter of setcookie().

atorres757
  • 601
  • 5
  • 9