Tuesday, October 25, 2011

Page Redirection Using PHP updated

Earlier, I had asked for the entry level class of PHP students to create a Login Form and if the password is correct the user should be redirected to a members page or else the users should be redirected to the home page.  Generally we compare the login input from the user to the actual stored username and password in the database for authentication.  However, in this particular section, I'm going to hard code the password through a variable for learning purposes only.  The new thing to learn for beginners is how the form and the results can be  coded on the same page, using $_SERVER[ 'SCRIPT_NAME ' ] system global.  Here's the code for this below:


<?php

if(isset($_POST['submit'])){
$authorized = "abcotechnology";
//if statement to check the password
if($_POST['password']==$authorized){
header('Location: ProcessFileUpload.php');
} else {
header('Location: index.php');
}

}else { //else of post-submit

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login Form Assignment</title>
</head>

<body>
<h2> Login Page </h2>
<form name="frm_login" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table width="200" border="0" cellpadding="0" cellspacing="0">
    <tr>
  <td align="right"><strong> Username:</strong></td>      
        <td align="left"><input type="text" name="username" size="35" /></td>
        </tr>
            <tr>
  <td align="right"><strong> Password:</strong></td>      
        <td align="left"><input type="password" name="password" size="35" /></td>
        </tr>
       
        <tr>
        <td colspan="2" align="center"><input type="submit" name="submit" value="Login" />
        </tr>
    </table>
</form>
</body>
</html>
<?php
}
?>

2 comments:

ABCO Technology Los Angeles said...

Feel free to comment or share your thoughts on the code below, or you can just send an appreciation to the author :)

David Flores said...

Great post, keep up the good work. I spend a lot of time searching for simple scripts so as to brush up my skills as a coder. Tedious and sometimes stressful i have to admit. Blogs like this are a huge help. Thanks ABCO...