PHP Session ID is the same, but variables are lost

I have a login page that sets up session parameters on successful login. The login page is then redirected to the admin page. The admin page can read session variables easily. But when I execute the jQuery load () function, which loads viewUsers.php in the contents of the div, the session variables have disappeared. The strange part is the session identifier.

I used var_dump () on both the admin page and the viewUsers page. All the correct variables from the login page are displayed on the admin pages, but the viewUsers page, called by the jQuery load () var_dump function in $ _SESSION, is empty. The var_dump of $ _COOKIE ['PHPSESSID'] has the correct identifier, but it makes no sense to me.

This is how I set the session variables.

$_SESSION['userID'] = $userInfo['ID']; $_SESSION['userType'] = $userInfo['userType']; $_SESSION['programID'] = $userInfo['programID']; 

This is jQuery

 $("#content").load("viewUsers.php"); 

All pages have session_start (). Session variables also did not work when I tried to use window.open and window.location instead of the jQuery load () function.

Some, like session variables, are lost, although I have the correct session id. If anyone could shed light on this, I would really appreciate it!

I am currently filling out hidden fields and using the post function instead of the load to get around it. I understand that this is not the best way, but this is the only way to understand how to do it.

Edit: Here is the top of the index that reads session variables perfectly.

  <?php session_start(); //require("session.php"); if(!isset($_SESSION['userID'])){ header("location: ../login/index.php"); } ?> 

Here are all viewusers

  <?php session_start(); //foreach($_POST as $name => $value){ //$_SESSION[$name] = $value; //} //echo " session id " . $_COOKIE['PHPSESSID']; var_dump($_COOKIE); var_dump($_SESSION); ?> <?php require("adminFunctions.php"); ?> <h2>View Current Users</h2> <?php require("userlinks.php"); ?> <table id="userTable" class="tablesorter"> <?php getUsers($_SESSION['programID'], $_SESSION['userType']) ?> </table> <script type="text/javascript"> $('td[name="userID"]').hide(); //$("#userTable th").click(function(){ //color(); //colorTable(); //color(); //}); function colorTable(){ $("tr:odd").css("background-color", "#c0c0c0"); $("tr:even").css("background-color", "#ffffff"); } function color(){ $("tr:odd").css("background-color", "#ffffff"); $("tr:even").css("background-color", "#ffffff"); } $(document).ready(function(){ //colorTable(); $("#userTable").tablesorter({widgets: ['zebra']}); }); </script> 

Other Edit: Here is the javascript code to load viewusers The only reason I use the post is that I set the session variables as hidden fields to pass the session variables. On viewusers, I use a foreach loop to set session variables. I understand that this is not safe. function maintainUsers () {

 $.post("viewUsers.php", $("#sessionform").serialize(),function(data){ //alert(data); $("#content").load("viewUsers.php"); }); } 
+4
source share
6 answers

It may be a long shot, but are you using any frames or cms? I know that wordpress will delete session variables (http://blog.ginchen.de/en/2008/08/15/session-variablen-in-wordpress/), can you show the javascript code you use to load viewUsers ? Are you programming on a local server?

+1
source

If you have the correct $ _ COOKIE ['PHPSESSID'] , try adding the session_id () destination to session_start () like this

 <?php /** Add this: **/ if (isset($_COOKIE['PHPSESSID'])) session_id($_COOKIE['PHPSESSID']); /** Start session **/ session_start(); /** Rest of the script... **/ if(!isset($_SESSION['userID'])){ header("location: ../login/index.php"); } ?> 

I use this all the time and have no problems with sessions in PHP.

+1
source

I suspect the session is not starting. I had similar problems in the past to find out that the session did not start in the first place.

Question: did you try to print the value in the session variable to make sure that it is?

Before calling the page and after calling the page.

+1
source

Typically, this error occurs if you do not use session_start() to read session data.

Try posting (although, as you said, you have one, I would suggest double-checking)

 session_start(); 

At the beginning of your viewUsers.php

If the above is not the case, then your current page (the one from which you execute the .load() function) is a session reset and fake values. If you do not download the code or find it, then there is no solution.

0
source

You have spaces before the open php tag. They send output to the browser, and so the session does not start.

0
source

I would suggest something simple. Include this in EVERY file.

 <?php session_name("yourapplication_session"); session_start(); ?> 
0
source

All Articles