Forum Moderators: open
The website i am creating is for a conference my school is hosting. What I want to do is give the username and password for each delegation to use; once they log in, they will be directed to a page which contains information pertaining only to their delegation.
Right now, this is the script I have, but it only directs to one page, which is "user1.php". I want to be able to direct user2 to user2.php, user3 to user3.php and so on.
<?php
$usernames = array("user1", "user2", "user3", "superman");
$passwords = array("pass1", "pass2", "password3", "supermans password");
$page = "user1.php"
for($i=0;$i<count($usernames);$i++){
$logindata[$usernames[$i]]=$passwords[$i];
}
if($logindata[$_POST["username"]]==$_POST["password"]){
session_start();
$_SESSION["username"]=$_POST["username"];
header('Location: '.$page);
exit;
}else{
header('Location: login.php?wrong=1');
exit;
}
?>
Any help would be greatly appreciated. Thanks!
The "standard" way to go about it is to store the info in a database:
id¦delegation_id¦password¦content
123¦3456¦abcdef¦this is the content for delegation 1
124¦5656¦ghijkl¦this is the content for delegation 2 ....
You'd do the login validation and get the content, and if the login validates, display only content for that user.
header("content-type:text/html\n\n");
// mysql connect, get $row data based on input
if(
($_POST["userid"]==$row["delegation_id"]]) and
$_POST["password"]==$row["password"]])
) {
session_start();
echo $row['content'];
}
else {
echo "Login failed";
// add form here so they can try again
}
If you don't use a database, you can store the delegation data in files and create an array associating files with users:
<?php
$users = array(
"user1" => "pass1",
"user2" = > "pass2",
"user3" => "pass3"
);
$files = array(
"user1" => "file1.txt",
"user2" = > "file2.txt",
"user3" => "file3.txt"
);
// a single multidimensional array here would work too
header("content-type:text/html\n\n");
if (
// Make sure "user1, user2..." is in the array
(array_key_exists($users[$_POST["username"]],$users)) and
($_POST["password"] == $users[$_POST["username"]])
) {
session_start();
$_SESSION["username"]=$_POST["username"];
$content = file_get_contents($files[$_POST["username"]]);
echo $content;
exit;
}
else{
$form='
<p>Login failed, try again</p>
<form><!-- put login form here --></form>
';
echo $form;
exit;
}
?>
There are more details, such as cleansing input, etc. . . . but this would work and to maintain it you would only have to add entries to the database or modify the arrays, which could be pared off into a config file or something.