Benjamin Oats Benjamin Oats - 23 days ago 8
PHP Question

loosing data on page refresh php session

I am posting data from one site to another, then turning that posted data into a session.

form from other site

<form action="https://www.assessmentday.co.uk/abintegro/" method="POST">
<input type="hidden" name="userid" value="1010101">
<input type="submit" value="Go" style="font-size:14px; padding:20px;">




I then post this data to my new site where I turn it into a variable
$userid


then checking that the variable is empty or not, if not show some code.

check variable is not empty

if (!empty($userid)) {
?>
<div class="container">
<?php
echo "<hr>";
echo "You Are Logged In,</br>";
echo "Your User ID is <strong>" . $_POST['userid'] . "</strong>.";


When I refresh the page with a button click I loose the session data, so the code in the check dose not show.

how do I have a button to refresh to page and keep session data ?

button to refresh

<button class="btn btn-primary hidebutton showbuttonv2" role="button" id="homebutton" onClick="history.go(0)">Home</button>


Full code spinet

<?php

session_start();

$_SESSION['userid'] = $_POST['userid'];

$userid = $_SESSION['userid'] = $_POST['userid'];

//$_SESSION["userID"] = $userid;

if (!empty($userid)) {
?>
<div class="container">
<?php
echo "<hr>";
echo "You Are Logged In,</br>";
echo "Your User ID is <strong>" . $_POST['userid'] . "</strong>.";

Answer

This is because you're always overwriting the session data with the POST data, even if there isn't anything in the $_POST['userid'] field.

I see that you are very new to programming (PHP), and as such I would recommend you to sit down and think a bit about what you want to do. Then try to write down short item lists of the steps needed to do these things. Remember to be really specific, as that is what you need to be when writing the code. This is called pseudo-code, and is a vital tool in programming.
Once you've done that, read through the code you've written, and really read what it does. Not just what you expect it to. Then you'll learn a lot about how programming works. ;)

Short example, based upon the code you've posted:

  • Start session.
  • If user submitted ID -> Store ID in session
  • If session contains user ID -> Show login confirmation.
    : Else -> Show login form

Your code, in "English" form.

// Start the session
session_start ();

// Save the POSTed value from the form in the session array.
$_SESSION['userid'] = $_POST['userid']; 

// Save the POSTed value fromthe form in the session array, and a variable of its own.
$userid = $_SESSION['userid'] = $_POST['userid'];


// If we have a (non-empty) value in the user ID variable.
if (!empty ($userid)) {
    // Show the login confirmation.
}

As you can see, there is a discrepancy between your expected behavior and the code you wrote. :)

Remember: Details always matter in programming.

Also, using JavaScript's history to create breadcrumb isn't the best option. This will lead to somewhat unpredictable behavior for the users, especially if they come to your page from a web search. (It'll take them to the search engine again, instead of your home page.)
That is why I recommend using proper URLs instead. Either relative, or dynamically created full ones. That way you always have full control where the user ends up when clicking on your links. :)

Edit, Added #2: Here's how I'd write the code, to perform the tasks outlined by the pseudo-code.

// Using sessions to persist data across page views.
session_start ();

// Check if user has tried to log in.
if ($user = login_user ($username, $password)) {
    // He has. Use session to keep him logged in.
    $_SESSION['user'] = $user;
}

// If the user is logged in, show confirmation message.
if (!empty ($_SESSION['user']['id'])) {
    $page = 'login_confirmed';
} else {
    $page = 'login_form';
}

See how the comments, and the code, aligns withe the pseudo-code from the bullet list? And how it describes exactly what you want, in detail? ;)

Comments