$_SERVER[] Command and Page Linking
Posted: Tue May 26, 2009 10:33 am
I am trying to make a login page which will redirect to another page once the login is successful. I am keeping things very basic for now with a hardcoded username and password. I really don't understand what the $_SERVER[] (lines 42 and 50) command is exactly (even after reviewing the php documentation and googling) and how I can get my page to link to another page.
Basically what I want to do is enter the correct username and password and link to a main menu-like page. I am not very familiar with php, but I have a feeling my problem is something to do with the $_SERVER command.
Here is my code:
Right now, when I enter the correct username and password, I get redirected to a blank page. You can check out the page here.
Thanks for your patience!
Basically what I want to do is enter the correct username and password and link to a main menu-like page. I am not very familiar with php, but I have a feeling my problem is something to do with the $_SERVER command.
Here is my code:
Code: Select all
<!-- Original code from http://www.tutorialtastic.co.uk/tutorial/creating_a_secure_php_login_page with permission-->
<?php
$username = "user";
$password = "pass";
$randomword = "randomWord";
//Checking cookie. If it is verified, we grab the password provided by the user and use the md5 method (encryption) on the password
//and random word. This value will be compared to the password value in the database.
if (isset($_COOKIE['MyLoginPage'])) {
if ($_COOKIE['MyLoginPage'] == md5($password.$randomword)) {
?>
<!--CONTENT HERE-->
<html>
<head>
<title>Login</title>
</head>
<body>
</body>
</html>
<?php
exit;
} else {
echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Bad cookie. Please clear your cookies and try
again.</b></p></center>";
exit;
}
}
if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST['user'] != $username) {
echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Failed to find username. Click the back button
to try again.</b></p></center>";
exit;
} else if ($_POST['pass'] != $password) {
echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Failed to match password. Click the back button
to try again.</b></p></center>";
exit;
} else if ($_POST['user'] == $username && $_POST['pass'] == $password) {
setcookie('MyLoginPage', md5($_POST['pass'].$randomword));
header("Location: $_SERVER[PHP_SELF]");
} else {
echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Could not login at this time. Refresh the page
and try again.</b></p></center>";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
<h1><center><font face="Trebuchet MS">Login</font></center></h1>
<hr>
<br>
<center><font face="Arial"><b>Username: </b></font><input type="user" size="15" name="user"></center><br>
<center><font face="Arial"><b>Password: </b></font><input type="password" size="15" name="pass"></center>
<br>
<br>
<center><input type="submit" id="submit" value="Login"></center>
<br>
<hr>
<p align="right"><font face="Arial">Page Information © 2009</font></p>
</form>
</div>Thanks for your patience!