Page 1 of 1

I Need help making a login page

Posted: Wed Dec 22, 2004 7:57 pm
by Chastity
I need help making a php login page.

Could someone help me and walk me through it?

I would really appreciate it!

Posted: Wed Dec 22, 2004 8:07 pm
by Todd_Z
Well first you need to decide what session variables you are going to keep track of... this is very dependent of what your site's content is. Then you need to use the session_start() function... then you need to make a database of all the users, then make a form which registers users - usually with a username and a password... Thats the start... you should include more info about your site for more help.

Posted: Wed Dec 22, 2004 8:34 pm
by Chastity
well, I'm creating my own sim game.
so,
I first need to figure out what session variables,
then I need to create a db for the members,
then make a form for the registered users to login.

What are session variables, and how do I decide which I should use?

Posted: Wed Dec 22, 2004 8:49 pm
by John Cartwright
session variables are variables that are stored as long as the browser is open. A login script is generally done like this. Althought this is a simplified version.

Code: Select all

<?php
//initialize your session, generally has to be on first line (or before headers are sent
session_start();
//search your database to see if you have matched the input from the form to your db
//this is assumming your login form has the input fields named user and pass
$sql = "SELECT * FROM `tablename` WHERE `user` = '".$_POSTї'user']."' AND `pass` = '".$_POSTї'pass']."' LIMIT 1";
//run the search (and check for mysql errors)
$result = mysql_query($sql) or die(mysql_error());
//check to see if any results are returned
if (mysql_num_rows($result) == 1)
{
//create the session logged in 
$_SESSIONї'loggedin'] = TRUE;
//send them to members page.. or main page? whatever u want
header("Location: membersonly.php");
}
else
{
//spit out error message
echo 'incorrect username or password';
//display form here
}
?>

Code: Select all

<?php

//initalize session
session_start();
//check to see if they are logged in
if ($_SESSIONї'loggedin'])
{
//show this page's content here
}
else
{
echo 'you do not have access to this page';
//or
//header("Location: mainpage.php");
}
?>
that is it in a nutshell. search these forums for login validation and i'm sure you'll find several other examples.