Page 1 of 1

How to write a login page?

Posted: Fri Jun 23, 2006 1:33 pm
by bluelad
How to create a login page which once authenticated takes the user to another page.

Posted: Fri Jun 23, 2006 1:36 pm
by Luke
Multi-user or just one?

Posted: Fri Jun 23, 2006 1:40 pm
by bluelad
I think one will do for now

Posted: Fri Jun 23, 2006 1:42 pm
by Luke
If you only have one user, you don't even need a database... it's a good idea to use one anyway (for a number of reasons). Are you interested in making a database auth system or is that beyond the scope of your needs?

Posted: Fri Jun 23, 2006 1:47 pm
by bluelad
I only want only a few people who know the password to access the page. So i req only one username and password. It really has to very simple. I dont req too complicated codes. i searched on google, but those codes were a little too heavy for me. I want a simple code.

Posted: Fri Jun 23, 2006 1:59 pm
by Luke
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

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;
}
Every other page:

Code: Select all

require('authenticate.php');

if($authenticated){
    //Show 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?

Posted: Fri Jun 23, 2006 2:13 pm
by bluelad
This is what i ahve done

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>
But it is showing this error

Code: Select all

Parse error: parse error in d:\program files\easyphp1-8\www\link\admin_login.php on line 11
The line in asteriks is line no 11

Posted: Fri Jun 23, 2006 2:15 pm
by Luke
bluelad wrote:This is what i ahve done

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>
But it is showing this error

Code: Select all

Parse error: parse error in d:\program files\easyphp1-8\www\link\admin_login.php on line 11
The line in asteriks is line no 11
That should do it

Posted: Fri Jun 23, 2006 2:18 pm
by bluelad
but it is still showing that eroor :!:

Posted: Fri Jun 23, 2006 2:34 pm
by bluelad
One more query...if i want to access the particular page i should use this code

Code: Select all

require('authenticate.php');

if($authenticated){
    //Show page
}
What does //show page mean over here

Posted: Fri Jun 23, 2006 2:39 pm
by Luke
bluelad wrote:One more query...if i want to access the particular page i should use this code

Code: Select all

require('authenticate.php');

if($authenticated){
    //Show page
}
What does //show page mean over here
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.

Posted: Fri Jun 23, 2006 4:33 pm
by RobertGonzalez
Shouldn't this thread be in PHP code? Just a thought.

Posted: Fri Jun 23, 2006 4:36 pm
by Luke
I suppose it COULD be a design question, probably could have gone either way.