Login System

Does anyone know how to make a simple login/out system with mysql? I have little experience with PHP. Any outside links to information would be helpful.

I have quickly thrown together a login system for you to build on.
This code will display two boxes (username & password) and a submit button.
Upon submitting the form it will send the data to itself - see the action in the form element.

From here, i have left you with a ‘project’ to complete. You should decide whether the stored values are kept in a textfile or database (database is far better).

Once you have filled in the blanks, the script will check the data matches that stored away and if so will set a session that you can call on to check if user is indeed logged in before displaying anything.
If the user is not logged in, the script will redirect the user to the login page - IE this one.

Hope that gets you on your way,
Red :wink:

login.php
[php]<?php
if(isset($_POST[‘submit’])) {
// do some checking here like database or textfile…
// if the values match, log the user in, if they don’t then redirect.

if($_POST['uname'] == $username && $_POST['pword'] == $password) {
  $_SESSION['logged_in'] == true;
  $_SESSION['username'] == $username;
}
else {
   // details don't match!
   // Redirect the user to login page..
   $url = '/login.php';
   header("Location: $url");
   exit();
}

}
else {
?>

Username Password <?php } ?>[/php]

alright man. Thanks. This should get me started. :smiley:

No worries, glad i could help.
If you get stumped, give us a shout i’ll help you on your way. :wink:

Important!!

The snippet of code i wrote was off the top of my head and re-reading it today i spotted a couple of typos - important ones too! :o

These lines:
[php]$_SESSION[‘logged_in’] == true;
$_SESSION[‘username’] == $username;[/php]

Should be:
[php]$_SESSION[‘logged_in’] = true; // single ‘=’ not double! We are assigning, not checking the value!
$_SESSION[‘username’] = $username; // single ‘=’ not double! We are assigning, not checking the value!
[/php]

Sorry if it caused you any confusion :-[
Red :wink:

aight thanx man.
i want to ask something though on part of your code:

    <label for="uname">Username</label>
       <input type="text" name="uname" value="">
    <label for="pword">Password</label>
       <input type="password" name="pword">
       <input type="submit" name="submit" value"Login">

Why is it that the “uname” input has no value while the “pword” input has no value to begin with? Also why does the submit button suddenly get a value of “login”?
:o
thanks in advanced. 8)

nvr mind i got it now.

Sorry for delay in replying, been a bit busy…

Let’s do this one at a time…

This:

The value is empty because, well it’s empty as the user has not typed anything yet.
You can put a default value in like so value=“enter username” but this will be ever present unless you use jquery/javascript etc. to remove the value onFocus or leave it to the user to delete. (bad idea!) so the user can type in the box.
Missing it from the password was an oversight on my part, my bad!

This:

This value is simply the label on the button.

Hope that helps :wink:

Hi,

I know a good tutorial I used myself:

It required PHP, jQuery and MySQL

@Redscouse thanks.
@Isaiah thanks, im checking it out right now.

btw Does anyone know what type of database for mysql i should use? Would “utf8_bin” work for a user system?

Sponsor our Newsletter | Privacy Policy | Terms of Service