Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SESSION['username'] is not set

I have a form which takes the username and puts it in a SESSION after submit. Now when I check if SESSION username exists and echo it, it shows me the result perfectly, but when I want to redirect to another page if the same SESSION is not empty it tells me that the SESSION doesn't exist.

I don't get it?!

if(isset($_POST['submit'])){
    $user = $_POST['username']; 
    $_SESSION['username'] = $user;

    echo $_SESSION['username']; // This shows me the result perfectly

    if(isset($_SESSION['username'])){
    header('location:index.php?page=dashboard&user='.$_SESSION['username'].'');
    }
    else{
    echo "SESSION user isn't set."; // This is the result I get from the isset()
    }
}

Value 10106 is the value I want to get in this part, my page prints this:

SESSION user isn't set10106

It does the redirect but can't load anything because SESSION is empty, but when I click on my button logout it does show me the value in the URL:

action=logout&user=10106

So that means the session is set. But the isset() still gives me a false result. I didn't forget the session_start(), else my echo wouldn't give me result either.

This the form I submit:

<form action='index.php?page=login' method='post'>
    <table id='login'>
       <tr>
          <td colspan=2><br></td>
       </tr>
       <tr>
          <td>User:</td>
          <td><input type='username' name='username'></td>
       </tr>
       <tr>
          <td>Password:</td>
          <td><input type='password' name='password'></td>
       </tr>
       <tr>
          <td colspan=2><input type='submit' name='submit'></td>
       </tr>
    </table>
</form>

EXTRA INFO:

When I click submit, it stays on the same page, the URL didnt changed but my menu did. The menu changes when SESSION username is set, so when I click on another URL in the menu it shows me the SESSION in the URL.

index.php?page=new_call&user=10106

EDIT: OK, so the problem isn't the SESSION which isn't set, it's the redirect that doesn't work. After submitting the form it should redirect but stays on the same page right now.

like image 352
Matheno Avatar asked Dec 06 '25 23:12

Matheno


2 Answers

session_start();
$user = "admin";

if(!isset($_SESSION['user_name'])){
    if($_POST){
        if($_POST['username'] == $user){
            $_SESSION['user_name'] = $user;
        } else {
            echo "SESSION user isn't set.<br>";
        }

    }
    echo "input your session username ";
} else {
    echo  "Youve got Session " . $_SESSION['user_name'];
}

try it :D

like image 101
Alfath Dirk Avatar answered Dec 08 '25 13:12

Alfath Dirk


I think you need to structure your code to something like this:

<?php
session_start();
if(isset($_GET['page'])){
    $page=$_GET['page'];
}
else{
    $page="";
}

if(isset($_POST['submit'])){ //log in submit
    $user = $_POST['username']; 
    $_SESSION['username'] = $user;

    echo $_SESSION['username']; // This shows me the result perfectly

    if(isset($_SESSION['username']) && !empty($_SESSION['username'])){
    header('location:index.php?page=dashboard&user='.$_SESSION['username'].'');
    }
    else{
    echo "SESSION user isn't set."; // This is the result I get from the isset()
    }
}
if($page=="dashboard"){ //dashboard page

?>
Dashboard page<br>
Your session: <?php echo($_SESSION['username']); ?>
<?php
}
else{ //login page
?>
<form action='' method='post'>
    <table id='login'>
       <tr>
          <td colspan=2><br></td>
       </tr>
       <tr>
          <td>User:</td>
          <td><input type='username' name='username'></td>
       </tr>
       <tr>
          <td>Password:</td>
          <td><input type='password' name='password'></td>
       </tr>
       <tr>
          <td colspan=2><input type='submit' name='submit'></td>
       </tr>
    </table>
</form>
<?php
}
?>
like image 32
Jm Verastigue Avatar answered Dec 08 '25 13:12

Jm Verastigue