I Need help making a login page

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
Chastity
Forum Newbie
Posts: 2
Joined: Wed Dec 22, 2004 7:56 pm

I Need help making a login page

Post by Chastity »

I need help making a php login page.

Could someone help me and walk me through it?

I would really appreciate it!
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
Chastity
Forum Newbie
Posts: 2
Joined: Wed Dec 22, 2004 7:56 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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