Page 1 of 1

PHP simple login help

Posted: Tue Mar 21, 2006 1:10 am
by Bonzol
Hey there,

Well, I want to make a simple login page (where differnt usernames go to differnt URL's), where the login details are just in a php page,, since I will only ever have 4 user names and 4 passwords, I don't require an SQL database.

I got this login form

Code: Select all

<html>
<head>
<title>login.html</title>
</head>
<body>
<form name="login" method="post" action="validate.php">
username:<input type="text" name="user_name"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Now I assume I just create in the validate.php page a statment of sorts which can redirect each login to a differnt url

I've really only ever programed basic java, so I have no idea about the php syntax,, I got some info from around the place and came up with this (its 100% wrong I know, but this is what I got,, )

Code: Select all

<?php 

if($password == $_POST['pass'] && $user_name == $_POST['user']){ 
  header('Location: page.php?user=\'.' $user_name ); 
} 
{ 
else{ 
  $error = '1'; 
}
variable names I think are incorrect in the validation form

I would very my appreciate it if someone told me what I put in that validation page, since I have no idea. for example sake (to make me understand easier),, lets say the specific user name "bob" and password "milk" redirect to, "www.google.com"


Thanx in Advanced,

Regards,
Chris

Posted: Tue Mar 21, 2006 1:16 am
by feyd
the quick and dirty version

Code: Select all

if('bob' == $_POST['user_name'] and 'milk' == $_POST['password'])
{
  header('Location: http://www.google.com');
}
elseif('larry' == $_POST['user_name'] and 'juice' == $_POST['password'])
{
  header('Location: http://forums.devnetwork.net');
}
exit;
remember to use a full URL when doing a header redirection.

Posted: Tue Mar 21, 2006 2:26 am
by Bonzol
thanx heaps

soooo,, is that all I have to do, is that the exact code? (im a total noob, I just need 1 example of the exact code,everything that needs to be in the php page then ill understand, sorry to be a pain) I ain't tried it yet, will try it tonight..

I'd also like it if the page im directing to (a page I make) to only be accessible by this login? Any help?

Posted: Tue Mar 21, 2006 4:03 am
by sosha
Create a session variable that you will be checking at the start of this page. If its not true re-direct to the login page

Posted: Tue Mar 21, 2006 4:52 am
by Maugrim_The_Reaper
Set something like

Code: Select all

session_start(); // only if not already started
$_SESSION['authenticated'] = TRUE;
after validation and authentication. Then on subsequent pages where a login is required check the value is indeed true.

Code: Select all

session_start(); // only if not already started
if(!$_SESSION['authenticated'])
{
    header('Location: http://myserver/index.php');
}

// else do normal page stuff for authenticated users...