How to write a login page?
Moderator: General Moderators
How to write a login page?
How to create a login page which once authenticated takes the user to another page.
Include a script (which contains your expected username and password and hash the password with sha1() or something if you want it fairly secure) then test against it
Include a script (similar to this): authenticate.php
Every other page:
Now, I wouldn't trust any type of information I needed REAL secure behind this system, but if you're just trying to keep the average joe out, it works I guess.
Does that make sense?
Include a script (similar to this): authenticate.php
Code: Select all
$authenticated = false; // Set authenticate default to false
$expected_username = "bob";
$expected_password = "1234!@#$";
if(!empty($_POST['username'])) $_SESSION['username'] = $_POST['username']; // Turn posted variables into session variables
if(!empty($_POST['password'])) $_SESSION['password'] = $_POST['password'];
if($_SESSION['username'] == $expected_username) && ($_SESSION['password'] == $expected_password){
$authenticated = true;
}Code: Select all
require('authenticate.php');
if($authenticated){
//Show page
}Does that make sense?
This is what i ahve done
But it is showing this error
The line in asteriks is line no 11
Code: Select all
<?php
$authenticated = false; // Set authenticate default to false
$expected_username = "admin";
$expected_password = "admin";
if(!empty($_POST['username'])) $_SESSION['username'] = $_POST['username']; // Turn posted variables into session variables
if(!empty($_POST['password'])) $_SESSION['password'] = $_POST['password'];
*if($_SESSION['username'] == $expected_username) && ($_SESSION['password'] == $expected_password)*
{
$authenticated = true;
}
?>
<table>
<tr>
<td>User</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password"></td>
</tr>
</table>Code: Select all
Parse error: parse error in d:\program files\easyphp1-8\www\link\admin_login.php on line 11That should do itbluelad wrote:This is what i ahve doneBut it is showing this errorCode: Select all
<?php $authenticated = false; // Set authenticate default to false $expected_username = "admin"; $expected_password = "admin"; if(!empty($_POST['username'])) $_SESSION['username'] = $_POST['username']; // Turn posted variables into session variables if(!empty($_POST['password'])) $_SESSION['password'] = $_POST['password']; if(($_SESSION['username'] == $expected_username) && ($_SESSION['password'] == $expected_password)) { $authenticated = true; } ?> <table> <tr> <td>User</td> <td><input type="text" name="username"></td> </tr> <tr> <td>Password</td> <td><input type="text" name="password"></td> </tr> </table>The line in asteriks is line no 11Code: Select all
Parse error: parse error in d:\program files\easyphp1-8\www\link\admin_login.php on line 11
One more query...if i want to access the particular page i should use this code
What does //show page mean over here
Code: Select all
require('authenticate.php');
if($authenticated){
//Show page
}You can use // for comments in php scripts... it was simply put there as an instruction. You put the page contents where //Show page is.bluelad wrote:One more query...if i want to access the particular page i should use this codeWhat does //show page mean over hereCode: Select all
require('authenticate.php'); if($authenticated){ //Show page }
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA