Home > Back-end >  Session loses its data or value after form submitting in php
Session loses its data or value after form submitting in php

Time:10-31

Greeting Senior and junior developers, I am new to php. Currently, I am facing a problem related to session. But before I describe my problem, I have tried other solutions as well and it didn't work. So here is my problem : I have two php files s1 and s2.php When I enter my name into the inputbox and click the submit button, I want my name to be displayed on another page which is s2.php. I want to achieve the above result by using session. What I have done is I wrote a conditional statement to cheak if the button has been submited and if it does, then assign the value I put in the input box in the url to my session variable. Then I defined session start on top of my html document. I have done the same to the s2 page and I tried to echo the session variable on the page but it displays nothing. Then I tried to cheak if the session is set or not and I have found that it has not been set yet.

So How to solve the problem? Why my session has not been set? If I tried without submitting form, it works fine. here is my s1.php code:

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
        <form action="s2.php" method="get">
            <input type="text" name="username">
            <input type="submit" name="submit">
        </form>
    

    <?php
        
            if(isset($_GET['submit'])){
                $_SESSION['name']=$_GET['username'];
            }
            
        
        
      
    ?>
</body>
</html>

here is my s2.php code:

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    

        if(isset($_SESSION['name'])){
            echo "it set"."<br>";
            echo $_SESSION['name'];
        }else{
            echo "it not set";
        }
    ?>
</body>
</html>

CodePudding user response:

s1.php is processed on the server & there your condition is false so an html form will be back & you see it in the browser. when you submit the form, form data will be sent to s2.php. there you're checking if session is set, tell us & show the content of session['name'].

so basically the problem is that your setter snippet at s1.php does not get the change at all to set session['name']. it is in the wrong place. like what Robert said move

if(isset($_GET['submit'])){
   $_SESSION['name']=$_GET['username'];
}

to s2.php before checking to see if session['name'] is set

& if you have nothing else to do with sessions in s1.php, you can remove

<?php
session_start();
?>

from the head of s1.php

best regards :)

  • Related