BoarderLine Posted July 14, 2009 Share Posted July 14, 2009 Hi, I have an ajax function which is changing a value in the DB and also updating the users SESSION variable. Form:- <form id="userhome" method="post" action="javascript:insert()"> <input type='submit' style='font:9pt Arial, Helvetica, sans-serif; cursor:hand; background:#666666; color: #FFFFFF; ' value="Set This As Home Page" /> <input name="set" id="set" type="hidden" value="Users_Home.php" /> </form> Javascript:- (also utilizes ajax_framework.js) var nocache = 0; function insert() { // Optional: Show a waiting message in the layer with ID login_response document.getElementById('insert_response').innerHTML = "Just a second..." // Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding. var sethome = encodeURI(document.getElementById('set').value); // Set te random number to add to URL request nocache = Math.random(); // Pass the login variables like URL variable http.open('get', 'insertchangehome.php?sethome='+sethome+'&nocache = '+nocache); http.onreadystatechange = insertReply; http.send(null); } function insertReply() { if(http.readyState == 4){ var response = http.responseText; // else if login is ok show a message: "Site added+ site URL". document.getElementById('insert_response').innerHTML = 'HOME PAGE HAS BEEN CHANGED'+response; } } PHP:- <?php require_once('Connections/MyDatabaseConnection.php'); ?> <?php if(isset($_GET['sethome'])){ unset($_SESSION['kt_userhome']); $userid=$_SESSION['kt_login']; $sethome=$_GET['sethome']; $_SESSION['kt_userhome']=$sethome; $insertSite_sql = "UPDATE users SET userhome = '$sethome' WHERE UserID = '$userid'"; $insertSite = mysql_query($insertSite_sql) or die(mysql_error()); echo $_SESSION['kt_userhome']; } else { echo 'Error changing home page!'; } ?> The problem is strange. Everything works correctly here, and the changed session value is being passed back to the form page from the php when I echo it, however once I leave the form page the SESSION reverts back to the previous value (as if SESSION value was not changed at all). This has been driving me nuts. Anyone have any suggestions? P.S. I have session_autostart=1 in php.ini, so session_start() at the beginning of the php code only returns the result ignoring session_start(), session already started etc.. Link to comment https://forums.phpfreaks.com/topic/165893-stuck-with-changing-session-value-from-ajax-function/ Share on other sites More sharing options...
BoarderLine Posted July 14, 2009 Author Share Posted July 14, 2009 Is this most likely an Ajax problem more so than PHP?? Link to comment https://forums.phpfreaks.com/topic/165893-stuck-with-changing-session-value-from-ajax-function/#findComment-875454 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2009 Share Posted July 14, 2009 Usually when you have session values that change (revert) depending on the page you are on, it is because the hostname/subdomain or path changed when going between the pages and the session cookie parameters are not setup to match or you are intentionally passing the session id on the end of the URL and it did not get passed or you changed from a relative URL to an absolute URL (php does not automatically append the session id to the url when using absolute URL's.) You might want to echo the session_id() on each page to see what it actually is. Link to comment https://forums.phpfreaks.com/topic/165893-stuck-with-changing-session-value-from-ajax-function/#findComment-875472 Share on other sites More sharing options...
BoarderLine Posted July 15, 2009 Author Share Posted July 15, 2009 Thanks PFMaBiSmAd for your response it's appreciated!! session_id() echo'd on both the form page and the php insert page are the same. The session id is not being passed on the end of the URL, so I assume it is something to do with your first comment: it is because the hostname/subdomain or path changed when going between the pages and the session cookie parameters are not setup to match Can you please give me more information regarding session cookie parameter setup, and how this will effect what is happening???? Thanks. Link to comment https://forums.phpfreaks.com/topic/165893-stuck-with-changing-session-value-from-ajax-function/#findComment-875524 Share on other sites More sharing options...
PFMaBiSmAd Posted July 15, 2009 Share Posted July 15, 2009 If the session_id is the same, it means that the same session is being used/reused on the two pages. The hostname/path settings I mentioned would have caused a new/different session to exist, each with different values in them. If you are seeing different/old values when you go between pages that have the same session_id, it either means that your code is causing the value to change or that the pages are being cached at some point and you are viewing page contents that are not current. Link to comment https://forums.phpfreaks.com/topic/165893-stuck-with-changing-session-value-from-ajax-function/#findComment-875842 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.