0

The following code works on my desktop in chrome for setting up a session. However, on my phone in chrome it constantly gives new sessionid upon refreshing. I've read through the other posts on her about a new sessionid every time and it seems to come down to permissions but there are no answers given. Is there something I can do to ensure this works on everyone's device, or is there a better way? I was trying to make a simple online ordering site for my restaurant.

<html>
<head>

 <title>Online Ordering</title>

</head>
<style>
h3 {
    text-align: center;
}
h5 {
    text-align: center;
}
</style>
<body>


<?php 
session_start();

$sessionid = session_id();
$currentDate = date('Y-m-d');

echo "sessionNUM &nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp   = $sessionid\n";
echo "<br>";        

//Connect to DB

        require_once 'configordonline.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 HEADERS WHERE sessionid='$sessionid' AND date='$currentDate'");
    echo mysql_error();
    if(mysqli_num_rows($result) > 0){
    echo "session info already exists";
    }
     else{


session_start();
$sessionid = session_id();

        $sql="INSERT INTO HEADERS VALUES (NULL, '$sessionid', '$currentDate', 'noneyet')";
        if ($conn->query($sql) === TRUE) {
                  echo "New record created successfully";
        } 
        else {
           echo "Error " . $sql . "<br>" . $conn->error;
        }
}
        $res=$conn->query("select ORDID from 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.";


?>

</body>
</html>

output on desktop:

sessionNUM          = 055666a122f5f77e748880c5e488c443 
session info already exists

ORDERNUM          = 77 

Session variables are set. 

on mobile (new sessionNUM and ORDERNUM on every refresh:

sessionNUM          = a703f4b3492be025c7b01cda45fb3653 
New record created successfully

ORDERNUM          = 113 

Session variables are set. 
user3866044
  • 181
  • 6
  • 20

1 Answers1

0

Per this post, I discovered that the session start should come before the html, then the rest of the php after.

Login works on desktop but not mobile?

It seems to be working fine now.

<?php 
error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
?>
<html>
<head>

 <title>Online Ordering</title>

</head>
<style>
h3 {
    text-align: center;
}
h5 {
    text-align: center;
}
</style>
<body>


<?php 


$sessionid = session_id();
$currentDate = date('Y-m-d');

echo "sessionNUM &nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp   = $sessionid\n";
echo "<br>";        

//Connect to DB

        require_once 'configordonline.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 HEADERS WHERE sessionid='$sessionid' AND date='$currentDate'");
    echo mysql_error();
    if(mysqli_num_rows($result) > 0){
    echo "session info already exists";
    }
     else{


$sessionid = session_id();

        $sql="INSERT INTO HEADERS VALUES (NULL, '$sessionid', '$currentDate', 'noneyet')";
        if ($conn->query($sql) === TRUE) {
                  echo "New record created successfully";
        } 
        else {
           echo "Error " . $sql . "<br>" . $conn->error;
        }
}
        $res=$conn->query("select ORDID from 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.";


?>
Community
  • 1
  • 1
user3866044
  • 181
  • 6
  • 20