+ Reply to Thread
Page 1 of 5 123 ... LastLast
Results 1 to 10 of 41

Thread: Simple Login/Register/Main Script

  1. #1
    Whitey's Avatar
    Whitey is offline Programming Professional
    Join Date
    Feb 2008
    Location
    Loveland, Colorado
    Posts
    254
    Rep Power
    18

    Simple Login/Register/Main Script

    This was made probably cheaply but i learned alot while making this and it could possibly help people who are starting out in PHP.

    config.php
    Code:
    <?php
    class MySQLDB
    {
       var 
    $connection;         //The MySQL database connection

       /* Class constructor */
       
    function MySQLDB(){
          
    /* Make connection to database */
          
    $this->connection = @mysql_connect(localhostwhiteyabc123) or die(mysql_error());
          @
    mysql_select_db(blue$this->connection) or die(mysql_error());
        }
        
    /**
        * query - Performs the given query on the database and
        * returns the result, which may be false, true or a
        * resource identifier.
        */
        //Use this function as query("Query line of code");
       
    function query($query){
          return 
    mysql_query($query$this->connection);
       }
    };

    $config = new MySQLDB;
    ?>
    login.php
    Code:
    <?php
    // Session Start is always needed when working with sessions.
    session_start();
    //Checks to see if the session error exist.. And if it does echo the error
    if(session_is_registered(error))
    {
    echo (
    $_SESSION['error']);
    session_unregister(error);
    session_destroy();
    }
    ?>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form name="form1" method="post" action="process.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
    <tr>
    <td colspan="4"><strong>Member Login </strong></td>
    </tr>
    <tr>
    <td width="68"><div align="right">Username</div></td>
    <td width="3">:</td>
    <td width="205"><input name="myusername" type="text" id="myusername"></td>
    <td width="205">
    <?php
    // Checks to see if the Session Bad_char exist which is another error type.
    if(session_is_registered(bad_char))
    {
    echo (
    $_SESSION['bad_char']);
    session_unregister(bad_char);
    session_destroy();
    }
    ?></td>
    </tr>
    <tr>
    <td><div align="right">Password</div></td>
    <td>:</td>
    <td><input name="mypassword" type="password" id="mypassword"></td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td><input name="login" type="hidden" value="1"></td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Login"></td>
    <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="4"><div align="center"><a href="register.php">Register Account? </a></div></td>
      </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    logout.php
    Code:
    <?
    //Just in case the page was viewed it does a session_start or it would release an error. 
    session_start();
    //Destroys all the sessions
    session_destroy();
    //Unregisters your login
    session_unregister(myusername);
    //Redirects you back to the login page after 5 seconds
    echo("<center><font size='4'>You are now logged out</font>");
    echo(
    "<br><a href='login.php'>Now redirecting you to home page or click here if you do not wish to wait.</a></center>");
    echo(
    "<META HTTP-EQUIV='refresh' CONTENT='5;login.php'>");

    ?>
    main.php
    Code:
    <?
    //Checks to see if you were logged in (if session myusername was registered or not)
    //Redirects back to login.php if you aren't logged in and tryed viewing this page.
    session_start();
    if(!
    session_is_registered(myusername)){
    header("location:login.php");
    }
    ?>

    <html>
    <body>
    <p>Login Successful<br>
      <a href="logout.php">Logout?</a></p>
    </body>
    </html>
    process.php
    Code:
    <?php
    include("config.php");
    class 
    Process
    {
        function 
    Process($connection){
            if(isset(
    $_POST['login'])){
            
    $this->login();
            }
            elseif(isset(
    $_POST['register'])){
            
    $this->register();
            }
            else{
            
    header("Location: login.php");
            }
        }
        
    //Member Login
        
    function login(){
        global 
    $config;
            
    ob_start();
            
            
    // Define $myusername and $mypassword
            
    $myusername=$_POST['myusername'];
            
    $mypassword=$_POST['mypassword'];
            
            
    # Allows letters, numbers
            
    if(!preg_match('/^[a-zA-Z0-9]+$/i'$myusername)) 
            {
            
    session_register(bad_char);
            
    $_SESSION['bad_char'] = "<center><font color='red' size='1'>Invalid Charcter; Only Letters Or Numbers Can Be Used!</font></center>";
            
    header("location:login.php");
            }

            
    // To protect MySQL injection (more detail about MySQL injection)
            
    $myusername stripslashes($myusername);
            
    $mypassword stripslashes($mypassword);
            
    $myusername mysql_real_escape_string($myusername);
            
    $mypassword mysql_real_escape_string($mypassword);
            
    $encrypt_password md5($mypassword);
            
    $query $config->query("SELECT * FROM members WHERE username='".$myusername."' and password='".$encrypt_password."'");
            
            
    // Mysql_num_row is counting table row
            
    $count=mysql_num_rows($query);
            
    // If result matched $myusername and $mypassword, table row must be 1 row
            
            
    if($count==1){
            
    // Register $myusername, $mypassword and redirect to file "login_success.php"
            
    session_register("myusername");
            
    header("location:main.php");
            }
            else {
            
    session_register(error);
            
    $_SESSION['error'] = "<center><font color='red' size='4'>Wrong Username or Password</font></center>";
            
    header("location:login.php");
            }
            
            
    ob_end_flush();
        }
        
        
    //Register_Submit
        
    function register(){
        global 
    $config;
        
            
    //Defines All The Users Inputs
            
    $myusername=$_POST['myusername'];
            
    $myusername2=$_POST['myusername'];
            
    $mypassword=$_POST['mypassword'];    
            
    $mypassword2=$_POST['mypassword2'];
            
    $email=$_POST['email'];
            
    $passwordcount=$_POST['mypassword'];
            
            
    # Allows letters, numbers
            
    if(!preg_match('/^[a-zA-Z0-9]+$/i'$myusername2)) 
            {
            
    session_register(bad_char);
            
    $_SESSION['bad_char'] = "<center><font color='red' size='1'>Invalid Charcter; Only Letters Or Numbers Can Be Used!</font></center>";
            
    header("location:register.php");
            }
            
            
            
    //Stop SQL Injection
            
    $myusername stripslashes($myusername);
            
    $mypassword stripslashes($mypassword);
            
    $mypassword2 stripslashes($mypassword2);
            
    $email stripslashes($email);
            
    $myusername mysql_real_escape_string($myusername);
            
    $mypassword mysql_real_escape_string($mypassword);
            
    $mypassword2 mysql_real_escape_string($mypassword2);
            
    $email mysql_real_escape_string($email);
            
            
    //encrypt password variable
            
    $encrypt_password md5($mypassword);
            
            
    $query $config->query("SELECT * FROM members WHERE username='".$myusername."'");
            
            
    // Mysql_num_row is counting table row
            
    $count=mysql_num_rows($query);
            
    // If result matches $myusername then username is taken
            
            
    if($count===1){
            
    // Send error back to the register page if count = 1
            
    session_register(username_taken);
            
    $_SESSION['username_taken'] = "<center><font color='red' size='1'>The Username You Chose Is Already In Use</font></center>";
            
    header("location:register.php");
            }    
            elseif(
    $mypassword != $mypassword2)
            {
            
    session_register(password_same);
            
    $_SESSION['password_same'] = "<center><font color='red' size='1'>Passwords Dont Match</font></center>";
            
    header("location:register.php");
            }
            elseif(
    strlen($mypassword) < "5")
            {
            
    session_register(password_less_then_5);
            
    $_SESSION['password_less_then_5'] = "<center><font color='red' size='1'>Password Must be Greater then 4 Charcters</font></center>";
            
    header("location:register.php");
            }
            else
            {
            
    $query $config->query("INSERT INTO members (id, username, password, email) VALUES (NULL, '$myusername', '$encrypt_password', '$email')");
            
    session_register(welcome_screen);
            
    $_SESSION['welcome'] = 
            
    "Welcome, You are now a member of Corpal Uploads.<br>
            Reccommend us to your friends.<br>
            We are a free Upload site and WILL STAY FREE!<br>
            Thanks,<br>
            Whitey.<br>
            <a href='login.php'>Continue</a>"
    ;
            
    header("location: register.php");
            }
        
        }
    };
    $process = new Process($connection);
    ?>
    register.php
    Code:
    <?php
    session_start
    ();
    if(
    session_is_registered(welcome))
    {
    echo(
    $_SESSION['welcome']);
    session_unregister(welcome);
    session_destroy();
    }
    else
    {
    ?>
    <table width="325" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form name="form2" method="post" action="process.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
    <tr>
    <td colspan="4"><strong>Member Register</strong></td>
    </tr>
    <tr>
    <td width="61" height="28"><div align="right">Username</div></td>
    <td width="3">:</td>
    <td width="144"><input name="myusername" type="text" id="myusername"></td>
    <td width="91">
    <?php
    if(session_is_registered(bad_char))
    {
    echo (
    $_SESSION['bad_char']);
    session_unregister(bad_char);
    session_destroy();
    }
    elseif(
    session_is_registered(username_taken))
    {
    echo (
    $_SESSION['username_taken']);
    session_unregister(username_taken);
    session_destroy();
    }
    ?></td>
    </tr>
    <tr>
    <td><div align="right">Password</div></td>
    <td>:</td>
    <td><input name="mypassword" type="password" id="mypassword"></td>
    <td rowspan="2">
    <?php
    if(session_is_registered(password_same))
    {
    echo (
    $_SESSION['password_same']);
    session_unregister(password_same);
    session_destroy();
    }
    elseif(
    session_is_registered(password_less_then_5))
    {
    echo (
    $_SESSION['password_less_then_5']);
    session_unregister(password_less_then_5);
    session_destroy();
    }
    ?></td>
    </tr>
    <tr>
      <td><div align="right">Password</div></td>
      <td>:</td>
      <td><input name="mypassword2" type="password" id="mypassword2"></td>
      </tr>
    <tr>
      <td><div align="right">Email</div></td>
      <td>:</td>
      <td><input name="email" type="text" id="email" /></td>
      <td>
      <font color="#FF0000" size="2">Optional</font> </td>
    </tr>
    <tr>
      <td><input name="register" type="hidden" value="1" /></td>
      <td>&nbsp;</td>
      <td colspan="2"><input type="submit" name="Submit" value="Login" /></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    <?php
    }
    ?>
    If you have any questions let me know. I will explain anything else if you need
    Attached Files Attached Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    ReekenX's Avatar
    ReekenX is offline Programmer
    Join Date
    Jan 2007
    Location
    Lithuania
    Posts
    135
    Rep Power
    0

    Re: Simple Login/Register/Main Script

    I think it's good idea, Whitey. Maybe this your tutorial will help peoples, who just have started PHP. 6 years ago, I was one of them...

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Simple Login/Register/Main Script

    Very useful, Whitey! +rep

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #4
    Jordan Guest

    Re: Simple Login/Register/Main Script

    For people just starting this code would come in very handy. Nice job +rep

  6. #5
    Whitey's Avatar
    Whitey is offline Programming Professional
    Join Date
    Feb 2008
    Location
    Loveland, Colorado
    Posts
    254
    Rep Power
    18

    Re: Simple Login/Register/Main Script

    Thanks for the reputation guy

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Simple Login/Register/Main Script

    Guys.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Simple Login/Register/Main Script

    Very nice! There isn't much I can recommend.

  9. #8
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: Simple Login/Register/Main Script

    So using this, I can display something to non-regged users and something else to regged and logged in users... right?

  10. #9
    Whitey's Avatar
    Whitey is offline Programming Professional
    Join Date
    Feb 2008
    Location
    Loveland, Colorado
    Posts
    254
    Rep Power
    18

    Re: Simple Login/Register/Main Script

    yes you can do this
    Code:
    if(!session_is_registered(myusername)){
    echo "Display to non member";
    } 
    else
    {
    echo "Display to Member";
    }

  11. #10
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: Simple Login/Register/Main Script

    Thanks very much

    I might need this sometime for my website.

    +rep given!

+ Reply to Thread
Page 1 of 5 123 ... LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Login and Register Forms, using database
    By Zvone in forum Visual Basic Programming
    Replies: 2
    Last Post: 09-03-2011, 11:17 AM
  2. Beginner Simple Register-Login-Logoff System
    By Jaan in forum PHP Tutorials
    Replies: 76
    Last Post: 01-04-2011, 08:25 PM
  3. Login Script - With MySql - Register
    By jthom263 in forum PHP Development
    Replies: 1
    Last Post: 05-08-2010, 04:10 AM
  4. Login/Register Script
    By Whitey in forum PHP Development
    Replies: 6
    Last Post: 06-07-2008, 10:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts