javascript - Set cookie with php from an ajax call -
i've reviewed lot of posts on subject already, code doesnt quite seem setting cookies.
so initial ajax call 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 equal") ) location.reload(); else $('#error1').html(msg).show(100); } }); });
where wrote strcmp()
function in different js library.
after doing bunch of hashing , salting stuff php looks like:
<?php if($hashpass == $pass) { echo "passwords equal"; $cookieinfo = $_post['user'].",".$clearance; setcookie("loggedin", $cookeinfo,0,"/"); exit; } else { echo "invalid password"; exit; } ?>
when test out, "passwords equal" message comes , page gets reloaded... cookie never gets set when check browser.
i'm not sure i'm doing wrong... i've used code before when used form submit directly php file , works fines, i'm using ajax cookie doesnt seem set.
is because i'm not setting cookie after header("location: ...");
call..?
you have typo in setcookie()
statement. result, no data available cookie.
setcookie("loggedin", $cookeinfo,0,"/");
should be
setcookie("loggedin", $cookieinfo,0,"/"); // cookie != cooke
additionally, should remove echo
statement avoid sending output before headers. header information (including cookie-setting) must sent before other types of output.
Comments
Post a Comment