GregL83 Posted April 20, 2008 Share Posted April 20, 2008 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 More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 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 More sharing options...
p2grace Posted April 20, 2008 Share Posted April 20, 2008 Is the cookie not being set, or are you trying to access it without refreshing the page? The page must be refreshed in order to use a saved cookie. I'd use ajax to redirect to a page and save the cookie then redirect back to continue your javascript processing. Link to comment https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522290 Share on other sites More sharing options...
GregL83 Posted April 20, 2008 Author Share Posted April 20, 2008 the issue is accessing the cookie. I believe it is never set because it is trying to be set through an ajax page. It seems I cannot set a cookie on an ajax page with php....I cannot figure a away around this Link to comment https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522292 Share on other sites More sharing options...
GregL83 Posted April 21, 2008 Author Share Posted April 21, 2008 so any ideas???? How can I set cookies from an ajax page??? Link to comment https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522348 Share on other sites More sharing options...
p2grace Posted April 21, 2008 Share Posted April 21, 2008 You say the issue is with accessing the cookie, is this before or after the page has been refreshed? I've set cookies while using ajax before and never had an issue Link to comment https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522440 Share on other sites More sharing options...
redarrow Posted April 21, 2008 Share Posted April 21, 2008 try it this way as printed.......... setcookie("number","one",time()+3600); Link to comment https://forums.phpfreaks.com/topic/102048-php-ajax-cookie/#findComment-522453 Share on other sites More sharing options...
GregL83 Posted April 21, 2008 Author Share Posted April 21, 2008 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 More sharing options...
GregL83 Posted April 21, 2008 Author Share Posted April 21, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.