1
votes

There are similar questions related to the topic but none of them have solved my problem. Its kind of weird but my $_SESSION is working on the same page but not on any other page. If I put isset($_POST['submit') the condition doesn't satisfy and without it the $_SESSION remains null.

This is my code.

This is the login page.

<!-- Login.php  -->
<?php
session_start();
?>
<html>
<body>
<form method="post" action="profile.php">
<fieldset>

<legend>
Login
</legend>
<label> User ID  :</label>   <input type="text" placeholder="Username" name="user"><br>
<label> Password  :</label>   <input type="password" placeholder="Password" name="password">


<input type="submit" name="submit" value="Login">
</fieldset>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
?>

This is where I want my session variable to appear.

<!--  profile.php  -->
<?php
session_start();
echo "Session user is ".$_SESSION['USER']."<br>";
unset($_SESSION['USER']);
unset($_SESSION['PASS']);
session_unset();
session_destroy();
?>

This is what I have tried :

  1. Changing form method to GET.
  2. Using $_REQUEST and $_GET.
  3. Using $_SESSION on the same page. It works on the same page.
  4. Checking session id. The session on the other pages are present but values are either null or empty.
  5. Running the code without isset(). In that case all the session variables remain NULL.
1
The form action is profile.php - but you are checking for isset() inside login.php and of course it will not satisfy. You should either change the action in the form - or move your code into profile.phpIVO GELOV

1 Answers

0
votes

$_POST['submit'] and the rest of the post parameters are not available in Login.php They are available only in profile.php because your form action points to it.

You may move the following code after the session_start() in profile.php.

if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}

Keep in mind that you unset the session values in the end of profile.php