PHP simple login help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bonzol
Forum Newbie
Posts: 10
Joined: Mon Mar 20, 2006 8:44 pm

PHP simple login help

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Bonzol
Forum Newbie
Posts: 10
Joined: Mon Mar 20, 2006 8:44 pm

Post 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?
sosha
Forum Newbie
Posts: 5
Joined: Tue Mar 21, 2006 1:52 am

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...
Post Reply