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.
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
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
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With