Thread: Making a basic login system with PHP & MySQL

Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1. #1 Making a basic login system with PHP & MySQL 
    Website Designer
    TheXenon's Avatar
    Join Date
    Sep 2013
    Posts
    121
    Thanks given
    10
    Thanks received
    23
    Rep Power
    48
    Hello guys, I'm here today to release a little tutorial on how to create a simple login system via PHP & MySQL.


    - Step 1
    Create a new MySQL database.

    - Step 2
    Create a new table called 'members'

    - Step 3
    Add the following columns in order :
    id
    username
    password

    - Step 4
    Go to your File Manager or FTP Client.

    - Step 5
    Create four new files called the following :
    account.php
    index.php
    xenon.php
    logout.php

    - Step 6
    Open 'account.php' and add the following code :
    Code:
    <?php
    // Starts Session
    session_start();
    // Checks if they are logged in or not.
    if (isset($_SESSION['id'])) {
        // Put stored session variables into local PHP variable
        $uid = $_SESSION['id'];
        $usname = $_SESSION['username'];
        // What should it say if they are logged in? 
        $result = "My Account Details:<br /> Id: ".$uid. <br /> Username: ".$usname;
    } else {
        // What should it say if they are logged out?
        $result = "You are not logged in yet";
    }
    ?>
    <!-- Created by TheXenon from Rune-Server.org --!>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <!-- Title of Web Page --!>
    <title><?php echo $usname ;?> - MyAccount</title>
    </head>
     
    <body>
    <!-- show results (if logged in or logged out) --!>
    <?php
    echo $result;
    ?>
    </body>
    </html>
    <!-- Created by TheXenon from Rune-Server.org --!>
    - Step 7
    Open 'index.php' and insert the following code :
    Code:
    <?php
    session_start();
     
    if ($_POST['username']) {
            
            // Include the databas connection script
    	include_once("xenon.php");
    	
    	// Set the posted data from the form into local variables
            $usname = strip_tags($_POST['username']);
    	$paswd = strip_tags($_POST['password']);
    	
    	$usname = mysqli_real_escape_string($dbCon, $usname);
    	$paswd = mysqli_real_escape_string($dbCon, $paswd);
    	
    	$paswd = md5($paswd); // using md5 just for testing purposes
    	
    	$sql = "SELECT id, username, password FROM members WHERE username = '$usname'";
    	$query = mysqli_query($dbCon, $sql);
    	$row = mysqli_fetch_row($query);
    	$uid = $row[0];
    	$dbUsname = $row[1];
    	$dbPassword = $row[2];
    	
    	// Check if the username and the password they entered was correct
    	if ($usname == $dbUsname && $paswd == $dbPassword) {
    		// Set session 
    		$_SESSION['username'] = $usname;
    		$_SESSION['id'] = $uid;
    		// Now direct to users feed
                    // If login is correct goto account page
    		header("Location: account.php");
    	} else {
                    // Wrong password or username output
    		echo "<h2>Oops that username or password combination was incorrect.
    		<br /> Please try again.</h2>";
    	}
    	
    }
    ?>
     
    <!-- Made by TheXenon from Rune-Server.org --!>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <!-- WebPage Title --!>
    <title>The Xenon's PHP Login Tutorial</title>
    <style type="text/css">
    /* HTML Font */
    html {
    	font-family: Verdana, Geneva, sans-serif;
    }
    /* H1 CSS */
    h1 {
    	font-size: 24px;
    	text-align: center;
    }
    /* Wrapper CSS */
    #wrapper {
    	position: absolute;
    	width: 100%;
    	top: 30%;
    	margin-top: -50px;/* half of #content height*/
    }
    /* Form CSS */
    #form {
    	margin: auto;
    	width: 200px;
    	height: 100px;
    }
    </style>
    </head>
     
    <body>
    <div id="wrapper">
    <!-- Title --!>
    <h1>TheXenon's Tutorial Login</h1>
    <!-- Login Form --!>
    <form id="form" action="index.php" method="post" enctype="multipart/form-data">
    <!-- Username Text Box --!>
    Username: <input type="text" name="username" /> <br />
    <!-- Password Text Box --!>
    Password: <input type="password" name="password" /> <br />
    <!-- Login Button --!>
    <input type="submit" value="Login" name="Submit" />
    </form>
    </body>
    </html>
    <!-- Created by TheXenon from Rune-Server.org --!>
    - Step 8
    Open 'xenon.php' and insert the following code :
    Code:
    <?php
    $dbCon = mysqli_connect("localhost", "root", "yourpasshere", "members");
    ?>
    - Step 9
    Open 'logout.php' and insert the following code :
    Code:
    <?php
    session_start();
    session_destroy(); 
    if (isset($_SESSION['username'])) { 
        // Logged out text
        $msg = "You are now logged out";
    } else {
        // Error Logging out 
        $msg = "<h2>Could not log you out</h2>";
    } 
    ?> 
    <!-- Created by TheXenon from Rune-Server.org --!>
    <html>
    <body>
    <?php echo $msg; ?><br>
    <!-- Where should we go? --!>
    <p><a href="/login-test">Click here</a> to return to our home page </p>
    </body>
    </html>
    <!-- Created by TheXenon from Rune-Server.org --!>
    Step 10 -
    Edit to your liking, you are finished!




    If you have any problems, feel free to PM me. I also will help work on your website with you
    for free, I'm on almost always. Thanks for reading
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Registered Member
    Morphine's Avatar
    Join Date
    Jul 2011
    Posts
    1,817
    Thanks given
    495
    Thanks received
    262
    Rep Power
    136
    good work, Xenon.
    Reply With Quote  
     

  4. #3  
    Website Designer
    TheXenon's Avatar
    Join Date
    Sep 2013
    Posts
    121
    Thanks given
    10
    Thanks received
    23
    Rep Power
    48
    Quote Originally Posted by Morphine View Post
    good work, Xenon.
    Thanks, Morphine I know this is basic but I hope to one
    day create more advanced guides for the community
    Reply With Quote  
     

  5. #4  
    Website Designer
    TheXenon's Avatar
    Join Date
    Sep 2013
    Posts
    121
    Thanks given
    10
    Thanks received
    23
    Rep Power
    48
    Registration page will be comming soon!
    Reply With Quote  
     

  6. #5  
    Registered Member
    Join Date
    Aug 2008
    Posts
    2,420
    Thanks given
    721
    Thanks received
    595
    Rep Power
    1220
    What's with you and red?
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  7. Thankful user:


  8. #6  
    Website Designer
    TheXenon's Avatar
    Join Date
    Sep 2013
    Posts
    121
    Thanks given
    10
    Thanks received
    23
    Rep Power
    48
    Quote Originally Posted by Bear Hunter View Post
    What's with you and red?
    lovely color
    Reply With Quote  
     

  9. #7  
    Registered Member viacom's Avatar
    Join Date
    Jul 2012
    Posts
    68
    Thanks given
    0
    Thanks received
    10
    Rep Power
    51
    this is 2013 pls use oop/pdo. kthx
    Reply With Quote  
     

  10. Thankful user:


  11. #8  
    Web Developer
    Leon's Avatar
    Join Date
    Jan 2012
    Posts
    392
    Thanks given
    64
    Thanks received
    123
    Rep Power
    38
    Quote Originally Posted by viacom View Post
    this is 2013 pls use oop/pdo. kthx
    Is it possible to make a thread anywhere on the internet about mysql without someone like you bitching about needing to use PDO?
    And even at that, he's using mysqli not mysql so I don't even know what you're bitching about.

    @OP, nice work.
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  12. #9  
    Website Designer
    TheXenon's Avatar
    Join Date
    Sep 2013
    Posts
    121
    Thanks given
    10
    Thanks received
    23
    Rep Power
    48
    Quote Originally Posted by Leon View Post
    Is it possible to make a thread anywhere on the internet about mysql without someone like you bitching about needing to use PDO?
    And even at that, he's using mysqli not mysql so I don't even know what you're bitching about.

    @OP, nice work.
    Thanks, I just realized I forgot about registration page. I'll try making some free time to update soon
    Reply With Quote  
     

  13. #10  
    Old Webdeveloper
    Dondxon's Avatar
    Join Date
    Aug 2011
    Posts
    1,147
    Thanks given
    209
    Thanks received
    108
    Rep Power
    65
    why dont you use [Only registered and activated users can see links. ]? its a way better.
    Do you need any PHP work done? Or are you looking for RSPS integrations? click [Only registered and activated users can see links. ].

    [Only registered and activated users can see links. ]

    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [VB: 05-08] Visual Basic Simple Login System
    By HyreScape in forum Application Development
    Replies: 7
    Last Post: 05-15-2010, 12:59 AM
  2. How to store passwords safely with PHP and MySQL
    By Defqon in forum Website Development
    Replies: 1
    Last Post: 03-27-2010, 07:56 PM
  3. Basic Login Script with page protecter
    By Miscbrah in forum Website Development
    Replies: 6
    Last Post: 05-24-2009, 02:11 PM
  4. Basic Login System
    By DJ Dan in forum Website Development
    Replies: 6
    Last Post: 02-18-2009, 03:12 PM
  5. PHP/MySQL Login System
    By Vegeta in forum Website Development
    Replies: 7
    Last Post: 06-23-2008, 07:22 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •