PHP Login Script is not Working

Discussion in 'PHP' started by selvaganesh87, Sep 25, 2010.

  1. #1
    Am learning PHP, I have been trying for this login form for past four days, but i cant find the result and also cant find the bug in my query can any body here help me to find the error in my query. I will much thankful for them

    This is the MYSQL Query i used

    mysql> select * from users where password=password('god');
    +--------+-------+-------------------------------------------+
    | userID | name | password |
    +--------+-------+-------------------------------------------+
    | 1 | selva | *895DC6A9BBBCFCDB8B7FDB51FA0383A59F38C60E |
    +--------+-------+-------------------------------------------+
    1 row in set (0.00 sec)

    mysql> desc users;
    +----------+-------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +----------+-------------+------+-----+---------+----------------+
    | userID | int(11) | | PRI | NULL | auto_increment |
    | name | varchar(20) | | | | |
    | password | varchar(50) | | | | |
    +----------+-------------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)

    This is the PHP code i used to develop Login Form

    <? session_start() ?>
    <html>
    <head>
    <title>Log In Form</title>
    </head>
    <body>
    <?
    if($user && $pass) {
    if($logged_in_user == $user) {
    echo "Your Already Logged In";
    echo $user. ", your already logged in";
    exit;
    }
    $db = mysql_connect("localhost")or die("Cannot Connect");
    mysql_select_db("userlist", $db)or die("Cannot Select Database");
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $user = mysql_real_escape_string($user);
    $pass = mysql_real_escape_string($pass);
    $user = stripslashes($user);
    $pass = stripslashes($pass);
    $query = "select * form users where name='$user'AND password=PASSWORD('$pass')";
    //$query = "select * form users where name like '%$user%' ";
    $result = mysql_query($query);
    if(!$result) {
    echo "error" .mysql_error();
    exit;
    }
    if(mysql_num_rows($result) == 1) {
    $logged_in_user = $user;
    session_register("logged_in_user");
    //echo $user.;
    echo "Welcome" .$logged_in_user;
    exit;
    }
    else {
    echo "Invalid User Please try again";
    }
    }
    else if($name || $pass)
    {
    echo "Please Enter Both The Field" ;
    }
    ?>
    <form action="Login.php" method="POST">
    <H2>Enter Login Details</H2>
    User Name:
    <input type=text name="user"><BR><BR>
    Password :
    <input type=password name="pass"><BR><BR>
    <input type=submit value="Login"><BR>
    </form>
    </body>
    </html>
     
    selvaganesh87, Sep 25, 2010 IP
  2. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    Hi!

    Try:

    query = "select * form users where name='$user' AND password='$pass'";
     
    HungryMinds, Sep 25, 2010 IP
  3. selvaganesh87

    selvaganesh87 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply. i used the same procedure what you told, even i get a same response. I get the page with username text box and password text box. After typing the username and password i didnt get any response it is landing in the same page
     
    selvaganesh87, Sep 25, 2010 IP
  4. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #4
    Ok. Wait...
     
    HungryMinds, Sep 25, 2010 IP
  5. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    OMG My Friend :S

    Use php with PHp Start Code Like: "<?php"

    U Are Using Just "<?" :S

    Check... And Reply... I'm Here...
     
    HungryMinds, Sep 25, 2010 IP
  6. Media Signal

    Media Signal Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    $db = mysql_connect("localhost")or die("Cannot Connect");
    PHP:
    What the heck was this supposed to mean ? :confused:

    $db = mysql_connect("localhost", "username", "password") or die("Cannot Connect");
    PHP:
     
    Media Signal, Sep 25, 2010 IP
  7. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #7
    Another Mistake...

    Note Down All Your Mistakes For Next Time...

    U Are Using: "select * form"

    It's "from" Not "form"
     
    HungryMinds, Sep 25, 2010 IP
  8. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #8
    Ok Now Final CODE & Tested, It's Working Perfect...

    CLICK HERE TO PREVIEW

    
    <?php session_start(); ?>
    <html>
    <head>
    <title>Log In Form</title>
    </head>
    <body>
    <?php
    
    if(isset($_POST["user"]) && isset($_POST["pass"]))
    {
    	
    	$user = $_POST["user"];
    	$pass = $_POST["pass"];
    	
    	if($user == "" || $pass == "")
    	{
    		echo "Blank Fields";
    	}
    	else
    	{	
    	
    		if($_SESSION["logged_in_user"] == $user)
    		{
    			echo $user. ", your already logged in";
    		}
    		else
    		{
    	
    			$db = mysql_connect("localhost", "", "") or die("Cannot Connect");
    			mysql_select_db("userlist", $db)or die("Cannot Select Database");
    			
    			$user = mysql_real_escape_string($user);
    			$pass = mysql_real_escape_string($pass);
    			$user = stripslashes($user);
    			$pass = stripslashes($pass);
    			$query = "select * from users where user='$user' AND pass='$pass'";		
    			$result = mysql_query($query);
    			
    			if(!$result)
    			{
    				echo "error" .mysql_error();
    			}
    			
    			if(mysql_num_rows($result) == 1)
    			{
    				$_SESSION["logged_in_user"] = $user;
    				echo "Welcome, Mr. " . $_SESSION["logged_in_user"];
    			}
    			else
    			{
    				echo "Invalid User Please try again";
    			}
    			
    			mysql_close();
    		}	
    	}
    }
    
    ?>
    <form action="index.php" method="POST">
    <H2>Enter Login Details</H2>
    User Name:
    <input type=text name="user"><BR><BR>
    Password :
    <input type=password name="pass"><BR><BR>
    <input type=submit value="Login"><BR>
    </form>
    </body>
    </html>
    
    Code (markup):
     
    Last edited: Sep 25, 2010
    HungryMinds, Sep 25, 2010 IP
  9. selvaganesh87

    selvaganesh87 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Great successfully working. Thank your very much ya. I find your hungry mind from your site,very interesting to see your mind only with knowledge. As i worked in SEO i can suggest you to make your site more better in search engine. You can place related content for each topic you used in the title. so that you can find your site in search result with proper back links, that i find in your signature. Thanks for your effort on my query
     
    selvaganesh87, Sep 25, 2010 IP
  10. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #10
    Thank You So Much My Friend, It's My Pleasure :)
    These Days Im Working In SEO :)
    But Not For My Site...
    I've Another One...
    So Im Reading Aaron Walls' eBook...
    SEO BIBLE eBook...
    And Etc...
    And Thank You So Much For Your Guide...
    Off course I'll Do All These Things...
    BEST OF LUCK FOR YOUR FUTURE :)
    And Remember 1 Thing! I'll Knock Your Door Some Day For SEO HELP :)
     
    HungryMinds, Sep 25, 2010 IP