Jump to content

PHP AJAX COOKIE


GregL83

Recommended Posts

Hello,

 

I need help setting a cookie from an ajax page.  Basically

 

page.php (javascript uses ajax to POST data to ajax page -> ajax_page.php (recieves posted data and sets cookie)

 

Cookie never gets set.  I cannot access the cookie with any pages.  I have tested setting cookies without ajax and it works.

 

I use:  setcookie("number","one",time() + 3600, "/");

 

Please help...

Link to comment
https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/
Share on other sites

When you use setcookie() all you are doing is sending a header to the browser requesting the browser to set the cookie. I'm not sure if the browser will intercept and process cookie headers for AJAX requests.

 

This doesn't seem to be a PHP problem. Try setting the cookie with JavaScript. You can get help with that on google or the JavaScript forum: http://www.phpfreaks.com/forums/index.php/board,6.0.html

Link to comment
https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522249
Share on other sites

Weird I will go into more detail.

 

I have refreshed the page many times with no result.

 

I am using the correct code to set a cookie as I have tested it without AJAX on the same vserver.

 

Basically, I have a login script that uses AJAX to run PHP code and validate the username and password entered in form fields.  If valid, php sets session variables and returns result code back to the calling page.  At this point the page is redirected.

 

I am trying to have the ajax page also set cookies if user selects remember me checkbox.  I simply passed the checkbox value, along with username and password, of true|false to the ajax page.  The PHP adds the cookie depending on the value of the checkbox.

 

My browser never recognizes the cookie that was set from the ajax page.  I have tried testing basic ajax request setting cookie and got no results as well.

Link to comment
https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522455
Share on other sites

thats seems confusing...here is a sample that DOESNT work...

 

file: set.php

<script type="text/javascript">

var ajaxRequest;  // AJAX main variable

try{

// Opera 8.0+, Firefox, Safari

ajaxRequest = new XMLHttpRequest();

} catch (e){

// Internet Explorer Browsers

try{

ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try{

ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e){

// Something went wrong

alert("Your browser broke!");

return false;

}

}

}

// Create Function To Receive Data Sent From Server

ajaxRequest.onreadystatechange = function(){

if(ajaxRequest.readyState == 4){

var result = ajaxRequest.responseText;

alert(result);

}

}

var params = "ajax=greg";

var url = "ajax.php";

ajaxRequest.open("POST", url, true);

ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

ajaxRequest.setRequestHeader("Content-length", params.length);

ajaxRequest.setRequestHeader("Connection", "close");

ajaxRequest.send(params);

}

</script>

<?php

# display cookie

if(isset($_COOKIE["name"])){

echo "Cookie set to \"".$_COOKIE["name"]."\"";

}

else{

echo "Cookie not set";

}

?>

 

file: ajax.php

<?php

if(isset($_POST['ajax'])){

$variable = $_POST['ajax'];

setcookie("name",$variable, time() + 3600, "/");

}

?>

 

file: home.php

<?php

# home

if(isset($_COOKIE["name"])){

echo "Cookie is set to \"".$_COOKIE["name"]."\".";

}

else{

echo "Cookie not set";

}

?>

 

I used this code to try and test the ajax setcookie example with no success....

Link to comment
https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522463
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.