-1

There may be better ways of doing this, but I'm pretty sure I should still get my session_id() passed to the second page. Instead, calling this on page 2 as my first few lines generates a brand new session_id() than the one created on page 1.

PAGE 2 BEGINNING:

<?php
error_reporting(E_ALL);ini_set('display_errors',1);
session_start();
echo session_id();
$sessionid = session_id();
echo "sessionNUM &nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp   = $sessionid\n";
echo "<br>";

echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';

PAGE 1 ALL:

<?php
error_reporting(E_ALL);ini_set('display_errors',1);
session_start();
$sessionid = session_id();
$currentDate = date('Y-m-d');

//echo "sessionNUM &nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp   = $sessionid\n";
//echo "<br>";
//echo "<br>";
//echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';

session_unset();
$old_sessid = session_id();
session_regenerate_id();
$new_sessid = session_id();
session_id($old_sessid);
session_destroy();


session_regenerate_id(FALSE);


session_start();
echo session_id();
echo "<br>";
$sessionid = session_id();
$currentDate = date('Y-m-d');

echo "sessionNUM &nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp   = $sessionid\n";
echo "<br>";
echo "<br>";
echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';

//Connect to DB

        require_once 'configPOS.php';
        $conn = new mysqli($hn, $un, $pw, $db);
        if ($conn->connect_error) die($conn->connect_error);


//Enter Session ID and set Order ID
    //search for session info already exsiting

    $result=$conn->query("SELECT * FROM POS_HEADERS WHERE sessionid='$sessionid' AND date='$currentDate'");
    echo mysql_error();
    if(mysqli_num_rows($result) > 0){
    echo "session info already exists";
    }
     else{

        echo "test.";

/*
$sessionid = session_id();
*/




$sql="INSERT INTO POS_HEADERS (sessionid, date)
VALUES('$sessionid', '$currentDate')";

       if ($conn->query($sql) === TRUE) {
                  echo "New order started.<br>";
        } 
        else {
           echo "Error " . $sql . "<br>" . $conn->error;
        }
}
        $res=$conn->query("select ORDID from POS_HEADERS where sessionid='$sessionid'");
        list($ORDERNUM)= $res->fetch_row();
        //echo "<br>";        
        echo "<br>";
        echo "ORDERNUM &nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp   = $ORDERNUM\n";

        $_SESSION["OrderNum"] = $ORDERNUM;
        echo "<br>";        
        //echo "<br>";
        echo "Session variables are set.";


 $res=$conn->query("select ORDID from POS_HEADERS where sessionid='$sessionid'");
        list($ORDERNUM)= $res->fetch_row();
        echo "ORDERNUM= $ORDERNUM\n";

//echo "<br>";   

echo $_SESSION["OrderNum"];
$ordernum= $ORDERNUM;
echo $ordernum;
echo '<br>';
$LOCATION = $_POST["pickedlocation"];
echo $LOCATION;
echo "<br>";   
echo "<br>";   


echo "END Debugging Info";
echo "<br>";   
echo "<br>";   

echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';

echo "<br>";   
echo session_id();

echo "<br>";   
?>


<input type="button" value="Home" class="homebutton" id="btnHome" 
onClick="window.location = 'https://www.example.com/POS/home.php'" />

My original problem was

PAGE 2 having one session_id(), lets call it "A",   
PAGE 3 had a previous session(id), lets call it "B", 
PAGE 4 had session_id() "A" again, and 
PAGE 5 had session_id() "B" again.

All pages 2-5 do are insert data into a database for that session_id(), but for some reason PAGE 2 kept creating a new session_id() after PAGE 1 created one, which has got me to where I am now.

Things I have discovered - The session_save_path() is writeable, the url path is identical up to the page (...). Also, on the same URL there are very similar pages in a different directory that work flawlessly, so I think it's a code error, not a php.ini error.

user3866044
  • 181
  • 6
  • 20
  • Can you check if your session cookie is persisting at the client end and is being sent back to the server? Also, check if session persistence is enabled on the server. – Akshat Singhal Dec 17 '18 at 06:03
  • I've got PAGE 2-5 working correctly sharing the same session_id now. It just that PAGE 1 to 2 switch that isn't working. I guess it can work like this, but it skips an order number in the database like this when it makes the new id from PAGE 1 to 2. Once it gets the session_id on page 2, it keeps that session_id to page 5 now. – user3866044 Dec 17 '18 at 06:14
  • Your code seems to make rather little sense. Why are you calling both session_start and session_regenerate_id _multiple_ times within the same script? – misorude Dec 17 '18 at 08:43

2 Answers2

1

First, Is your browser rejecting a cookie? In that case, you may always get a new session id. Different browsers have different options/panel to accept a cookie.

Second, edit the following fields in php.ini to suitable values.

session.use_cookies
session.save_path
session.use_trans_sid
Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41
  • I've got PAGE 2-5 working correctly sharing the same session_id now. It just that PAGE 1 to 2 switch that isn't working. – user3866044 Dec 17 '18 at 06:13
  • You cannot have any echo/output before session_start(). For page 1 and 2, make sure that the files are plain text, not unicode files. https://stackoverflow.com/questions/32986445/remove-a-bom-character-in-a-file Save file as ANSI. – Bimal Poudel Dec 17 '18 at 06:17
0

Two pages have different session_start() calls? If you want to use one session_id for all your pages you should use only one for session_start() call for both of that files.

For example, create the third page let's name it bootstrap.php and include it at the beginning of the PAGE1 and PAGE2. In this file, you can start your session.

Hope I understand your problem correctly.

alexey-novikov
  • 593
  • 2
  • 16