Reputation: 1
I just developed a user login system and want to create different sessions for different users.
How do I write a code in PHP so that it starts a session and if the username entered is "a", it redirects to a different page and if the username is "b", it redirects to a different page.
I used this
session_start();
switch ($_SESSION['username']) {
case 'a':
// $_SESSION['username'] is 'a'
// redirect to file1.php
header('Location: file1.php');
die();
break;
case 'b':
// $_SESSION['username'] is 'b'
// redirect to file2.php
header('Location: file2.php');
die();
break;
default:
// $_SESSION['username'] is neither a, nor b, or is not set at all
// redirect to default.php
header('Location: default.php');
die();
break;
}
but no matter what username I enter, it is taking me to file1.php
Upvotes: 0
Views: 206
Reputation: 28132
session_start();
// ...
if ($_SESSION['username'] == 'a') {
header("Location: ./page_a");
} else if ($_SESSION['username'] == 'b') {
header("Location: ./page_b");
} else {
header("Location: ./otherpage");
}
Upvotes: 2
Reputation: 3928
Just like quantumSoup's code but He has made a simple error, use this:
session_start();
// ...
if ($_SESSION['username'] == 'a') { header("Location: ./page_a"); } else if ($_SESSION['username'] == 'b') { header("Location: ./page_b"); } else { header("Location: ./otherpage"); }
Upvotes: 0
Reputation: 2592
Using switch()
instead of if statements:
session_start();
switch ($_SESSION['username'])
{
case 'a':
// $_SESSION['username'] is 'a'
// redirect to file1.php
header('Location: file1.php');
die();
break;
case 'b':
// $_SESSION['username'] is 'b'
// redirect to file2.php
header('Location: file2.php');
die();
break;
default:
// $_SESSION['username'] is neither a, nor b, or is not set at all
// redirect to default.php
header('Location: default.php');
die();
break;
}
Note that this alone isn't enough to be secure. In file1.php and file2.php you must also call session_start()
and carry out a similar check that the user is allowed to view this page, before displaying the page content.
Upvotes: 1
Reputation: 38506
All of your PHP pages should start with:
<?php
session_start();
You'll set session variables like this:
$_SESSION['myVariableName'] = 'myIntendedValue';
So when you process the login, you'll likely set a session variable for username or userid, and then you can handle that redirection:
if (isset($_SESSION['userName']) && $_SESSION['userName'] == "a") {
header("Location: pageA.php");
exit();
} else if (isset($_SESSION['userName']) && $_SESSION['userName'] == "b") {
header("Location: pageB.php");
exit();
}
Upvotes: 1
Reputation: 16841
You may want to read the PHP.net documention on session_start().
Basically, every page will start with session_start()
to initiate the PHP session. After that, you have access to the global $_SESSION[]
array which you can use to store and read data.
For example, you could store the username in the session during the login process:
$_SESSION['username'] = 'a';
Then, in your other pages, you would read the session username, find out the username and do proper handling:
if (isset($_SESSION['username']))
{
header("Location: newlocation.php");
}
Upvotes: 0
Reputation: 7750
session_start();
if($_SESSION['username'] == 'a')
header('Location: pageA.php');
else
header('Location: pageB.php');
Upvotes: 0
Reputation: 7349
Simply start php sessions with:
session_start() ;
Then, on login, save the logged in user's id to:
$_SESSION['currentuser'] = $userid ;
On logout, don't forget to:
unset($_SESSION['currentuser']) ;
On the page you want to display differently to each user, check $_SESSION['currentuser']
and then redirect:
if($_SESSION['currentuser'] == 1) {
Header("Location:/page1.php") ;
} else {
Header("Location:/page_default.php") ;
}
exit ;
Upvotes: 1