/    Sign up×
Community /Pin to ProfileBookmark

PHP Login Redirect – Multiple Users

How can I modify this to accompany multiple users with the ability to redirect people to different ‘main.php’ pages based on their user and pass? I’d like to do this within the PHP and not use a database.

login.php

[code=php]
<?php
// start the session
session_start();

$errorMessage = ”;
if (isset($_POST[‘txtUserId’]) && isset($_POST[‘txtPassword’])) {
// check if the username and password combination is correct
if ($_POST[‘txtUserId’] === ‘theuser’ && $_POST[‘txtPassword’] === ‘thepass’) {
// the username and password match,
// set the session
$_SESSION[‘basic_is_logged_in’] = true;

// after login we move to the main page
header(‘Location: main.php’);
exit;
} else {
$errorMessage = ‘Sorry, wrong username / password’;
}
}
?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>

<body>

<?php
if ($errorMessage != ”) {
?>
<p align=”center”><strong><font color=”#990000″><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
<form action=”” method=”post” name=”frmLogin” id=”frmLogin”>
<table width=”400″ border=”1″ align=”center” cellpadding=”2″ cellspacing=”2″>
<tr>
<td width=”150″>User Id</td>
<td><input name=”txtUserId” type=”text” id=”txtUserId”></td>
</tr>
<tr>
<td width=”150″>Password</td>
<td><input name=”txtPassword” type=”password” id=”txtPassword”></td>
</tr>
<tr>
<td width=”150″>&nbsp;</td>
<td><input name=”btnLogin” type=”submit” id=”btnLogin” value=”Login”></td>
</tr>
</table>
</form>

</body>
</html>
[/code]

main.php

[code=php]
<?php
// start the session
session_start();

// is the one accessing this page logged in or not?
if (!isset($_SESSION[‘basic_is_logged_in’]) || $_SESSION[‘basic_is_logged_in’] !== true) {
// not logged in, move to login page
header(‘Location: login.php’);
exit;
}

?>
<html>
<head>
<title>Main User Page</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>

<body>

<p>This is the main application page. You are free to play around here since you are an autenthicated user πŸ™‚ </p>
<p>&nbsp;</p>
<p><a href=”logout.php”>Logout</a></p>

</body>
</html>
[/code]

logout.php

[code=php]
<?php
// start the session
session_start();

// if the user is logged in, unset the session
if (isset($_SESSION[‘basic_is_logged_in’])) {
unset($_SESSION[‘basic_is_logged_in’]);
}

// now that the user is logged out,
// go to login page
header(‘Location: login.php’);
?>
[/code]

to post a comment
PHP

15 Comments(s) ↴

Copy linkTweet thisAlerts:
@tbirnsethNov 18.2005 β€”Β why not do something like:

[CODE]
$username = get_username();
$password = get_password();
$user_dat = array(
'user1' => array('password' => 'theirpassword', 'location' => 'location to goto'),
'user2' => array('password' => 'theirpassord2', 'location' => 'location2 to goto')
);

if(isset($user_dat[$username]) && ($user_dat[$username]['password'] == $password) )
header("Location: " .$user_dat[$username]['location']);
die( isset($user_dat[$username]) ? "Bad Password." : "Unknown User");
[/CODE]


But why would you want to keep all this in a file you'd have to maintain versus a database?

tony
Copy linkTweet thisAlerts:
@wyclefauthorNov 20.2005 β€”Β Because i'm new to PHP and wouldn't know where to begin to use a database. This seems to work. What do you think? I'd be open to using a database if u think u could walk me through it. I've got phpMyAdmin running on my server but I have no idea how to use it.

[code=php]
<?php
session_start();

$data=array("username1"=>array("url"=>"somefile.php","password"=>"password1"),
"username2"=>array("url"=>"someotherfile.php","password"=>"password2"));

if(isset($_POST['username']) && isset($_POST['password'])) {
if($data[$_POST['username']]['password'] == $_POST['password']) {
$_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
header('Location: ' . $data[$_POST['username']]['url']);
} else {
echo "Wrong user name or password. <br>";
logIn();
}
} else {
logIn();
}
?>

<?php
function logIn() {
?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<p align="center"><strong><font color="#990000"></font></strong></p>

<form action="" method="post" name="frmLogin" id="frmLogin">
<table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="150">User Id</td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td width="150">Password</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td width="150">&nbsp;</td>
<td><input name="btnLogin" type="submit" id="btnLogin" value="Login"></td>
</tr>
</table>
</form>

</body>
</html>
<?php
}
?>
[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsNov 20.2005 β€”Β Not doing something for the sake of never doing before doesn't make much sense...
Copy linkTweet thisAlerts:
@SheldonNov 21.2005 β€”Β Go for the Database!! Its alot better.

Here is a step by step article to walk you into it.

http://www.charles-reace.com/login_article.html
Copy linkTweet thisAlerts:
@wyclefauthorNov 22.2005 β€”Β Ok, i'm looking into the Db stuff. In the meantime i'd also like to figure out this other weaker method and I have something that is working now however I was wondering if someone could figure out how to modify this to implement md5 encrypted passwords?

[code=php]
<?php
session_start();

$data=array("user1"=>array("url"=>"file1.php","password"=>"pass1"),
"user2"=>array("url"=>"file2.php","password"=>"pass2"));

if(isset($_POST['username']) && isset($_POST['password'])) {
if($data[$_POST['username']]['password'] == $_POST['password']) {
$_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
header('Location: ' . $data[$_POST['username']]['url']);
} else {
login('Wrong user name or password. <br>');
}
} else {
login();
}
?><?php

function login($response='This is the default message, if there is none provided') {

?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1>Login</h1>
<p><?=$response?></p>
<form action="" method="post">
<label for="username">User Id</label>
<input name="username" type="text" /><br />

<label for="password">Password</label>
<input name="password" type="password"><br />
<input type="submit" value="Login" />
</form>
</body>
</html>
<?php } ?>
[/code]
Copy linkTweet thisAlerts:
@SheldonNov 22.2005 β€”Β [url=http://php.net/md5]MD5 @ php.net[/url] the almighty source for all php information
Copy linkTweet thisAlerts:
@wyclefauthorNov 23.2005 β€”Β thx...although their info on md5 seems pretty vague. i'll look deeper into it.
Copy linkTweet thisAlerts:
@wyclefauthorNov 27.2005 β€”Β Could someone direct me to an example similar to the one sheldon posted but that starts off explaining how to set up the mysql database?
Copy linkTweet thisAlerts:
@SheldonNov 27.2005 β€”Β Does your Host have a control/admin panel for your hosting account? Do you have PHPmyAdmin istalled?

Otherwise

http://dev.mysql.com/doc/refman/5.1/en/create-database.html

is the mySQL dev centre.
Copy linkTweet thisAlerts:
@wyclefauthorNov 28.2005 β€”Β yea, i have PHPmyAdmin although i've never used it.
Copy linkTweet thisAlerts:
@SheldonNov 28.2005 β€”Β Well you can set up a databse with that, assign a user with all privlages to your new database, then the ball is rolling faster than ever, with NogDog's article your cant go wrong with a decent PHP/mySL login script!

Fantastic!




(Why am i in such a good mood?)
Copy linkTweet thisAlerts:
@sofiocoSep 02.2009 β€”Β I need something just like this, but using MySQL... Can someone help me?

Thanks
Copy linkTweet thisAlerts:
@anoop0118Feb 03.2015 β€”Β Ok, i'm looking into the Db stuff. In the meantime i'd also like to figure out this other weaker method and I have something that is working now however I was wondering if someone could figure out how to modify this to implement md5 encrypted passwords?

[code=php]
<?php
session_start();

$data=array("user1"=>array("url"=>"file1.php","password"=>"pass1"),
"user2"=>array("url"=>"file2.php","password"=>"pass2"));

if(isset($_POST['username']) && isset($_POST['password'])) {
if($data[$_POST['username']]['password'] == $_POST['password']) {
$_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
header('Location: ' . $data[$_POST['username']]['url']);
} else {
login('Wrong user name or password. <br>');
}
} else {
login();
}
?><?php

function login($response='This is the default message, if there is none provided') {

?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1>Login</h1>
<p><?=$response?></p>
<form action="" method="post">
<label for="username">User Id</label>
<input name="username" type="text" /><br />

<label for="password">Password</label>
<input name="password" type="password"><br />
<input type="submit" value="Login" />
</form>
</body>
</html>
<?php } ?>
[/code]
[/QUOTE]


How to use session in this code
Copy linkTweet thisAlerts:
@JedijonSep 06.2018 β€”Β <?php

session_start();

$data=array("user1"=>array("url"=>"file1.php","password"=>"pass1"),

"user2"=>array("url"=>"file2.php","password"=>"pass2"));

if(isset($_POST['username']) && isset($_POST['password'])) {

if($data[$_POST['username']]['password'] == $_POST['password']) {

$_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];

header('Location: ' . $data[$_
POST['username']]['url']);

} else {

login('Wrong user name or password. <br>');

}

} else {

login();

}

?><?php

function login($response='This is the default message, if there is none provided') {

?>

<html>

<head>

<title>Basic Login</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<h1>Login</h1>

<p><?=$response?></p>

<form action="" method="post">

<label for="username">User Id</label>

<input name="username" type="text" /><br />

<label for="password">Password</label>
<input name="password" type="password"><br />
<input type="submit" value="Login" />

</form>

</body>

</html>

<?php } ?>

How to use session in this code
Copy linkTweet thisAlerts:
@rootSep 07.2018 β€”Β {"locked":true}
Γ—

Success!

Help @wyclef spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 4.20,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...