Why should I rely on PHPJabbers.com for all my PHP scripting needs? What do you offer me when buying some script?
  • Top quality scripts
  • Full demo before buying
  • Best pricing
  • Script customization
  • 7 days a week support
  • Installation support
  • Secure buying
  • Create user login in PHP

    posted on 2006-10-03 22:05:11
    Comments
    venkat posted on 2008-04-23 14:42:56
    i want php user login page

    Rob posted on 2008-04-21 21:54:50
    Going back to the post on 2008-02-08 21:45:08, is there an example that you can show us on how to query users and passwords from the MySQL database instead of using an array? Thanks!
    ---------------
    Veselin: this is a piece of code that I usually use to do it

    if ($ac=='logout') {
    $_SESSION["userID"] = '0';
    } elseif ($ac=='login') {
    $sql = "SELECT * FROM userstable WHERE username='".$_REQUEST["username"]."' AND password='".$_REQUEST["password"]."'";
    $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
    if (mysql_num_rows($sql_result)>0) {
    $USER = mysql_fetch_assoc($sql_result);
    $_SESSION["userID"] = $USER["id"];
    } else {
    $message = '<strong style="color:#FF0000">Incorrect login details.</strong>';
    $_SESSION["userID"] = "";
    unset($_SESSION["userID"]);
    };
    };

    Mary McGowan posted on 2008-04-16 18:03:56
    interested in login

    Rob posted on 2008-04-09 23:26:43
    THIS WORKED LIKE A CHARM!!! THANK YOU, THANK YOU, THANK YOU!! One question I have is though, is there a way to make the username and password not case sensitive?
    --------------
    Veselin: array_key_exists() function used is case sensitive. This is wht you should define all user names and passwords in lowercase. Then change login.php page instead of
    if ($USERS[$_POST["username"]]==$_POST["password"]) {
    $_SESSION["logged"]=$_POST["username"];
    it should be
    $tmpUser = strtolower($_POST["username"]);
    if ($USERS[$tmpUser]==strtolower($_POST["password"])) {
    $_SESSION["logged"]=strtolower($_POST["username"]);

    Spencer posted on 2008-03-17 19:08:19
    thank you, but on the logout code, do i make a link to it? and on the other post,do i put this on the passwords.php and login.php or just the users page (s)?

    $PAGE["username1"] = true
    $PAGE["username3"] = true;
    and then change check_logged() function to

    function check_logged(){
    global $_SESSION, $USERS;
    if (!array_key_exists($_SESSION["logged"],$USERS) OR !array_key_exists($_SESSION["logged"],$PAGE)) {
    header("Location: login.php");
    };
    };

    ------------
    Veselin: you need to create a new page logout.php and put the code in it. Then create logout link which points to this logout.php page

    You need to put the $PAGE array on pages which user can access. For each page you can allow the users that can access it by creating $PAGE array element with their username. And finally change the line in function in passwords.php with the one I sent you.

    spence posted on 2008-03-17 03:00:40
    okay i got everything to work but the logout page, explain please how to make it so users can logout
    ----------------------
    Veselin: logout code can be found in my post on 2008-02-09 08:12:39

    Spencer posted on 2008-03-16 23:25:45
    okay i can make everything,but how do i make it so only users can access surtain pages?
    ---------------
    Veselin: at the top of each page above check_logged(); you can define which users can access it using an array
    $PAGE["username1"] = true
    $PAGE["username3"] = true;
    and then change check_logged() function to

    function check_logged(){
    global $_SESSION, $USERS;
    if (!array_key_exists($_SESSION["logged"],$USERS) OR !array_key_exists($_SESSION["logged"],$PAGE)) {
    header("Location: login.php");
    };
    };

    Rodrigo posted on 2008-02-10 02:57:30
    Same question as Art:

    Do you know how to destroy the session when a user closes the window or closes the tab?
    --------------------
    Veselin: if window is closed session is destroyed but if a tab is close it is not

    Veselin posted on 2008-02-09 08:12:39
    to make a logout page you can use this code (put it in a PHP file called logout.php)
    <?php
    session_start();
    $_SESSION["logged"]='';
    unset();
    $_SESSION["logged"]
    echo "You are logged out";
    ?>

    Ginger posted on 2008-02-08 21:45:08
    I'm very new at this. Your code worked splendidly for me and it was really easy to follow. However, how exactly do I tie the php code to the database, so that when users click the Submit button, the php page actually queries my database for the user information ... and allows authenticated users through?
    ------------------
    Veselin: First you will need to change login.php file and make a SELECT query to your MySQL table to check if there is an user with inputted username and password. If there is such an user you should assign the username (or a random ID associated to that user) to $_SESSION["logged"] variable. You will also have to change check_logged() function and instead of using array_key_exists() you should do SELECT query to the users table again to see if the value in $_SESSION["logged"] variable is in your database.

    Pages:   1 2 3 4 5 6 7
       
    If you have any additions, suggestions or modifications for this example please contact us or use the form below.
       
    Your name:
    Your email: (email address will not be posted on the web site)
    Comment:
    Verification