where would u go for a simple login form tutorial
This is a discussion on simple php login form within the Coding Help forums, part of the Webmaster Discussion category; where would u go for a simple login form tutorial...
where would u go for a simple login form tutorial
personally I wouldn't mind going to cuba for one .. but I'm not sure they'd have one there
How simple do you want ?
Does it pull from a database or it it simply to hide some in page info ...
Could you use .htaccess for the pages which might be simpler
IT Books.ie | Computer Books
Recent Work | The Dropped.IE Domain List | Upgrade.ie | Children's Festivals in Ireland | Galway Hot Yoga | Freelance Jobs African Safari Holiday
Blog | James Larkin Work | Web Design Dublin, Ireland - Web Development Dublin, Ireland
How to Build a Full-Featured Login System: New PLUS Tutorial - Nettuts+ would probably be good to get you up to speed on it all![]()
IT Books.ie | Computer Books
Recent Work | The Dropped.IE Domain List | Upgrade.ie | Children's Festivals in Ireland | Galway Hot Yoga | Freelance Jobs African Safari Holiday
Blog | James Larkin Work | Web Design Dublin, Ireland - Web Development Dublin, Ireland
Hi,
In my opinion, you should go right here for a login form tutorial!
<?php
// Use this at the top of every page you want to retain the user's login state
session_start();
// This function returns true (and stores some info in the session) if we have a valid user and returns false if not.
function validUser($username,$password)
{
// Connect to your database (replace hostname, username, password, dbname with your own database details)
mysql_connect('hostname','username','password') or die ('Can\'t connect to database server');
mysql_select_db('dbname') or die ('Can\'t use database');
// Escape nasty characters to preven sql injection (never trust user input)
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// You will have had to set up a user table in the database
// Change the table, Username and Password to suit your database structure
$query = "SELECT COUNT(*) AS count FROM table WHERE Username='" . $username . "' AND Password='" . $password . "'";
$result = mysql_query($query);
// If we have found one row, the user is a valid user
if(mysql_num_rows($result) == 1){
// Store some data in the session for use later if you want
$_SESSION['loggedIn'] = 1;
$_SESSION['username'] = $username;
return TRUE;
// If there are zero or more than 1 rows the user is not a valid user.
// You will have to prevent the same user creating multiple accounts with the same details in your registration form code
} else {
return FALSE;
}
}
// If the login form was submitted...
if(isset($_POST['login'])){
// If we don't have a valid user, display an error message
if(!validUser($_POST['username'],$_POST['password'])){
echo '<p>You did not provide valid login details</p>';
// If we do have a valid user, display a welcome message
} else {
echo '<p>Thanks, you are now looged in!</p>';
}
}
?>
<form name="login" method="post" action="">
<p><label>Username:</label><input type="text" name="username" id="username" /></p>
<p><label>Password:</label><input type="text" name="password" id="password" /></p>
<p><input type="hidden" name="login" value="1" /><input type="submit" value="Login" /></p>
</form>
Copy all that code above into your code editor so you can read it easier with syntax highlighting, I still haven't figured out how to display php code with syntax highlighting in this forum!
Make sure you use session_start(); at the top of all pages where you want to check for logged in users with a bit of extra code...
<?php
session_start();
if($_SESSION['loggedIn'] != 1){
echo 'You must be logged in to view this page';
exit();
}
?>
These are also worth a look:
http://evolt.org/article/comment/17/60265/index.html
http://www.evolt.org/PHP-Login-Syste...Admin-Features
Rgds