jtorral Posted yesterday at 02:18 AM Share Posted yesterday at 02:18 AM Don't know if I should post this here or in the jquery forum. But here is the deal. My index.php has the following .... <?php session_start( [ 'cookie_lifetime' => 86400, ]); //session_start(); //$SID = session_id(); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); include_once( '../gallerysoftconfig.php'); include_once( 'functions.php'); include_once( '../iconfig.php'); include_once( 'indexPreChecks.php'); include_once( 'sessionChecks.php'); include_once( 'screenDetect.php'); //print_r($_SESSION); //$xyz = $_SESSION['gs_session_screen_width']; //echo "Width: $xyz <br>"; As you can see lots commented out for testing Then the issue is in screenDetect.php And here is screenDetect.php <?php if( isset($_REQUEST['sw'] ) ) { $screenWidth = $_REQUEST['sw']; $_SESSION['gs_session_screen_width'] = $screenWidth; //$s = $_SESSION['gs_session_screen_width']; //echo "New session processed: $s"; return; } if(isset($_SESSION['gs_session_screen_width']) ) { $screenWidth = $_SESSION['gs_session_screen_width']; //echo "Session processed: $screenWidth"; return; } if(! isset($_SESSION['gs_session_screen_width']) ) { echo ' <script src="https://apis.google.com/js/platform.js" async defer></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script> <script type="text/javascript"> var screenWidth = screen.width; $.ajax({ type: "POST", url: "/screenDetect.php", data: {sw: screenWidth}, success: function() { } }); </script> '; } ?> There are some comments there that I use for validation But the story is, that the ajax call is simply getting the device screen size and calling it self with a POST. That works just fine The if( isse($_REQUEST['sw']) ) does get executed and sets the session. I have validated this by assigning the session to a variable and it does print it. I even do a print_r($_SESSION) and I see it. However, when I pickup the code in index.php, the session that was set there is no longer visible. Its like it never existed. I have tried the session_start() at the begining of screenDetect but no luck. Any idea what is going on ??? Thanks JT Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/ Share on other sites More sharing options...
mac_gyver Posted yesterday at 02:45 AM Share Posted yesterday at 02:45 AM (edited) are the include_once statements (you should actually use require for things your code must have and include/require are not functions, the () around the path/file do nothing and should be remove) exactly as you have shown or are they actually using http(s) requests and you have redacted that information in the posted code? note: php's error_reporting and display_errors should be set before you execute any other php code (ideally they would get set in the php.ini on your system), in case that code produces an error. if the session is not persisting between requests, it's likely that the session_start() is failing, but you don't know that since you are setting the error_reporting/display_errors after the session_start() statement. also, you cannot set display_startup_errors in your code because php has already started at that point. edit: so I looked at the javascript. the php code that gets executed due to the http request it makes must have its own session_start(), but only if it has been requested via the ajax call, and the session parameters must be exactly the same as the index.php's session_start(). Edited yesterday at 02:50 AM by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/#findComment-1648936 Share on other sites More sharing options...
jtorral Posted yesterday at 06:13 AM Author Share Posted yesterday at 06:13 AM (edited) I managed to get a little further with these mods. But there is still an issue. <?php // $SID is set in indet.php session_start() if (session_id() != '') { session_write_close(); } session_id($SID); session_start(); if( isset($_REQUEST['sw'] ) ) { $screenWidth = $_REQUEST['sw']; $_SESSION['gs_session_screen_width'] = $screenWidth; return; } I am capturing the session id from the first session start on index.php and setting that into the variable SID then I close the session in the handler for the ajax call as you can see above. This has gotten me a lot further. But I cannot see the session until I refresh the page. In the index file I have this <?php ini_set('display_errors', 1); error_reporting(E_ALL); session_start(); //header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1 //header("Pragma: no-cache"); // HTTP 1.0 //header("Expires: 0"); // Proxies $SID = session_id(); include_once( '../gallerysoftconfig.php'); include_once( 'functions.php'); include_once( '../iconfig.php'); require_once 'indexPreChecks.php'; require_once 'sessionChecks.php'; require_once 'screenDetect.php'; echo "<pre>"; print_r($_SESSION); echo "</pre>"; You can see where SID is set, then the call to screenDetect.php Then I display sessions I dont see the session I refresh the page the session shows up now. I need it to be set and be visible first time around. I tried some cache settings but they didnt work. I left them in the code with comments Edited yesterday at 06:14 AM by jtorral Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/#findComment-1648940 Share on other sites More sharing options...
Moorcam Posted yesterday at 03:06 PM Share Posted yesterday at 03:06 PM 8 hours ago, jtorral said: I managed to get a little further with these mods. But there is still an issue. <?php // $SID is set in indet.php session_start() if (session_id() != '') { session_write_close(); } session_id($SID); session_start(); if( isset($_REQUEST['sw'] ) ) { $screenWidth = $_REQUEST['sw']; $_SESSION['gs_session_screen_width'] = $screenWidth; return; } I am capturing the session id from the first session start on index.php and setting that into the variable SID then I close the session in the handler for the ajax call as you can see above. This has gotten me a lot further. But I cannot see the session until I refresh the page. In the index file I have this <?php ini_set('display_errors', 1); error_reporting(E_ALL); session_start(); //header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1 //header("Pragma: no-cache"); // HTTP 1.0 //header("Expires: 0"); // Proxies $SID = session_id(); include_once( '../gallerysoftconfig.php'); include_once( 'functions.php'); include_once( '../iconfig.php'); require_once 'indexPreChecks.php'; require_once 'sessionChecks.php'; require_once 'screenDetect.php'; echo "<pre>"; print_r($_SESSION); echo "</pre>"; You can see where SID is set, then the call to screenDetect.php Then I display sessions I dont see the session I refresh the page the session shows up now. I need it to be set and be visible first time around. I tried some cache settings but they didnt work. I left them in the code with comments You need to make sure that the session is started correctly and that the session ID is set before any output is sent to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/#findComment-1648970 Share on other sites More sharing options...
mac_gyver Posted yesterday at 05:48 PM Share Posted yesterday at 05:48 PM 10 hours ago, jtorral said: I am capturing the session id from the first session start on index.php and setting that into the variable SID this doesn't exist at the point when the ajax request is made, because they are two different http(s) requests. 10 hours ago, jtorral said: But I cannot see the session until I refresh the page. that's because ALL the code in index.php has already been executed during the initial http(s) request for index.php, by the time the ajax request occurs. for the initial case when the session variable doesn't exist, after you output the javascript code, the php code needs to exit/die without doing anything else (it currently runs to completion.) the ajax request needs to be to index.php, instead of screenDetect.php. the code will then set the session variable and it will be available when the rest of the code in index.php runs. 1 Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/#findComment-1648978 Share on other sites More sharing options...
gizmola Posted 16 hours ago Share Posted 16 hours ago Basically what is being described to you is a race condition. You aren't going to be able to hack around it. A PHP script runs on the server, and has "page/request" scope. The client provide an HTTP request, and the server provides an HTTP response, and the connection is closed. Ajax is used to make changes to the state of a fully rendered DOM on the client. You are not going to be able to trick PHP into doing some of its work -- delaying while the client's DOM is in a partial/indeterminate state, in order for ajax to spawn another request, and then have the original PHP script resume in the way you hope it will, all so that you can get access to session variables that didn't even exist when the original HTTP request was made. I have no idea why you are trying to store some client state in a session, given that browser dimensions are dynamic, relative to the device and decisions made by the client. It's not clear what you expect to do with these dimensions, which you're getting from javascript code, but you certainly don't need to put them into a session, and you haven't made an attempt to explain what problem you are trying to solve, but this is not the way to solve whatever problem that is. If you want to actually take the time to explain the "problem to be solved" we might be able to better advise you on ways to solve it. Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/#findComment-1648996 Share on other sites More sharing options...
jtorral Posted 4 hours ago Author Share Posted 4 hours ago thanks, will try index.php for ajax request. Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/#findComment-1649019 Share on other sites More sharing options...
jtorral Posted 3 hours ago Author Share Posted 3 hours ago The problem is as follow. 1. This is some old code thats been around for ages. 2. Mobile devices have different screen sizes. 3. Setting the session of the screen size lets me manipulate the various page width and image sizes to display on a mobile device based on the width. If a device has a screen width of x I want to make sure the image is set for that screen width. with different device screens, the image can be too big or small. 4. Setting the session also let me access that stores screen size of other functions on the page like resizing pop up boxes and so on. Hope that explains it Quote Link to comment https://forums.phpfreaks.com/topic/326692-going-insane-ajax-call-sets-session-as-expected-but-does-not-persist/#findComment-1649024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.