PHP Session data not passing!!!

18 replies
Hi guys. I'm back again with another issue that makes no sense at all... .
I set a $_SESSION var on one page starting the session on both pages that use the var. So not starting the session is not the problem. ANYWAY I set it on one page and print it just to be sure that it is set, and it is. THEN I redir to another page, start the session and check the var and it is empty!!! It works for me on IE but not on my buddys pc using IE... It also doesnt work on chrome on either pc. This is the code that sets the var. THIS ONE WORKS BECAUSE I CAN ECHO IT ON THIS PAGE:

//JUST AN EXCERPT
$pwd = $row['pwd'];
$prepass = md5($pass);
if($prepass == $pwd){
$yes = "yes";
$_SESSION['MNCADMINLI'] = $yes;
}
if ($_SESSION['MNCADMINLI'] == 'yes'){
echo $_SESSION['MNCADMINLI'];
}
//IT ECHOS JUST FINE HERE


----------------------------------------------------------------
//THIS IS THE SECOND PAGE

session_start();
if ($_SESSION['MNCADMINLI'] == 'yes'){
//A BUNCH OF TABLES
echo $_SESSION['MNCADMINLI'];
}
//THIS DOES NOT WORK...
----------------------------------------------------------------
Anybody got a clue?
#data #passing #php #session
  • Profile picture of the author Earnie Boyd
    A session is a relationship between the user browser and the server. The data in the session is stored in a cookie in the browser. If cookies are disabled in the browser you will not have the session data stored between pages.
    Signature
    {{ DiscussionBoard.errors[5959532].message }}
    • Profile picture of the author PHPSpaZ
      Originally Posted by Earnie Boyd View Post

      A session is a relationship between the user browser and the server. The data in the session is stored in a cookie in the browser. If cookies are disabled in the browser you will not have the session data stored between pages.
      Thats just it, All the browsers I have tested on have had cookies enabled... I was doing the same thing with a cookie b4 but its not good for our purposes and it wasnt working properly either. It was the same code except I set cookie and it printed on the first page, and just like with session storing it did not print on the second page. Are there any noticeable problems with the code that jump out to you?
      {{ DiscussionBoard.errors[5959569].message }}
    • Profile picture of the author Brandon Tanner
      Originally Posted by Earnie Boyd View Post

      The data in the session is stored in a cookie in the browser. If cookies are disabled in the browser you will not have the session data stored between pages.
      Normally, only the ID of the session is stored in a cookie. The actual session data is stored on the server.

      But it's possible to create a session without relying on cookies at all, by passing the session ID via a query string in the URL...

      PHP: Passing the Session ID - Manual
      Signature

      {{ DiscussionBoard.errors[5960979].message }}
  • Profile picture of the author Earnie Boyd
    Make sure you're not outputting any data before the session_start. Note that any blank lines before <?php is considered output to the browser. Also you might want to use session_id() function to determine if a session is started already and use ob_start() to make sure that nothing is output to the browser before the session_start(). See PHP: session_start - Manual for more but you've probably been there already. I don't see anything staring at me that is wrong.
    Signature
    {{ DiscussionBoard.errors[5959773].message }}
    • Profile picture of the author PHPSpaZ
      Thanks again, I def made sure there is not even so much as a pixel after <? and before session_start(); Ill try the session id and echo it.
      {{ DiscussionBoard.errors[5959850].message }}
      • Profile picture of the author SteveJohnson
        Originally Posted by PHPSpaZ View Post

        Thanks again, I def made sure there is not even so much as a pixel after <? and before session_start(); Ill try the session id and echo it.
        Make sure there is nothing BEFORE the <?php in your file.

        In addition to checking the session_id, probably the easiest way to see the contents is to dump the variable:

        var_dump( $_SESSION );

        You'll be able to see what's there and what isn't.

        Do you have error display on so you can catch notices and such?
        Signature

        The 2nd Amendment, 1789 - The Original Homeland Security.

        Gun control means never having to say, "I missed you."

        {{ DiscussionBoard.errors[5960289].message }}
        • Profile picture of the author PHPSpaZ
          Yeah, the top of the page before any information is sent is where I am starting the sessions. Im posting the whole code. Login page, process page, and display page.
          -------------------------------------------------------------------------------------------
          //login

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>Untitled Document</title>
          </head>

          <body>
          <center>
          <form name="viewleads" action="http://www.mynewcity.com/adminliprocess.php" method="post">
          E-Mail:<input type="text" name="email" id="email" /></br>
          Password:<input type="password" name="pass" id="pass" /></br>
          <input type="submit" id="submit" value="Log In" />
          </form>
          </center>
          </body>
          </html>

          ----------------------------------------------------------------------------------------
          //process

          <?
          session_start();
          function ticket_connect() {
          $db_host = "NOTSHOWINGUTHIS";
          $db_user = "NOTSHOWINGUTHIS";
          $db_password ="NOTSHOWINGUTHIS";
          $db_name = "NOTSHOWINGUTHIS";
          //create database connection
          $connection = @mysql_connect($db_host,$db_user,$db_password);
          //if there is no connection then display a message letting the user know.
          if($connection) {
          @mysql_select_db($db_name, $connection);
          }
          else{
          echo "ERROR!! Could not connect to the database at this time, we are sorry for the inconvenience. Please try again later.";
          }
          } // end function db_connect()
          ?>
          <?
          ticket_connect();
          $result = mysql_query("SELECT email, pwd FROM administrators WHERE email='$email'") or die(mysql_error());
          $row = mysql_fetch_array($result);
          $pwd = $row['pwd'];
          $prepass = md5($pass);
          if($prepass == $pwd){
          $yes = "yes";
          $_SESSION['MNCADMINLI'] = $yes;
          }
          if ($_SESSION['MNCADMINLI'] == 'yes'){
          ?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>Untitled Document</title>
          </head>

          <body>

          <script type="text/javascript">
          window.location="http://mynewcity.com/adminassignment.php";
          </script>

          <? } else {
          echo "<center>Sorry, LOGIN FAILED! Please <a href='http://mynewcity.com/adminlogin.php'>CLICK HERE</a> to return and try again.</center>";
          echo "***PWD**";
          echo $pwd;
          echo "***PREPASS***";
          echo $prepass;
          echo "***RESULT***";
          echo $result;
          echo "***ROW***";
          echo $row;
          echo "***EMAIL***";
          echo $row['email'];
          echo "***DBPWD***";
          echo $row['pwd'];
          echo "***RUHERE***";
          echo $_SESSION['MNCADMINLI'];
          //THIS ECHO WORKS!!!
          }
          ?>
          </body>
          </html>

          ---------------------------------------------------------------------------------------
          //Display

          <?
          session_start();
          if ($_SESSION['MNCADMINLI'] == 'yes'){
          ?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>Assignment Op Center</title>
          </head>
          <body>

          <?
          function ticket_connect() {
          $db_host = "NOTSHOWINGUTHIS";
          $db_user = "NOTSHOWINGUTHIS";
          $db_password = "NOTSHOWINGUTHIS";
          $db_name = "NOTSHOWINGUTHIS";
          //create database connection
          $connection = @mysql_connect($db_host,$db_user,$db_password);
          //if there is no connection then display a message letting the user know.
          if($connection) {
          @mysql_select_db($db_name, $connection);
          }
          else{
          echo "ERROR!! Could not connect to the database at this time, we are sorry for the inconvenience. Please try again later.";
          }
          } // end function db_connect()

          ticket_connect();

          $result = mysql_query("SELECT ticketID,created,lead_status,subject,lo_franchise, email,name,lead_email,lead_name FROM mynewci_ticket") or die(mysql_error());
          $numrows= mysql_num_rows($result);
          echo "<center><bold>There are $numrows leads in the system</bold></center>";
          echo "<table id='tickettable' border='1'>";
          echo "<th>Ticket Number</th><th>Capture Date</th><th>Status</th><th>Subject</th><th>Franchise</th><th>Lead Owner</th><th>Owner Name</th><th>Lead Email</th><th>Lead Name</th><th>Submit</th>";


          $i = 0;

          while($row = mysql_fetch_array( $result )) {

          if($i <= $numrows){
          $ticketID = $row['ticketID'];
          $created = $row['created'];
          $subject = $row['subject'];
          $lo_franchise = $row['lo_franchise'];
          $lo_email = $row['email'];
          $lo_name = $row['name'];
          $lead_email = $row['lead_email'];
          $lead_name = $row['lead_name'];
          $status = $row['lead_status'];
          $oh = "id='$i'";
          $on = "name='$i'";
          $selstatus = "<form method='post' '$oh' '$on' action='http://mynewcity.com/assignleads.php'><tr><td>$ticketID</td><td>$created</td><td>$status</td><td>$subject</td><td>$lo_franchise</td><td>'$lo_email'</td><td>'$lo_name'</td><td>'$lead_email'</td><td>'$lead_name'</td><td><input type='hidden' name='ticketID' value='$ticketID'><input type='submit' name='doit' value='Assign Lead'></form></td></tr>";


          // Print out the contents of each row into a table
          echo $selstatus;
          $i++;
          }
          }

          }
          echo "</table>";
          ?>
          </body>
          </html>
          {{ DiscussionBoard.errors[5960440].message }}
  • Profile picture of the author baronz
    check '$prepass = md5($pass);'
    $pass = .....
    maybe you forget to delcalre $pass
    {{ DiscussionBoard.errors[5962463].message }}
    • Profile picture of the author PHPSpaZ
      Originally Posted by baronz View Post

      check ' = md5();'
      = .....
      maybe you forget to delcalre
      nope, it comes via post from the login page. It is in the code above.
      {{ DiscussionBoard.errors[5962529].message }}
  • Profile picture of the author baronz
    ouw sorry, my bad..
    cause i don't see $pass = $_POST['pass'];
    {{ DiscussionBoard.errors[5962558].message }}
  • Profile picture of the author Earnie Boyd
    //process

    <?
    session_start();
    Is this how the beginning of the file looks?

    It should look more like

    <?php
    //process

    session_start();
    Signature
    {{ DiscussionBoard.errors[5963832].message }}
    • Profile picture of the author PHPSpaZ
      Originally Posted by Earnie Boyd View Post

      Is this how the beginning of the file looks?

      It should look more like
      No Earnie. I just put the space in there to make it easier to read. Absolutely nothing other than the php tag before the session_start();
      And for Bunny, I am not willing to pay for help atm, I tried several services a week or so ago and was HIGHLY disappointed in the help I received. The people here are VERY smart and perfectly willing to help so Im gonna stick with them. Thanks for the info though, it may come in handy one day!

      On another note, I called our hosting provider and they looked over my code only to tell me that it was well written, but not perfect, yet they saw no way that my code was the issue... They also checked every server log, NOTHING IS WRONG!!! I dont understand how my code validates -- AND there is no problem with the cloud server -- AND I know my browsers are set up correctly for this. I was stuck on this for a week before I even posted this and now I'm simply looking for a new method of authenticating...
      Any Ideas?
      {{ DiscussionBoard.errors[5967405].message }}
  • Profile picture of the author CMSBunny
    Try this link for some help with this issue - cmshelplive.com
    {{ DiscussionBoard.errors[5965441].message }}
  • Profile picture of the author xrampage16
    Are you absolutely sure that your browser is not at fault, or any anti-spam software? I would first check out using other browsers, and making sure that you have the most up to date browsers. Try Firefox and Chrome first, as they seem to be most able to work the best with things like "sessions". Also, aside from that, check that you have cookies and javascript turned on. I know that that is a stupid suggestion, but you might have heightened security on one of those controls which is preventing you the information from passing between pages.

    Also, if you are still running into issues, remove all the content you are trying to pass, and make sure you can pass something like "test" between pages. If you are able to pass "test" successfully, then you might have an issue with the wrapping and unwrapping of the content, or you are using a character which is throwing everything off. At that point, you just need to drill down into the content until you find the culprit, and either convert it into something else, or remove it entirely.

    Hope that helps.
    {{ DiscussionBoard.errors[5967876].message }}
  • Profile picture of the author baronz
    do you have access to php.ini file?
    if you do, this might help:
    support.qualityunit.com/021373-How-To-Enable-Session-Support-for-PHP
    {{ DiscussionBoard.errors[5970006].message }}
  • Profile picture of the author Big Squid
    I checked the code from your original post, and it works for me. I'm wondering if it has something to do with the JS redirect that's happening. I don't know why, but maybe you could check.
    {{ DiscussionBoard.errors[5982812].message }}
    • Profile picture of the author PHPSpaZ
      Originally Posted by Big Squid View Post

      I checked the code from your original post, and it works for me. I'm wondering if it has something to do with the JS redirect that's happening. I don't know why, but maybe you could check.
      It was actually a strange thing that took a long time to figure out and makes no sense whatsoever. My sessions would not store text info inside the session vars and pass them from the login process page to the actual content page after login was successful. I had to take out the session vars and use post data instead..... NOT JUST THAT but the data that I was passing via host WOULD NOT pass between the two pages if it was text
      ie.
      $_SESSION['MNCADMINLI'] = "THISTEXT";
      Instead I had to use JS to send post data for authentication... Here is the working version...:

      * indicates new page

      ************************************************** ******
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Administrator Login</title>
      </head>

      <body>
      <center>
      <form name="viewleads" action="http://www.mynewcity.com/adminliprocess.php" method="post">
      E-Mail:<input type="text" name="email" id="email" /></br>
      Password:<input type="password" name="pass" id="pass" /></br>
      <input type="submit" id="submit" value="Log In" />
      </form>
      </center>
      </body>
      </html>
      ************************************************** *******
      <?
      session_start();
      ?>
      <?
      include "func.php";
      ticket_connect();
      $result = mysql_query("SELECT email, pwd FROM administrators WHERE email='$email'") or die(mysql_error());
      $row = mysql_fetch_array($result);
      $pwd = $row['pwd'];
      $prepass = md5($pass);

      $pwtest = yup($prepass,$pwd);

      if ($pwtest == true){
      ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Processing Area Video Leads</title>
      </head>

      <body>
      <form id="pwtesting" action="" method="">
      <input type="hidden" id="pwtest" value="" />
      <script type="text/javascript">
      window.location="http://mynewcity.com/adminassignment.php?pwtest=<? echo $pwtest; ?>";
      </script>
      <?
      } else {
      echo "<center>Sorry, LOGIN FAILED! Please <a href='http://mynewcity.com/adminlogin.php'>CLICK HERE</a> to return and try again.</center>";
      echo "***PWD**";
      echo $pwd;
      echo "***PREPASS***";
      echo $prepass;
      echo "***RESULT***";
      echo $result;
      echo "***ROW***";
      echo $row;
      echo "***EMAIL***";
      echo $row['email'];
      echo "***DBPWD***";
      echo $row['pwd'];
      echo "***RUHERE***";
      echo $pwtest;

      }
      ?>
      </body>
      </html>
      ************************************************** *******
      <?
      session_start();
      include "func.php";
      ?>
      <?
      if ($pwtest == true){
      ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Assignment Operations Center</title>
      </head>
      <body>

      <?
      ticket_connect();
      $result = mysql_query("SELECT ticketID,created,lead_status,subject,lo_franchise, email,name,lead_email,lead_name FROM mynewci_ticket") or die(mysql_error());
      $numrows= mysql_num_rows($result);

      echo "<center><bold>There are $numrows leads in the system</bold></center>";
      echo "<table id='tickettable' border='1'>";
      echo "<th>Ticket Number</th><th>Capture Date</th><th>Status</th><th>Subject</th><th>Franchise</th><th>Lead Owner</th><th>Owner Name</th><th>Lead Email</th><th>Lead Name</th><th>Submit</th>";

      $i = 0;

      while($row = mysql_fetch_array( $result )) {

      if($i <= $numrows){
      $ticketID = $row['ticketID'];
      $created = $row['created'];
      $subject = $row['subject'];
      $lo_franchise = $row['lo_franchise'];
      $lo_email = $row['email'];
      $lo_name = $row['name'];
      $lead_email = $row['lead_email'];
      $lead_name = $row['lead_name'];
      $status = $row['lead_status'];
      $oh = "id='$i'";
      $on = "name='$i'";
      $selstatus = "<form method='post' '$oh' '$on' action='http://mynewcity.com/assignleads.php'><tr><td>$ticketID</td><td>$created</td><td>$status</td><td>$subject</td><td>$lo_franchise</td><td>'$lo_email'</td><td>'$lo_name'</td><td>'$lead_email'</td><td>'$lead_name'</td><td><input type='hidden' name='ticketID' value='$ticketID'><input type='submit' name='doit' value='Assign Lead'></form></td></tr>";


      // Print out the contents of each row into a table
      echo $selstatus;
      $i++;
      }
      }

      } else {
      echo "HELLO";
      echo $pwtest;
      }
      echo "</table>";
      ?>
      </body>
      </html>
      ************************************************** *******
      And that was it.... Works fine... I see massive quantities of AJAX training in my future
      {{ DiscussionBoard.errors[5984523].message }}
  • Profile picture of the author IM-software
    At first check how session.auto_start is set up in your php.ini

    session.auto_start = 1 |||| means no need to use session_start();

    if you use session.auto_start = 0 try: <?php session_start(); ?>
    At the very top of your file.

    instead of

    <?php
    session_start();
    ?>

    Make sure that you have:

    session.cookie_lifetime = 0 |||| zero means: until browser is restarted
    session.use_cookies = 1
    session.use_only_cookies = 1 |||| as a protection

    Restart apache
    {{ DiscussionBoard.errors[5985288].message }}

Trending Topics