Login script doesn't run

Discussion in 'PHP' started by pcf43, Mar 25, 2011.

  1. #1
    i have some problems with the following code:
    
    <html>
    	<head>
    		<title>Session Page</title>
    	</head>
    	<body>
    	<?php
    	
    	session_start();		//start session
    	$user=$_POST['username'];
    	$pass=$_POST['password'];
    	$submit=$_POST['submit'];
    	
    	if($user&&$pass==false){
    		echo "please login";
    	}
    	
    	if(isset($_SESSION['loginUser']) && isset($_SESSION['loginPass'])){			
    			$_SESSION['loginUser']=$user;
    			$_SESSION['loginPass']=$pass;
    							
    	$loginCheck=mysql_query("SELECT * FROM users WHERE username=$user");	
    	
    	$rowUser=mysql_query("SELECT username,password FROM users");			
    	$num_rows=mysql_num_rows($rowUser);
    	
    	$result=mysql_result($loginCheck,$num_rows) or die(mysql_error());	
    			if($result&&$submit){		//this is another part which seems to not be runned by the script																			
    				echo "Welcome back ".$user.",log in succesfully";
    				}else{
    				echo "Login failed or fields not completed";	
    				}
    	}
    			
    	?>
    	</body>
    </html>
    
    PHP:
    also I get the following notices for the variables containing $_POST:
    Notice: Undefined index: username in C:\wamp\www\***\session.php on line 9

    Notice: Undefined index: password in C:\wamp\www\***\session.php on line 10

    Notice: Undefined index: submit in C:\wamp\www\***\session.php on line 11
     
    pcf43, Mar 25, 2011 IP
  2. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #2
    First of: Sesssion should be started at top of file BEFORE html tags.
    Second: To fix these Notices, do it like this

    <html>
        <head>
            <title>Session Page</title>
        </head>
        <body>
        <?php
        
        session_start();        //start session
    if (isset($_POST['submit'])
    {
        $user=$_POST['username'];
        $pass=$_POST['password'];
        $submit=$_POST['submit'];
        
        if($user&&$pass==false){
            echo "please login";
        }
        
        if(isset($_SESSION['loginUser']) && isset($_SESSION['loginPass'])){         
                $_SESSION['loginUser']=$user;
                $_SESSION['loginPass']=$pass;
                                
        $loginCheck=mysql_query("SELECT * FROM users WHERE username=$user");    
        
        $rowUser=mysql_query("SELECT username,password FROM users");            
        $num_rows=mysql_num_rows($rowUser);
        
        $result=mysql_result($loginCheck,$num_rows) or die(mysql_error());  
                if($result&&$submit){       //this is another part which seems to not be runned by the script                                                                           
                    echo "Welcome back ".$user.",log in succesfully";
                    }else{
                    echo "Login failed or fields not completed";    
                    }
        }
     }
        ?>
        </body>
    </html>
    PHP:
    Third: You are not connected into database
    Fourth: this line:

    $result=mysql_result($loginCheck,$num_rows) or die(mysql_error());

    gives you only last user from that table (if you have more than one, it will throw you warning) :p
     
    G3n3s!s, Mar 25, 2011 IP
  3. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yepp that's correct...
     
    Kload, Mar 27, 2011 IP