login php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolodede
    New Member
    • Apr 2009
    • 63

    login php

    i would like to create php login form but the problem is the form doesnt do anything
    Code:
    <?php require_once("nocache.php"); ?>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<title>login.php</title>
    <?php
    if(isset($_POST['Submit']))
    	if (empty($id) || empty($pword)){
    	    header("location: login.php");
    		}else{
    			$conn = mysql_connect("localhost", "TWAassign2", "student");
    			if(!$conn){
    				die('Conection could not be opened: ' . mysql_error());
    			}
    			$db_selected = mysql_select_db('acadcredit', $conn);
    			$sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
    			$rs = mysql_query($sql, $conn);
    			if (mysql_num_rows($rs)> 0 ) {
    		        session_start();
    			    $_SESSION["who"] = $id;
    				$_SESSION["level"] = mysql_result($rs,0,"level");
    				header("location: selectView.php");
    		    }else{
    				header("location: login.php");
    			}
    		}
    ?>
    
    </head>
    <body>
    <form method="post" action="login.php">
    <h1> Welcome to Login Page </h1>
    <h2> Please enter Your Staff ID and password- Thanks</h2>
    	<p>Staff ID  <input type="text" name="ID"  /><br/>
        Password <input type="password" name="pass" /></p> 
    <p> 
    <?php
    	if(isset($_POST['ID'])){
    		$id = $_POST["ID"];
    		$pword = $_POST["pass"];
    	}
    ?>
    
    	<input type="submit" value="login" />
        <input type="reset"  value="Reset" /></p>
    </form>
    </body>
    </html>
    why isnt working
    thanks
    Last edited by Markus; May 18 '09, 09:04 AM. Reason: Fixed [code] tags.
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    login php Reply

    Code:
    <?php session_start();
    // have to initialised session_start(); top of the page 
    ?>
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>login.php</title>
    <?php
    if(isset($_POST['Submit'])){
    	
    	$id= $_POST['ID'];
    	$pword= $_POST['pass'];
    	
    }
    if (empty($id) || empty($pword)){
    header("location: login.php");
    }else{
    $conn = mysql_connect("localhost", "TWAassign2", "student");
    if(!$conn){
    die('Conection could not be opened: ' . mysql_error());
    }
    $db_selected = mysql_select_db('acadcredit', $conn);
    $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
    $rs = mysql_query($sql, $conn);
    if (mysql_num_rows($rs)> 0 ) {
    session_start();
    $_SESSION["who"] = $id;
    $_SESSION["level"] = mysql_result($rs,0,"level");
    header("location: selectView.php");
    }else{
    header("location: login.php");
    }
    }
    ?>
    
    </head>
    <body>
    <form method="post" action="login.php">
    <h1> Welcome to Login Page </h1>
    <h2> Please enter Your Staff ID and password- Thanks</h2>
    <p>Staff ID <input type="text" name="ID" /><br/>
    Password <input type="password" name="pass" /></p> 
    <p> 
    	<!-- this pice of code goes top of the page then tou check $_POST['submit'] when you submit the page then passing $id and $pword would be
    	initialised,
    	
    	 -->
    <?php
    /*
    if(isset($_POST['ID'])){
    $id = $_POST["ID"];
    $pword = $_POST["pass"];
    }*/
    ?>
    
    <input type="submit" value="login" />
    <input type="reset" value="Reset" /></p>
    </form>
    </body>
    </html>

    Comment

    • lolodede
      New Member
      • Apr 2009
      • 63

      #3
      i did what you said but still doesnt do anything not giving error or goes to next page and doesnt submit to itself so whats wrong with it

      Comment

      • prabirchoudhury
        New Member
        • May 2009
        • 162

        #4
        try this and if any error come mention that
        Code:
        <?php session_start();
        // have to initialised session_start(); top of the page 
        ?>
        
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <title>login.php</title>
        </head>
        <body >
        	<br><br>
        
         
         <?
         
         if(isset($_POST['submit'])){
        	
        	$id= $_POST['ID'];
        	$pword= $_POST['pass'];
        	/echo "password".$pword;
         //echo "id".$id;
        }
         if (empty($id) || empty($pword)){
        header("location: login.php");
        }else{
        $conn = mysql_connect("localhost", "TWAassign2", "student");
        if(!$conn){
        die('Conection could not be opened: ' . mysql_error());
        }
        $db_selected = mysql_select_db('acadcredit', $conn);
        $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
        $rs = mysql_query($sql, $conn);
        if (mysql_num_rows($rs)> 0 ) {
        
        $_SESSION["who"] = $id;
        $_SESSION["level"] = mysql_result($rs,0,"level");
        header("location: selectView.php");
        }else{
        header("location: login.php");
        }
        }
         ?>
          <!--=============================================================================-->
         <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <h1> Welcome to Login Page </h1>
        <h2> Please enter Your Staff ID and password- Thanks</h2>
        <p>Staff ID <input type="text" name="ID" /><br/>
        Password <input type="password" name="pass" /></p> 
        
        <input type="submit" value="login" />
        <input type="reset" value="Reset" /></p>
        </form>
         
          
        	  
        </body>
        </html>

        Comment

        • lolodede
          New Member
          • Apr 2009
          • 63

          #5
          the error was no input satisfied and then page not found why? and i have this at the top of page0 ) { $_SESSION["who"] = $id; $_SESSION["level"] = mysql_result($r s,0,"level"); header("locatio n: selectView.php" ); }else{ header("locatio n: login.php"); } } ?> please help thanks

          Comment

          • lolodede
            New Member
            • Apr 2009
            • 63

            #6
            i realy need help to create the login page as soon as possible please help
            thanks

            Comment

            • Ciary
              Recognized Expert New Member
              • Apr 2009
              • 247

              #7
              creating a login is really simple:

              Code:
              <?
              if(!isset($_SESSION)){
              session_start();
              }
              
              if($_SERVER['REQUEST_MEHOD']='post'){
              $conn = mysql_connect("myServer","root","");
              mysql_select_db("myDB");
              
              $result = mysql_query("SELECT * FROM users WHERE user = ".$_POST['username']." AND password = ".$_POST['passwd'],$conn);
              
              if(mysql_num_rows($result) == 1){
              $_SESSION['user']=$_POST['username'];
              $_SESSION['pw']=$_POST['passwd'];
              
              header('location: anotherpage.php');
              } else {
              echo 'failed to login';
              }
              }
              
              mysql_free_result($result);
              mysql_close($conn);
              ?>
              
              <form method='post'>
              <input type='text' id='username' name='username' />
              <input type='password' id='passwd' name='passwd' />
              <input type='submit' />
              </form>
              on all other pages you use exacly the same code (appart from the form) but instead you use your session variables in your query. also, when you see the SQLquery returns more then one or no row, you use
              Code:
              header('location: login.php');
              in your if structure

              what did you do wrong:
              your first try came very close. you only forgot that php is executed before page loads so this
              Code:
              <?php
                  if(isset($_POST['ID'])){
                      $id = $_POST["ID"];
                      $pword = $_POST["pass"];
                  }
              ?>
              needs to be checked in the beginning instead of at the end of your page.

              hope that helped

              jan

              Comment

              • lolodede
                New Member
                • Apr 2009
                • 63

                #8
                i did what you said and i have these errors Notice: Undefined index: username in D:\wwwroot\twa0 9\twa015\assign ment2\login1.ph p on line 18
                Notice: Undefined index: passwd in D:\wwwroot\twa0 9\twa015\assign ment2\login1.ph p on line 18
                Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in D:\wwwroot\twa0 9\twa015\assign ment2\login1.ph p on line 20
                failed to login
                Warning: mysql_free_resu lt(): supplied argument is not a valid MySQL result resource in
                Code:
                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
                
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
                
                <head>
                	<title>login.php</title>
                	<?php
                if(!isset($_SESSION)){
                session_start();
                }
                 
                if($_SERVER['REQUEST_METHOD']='post'){
                $conn = mysql_connect("localhost", "TWAassign2", "student");
                $db_selected = mysql_select_db('acadcredit', $conn);
                
                 
                $result = mysql_query("SELECT staffName accessLevel  FROM users WHERE staffID= ".$_POST['username']." AND password = ".$_POST['passwd']);
                 
                if(mysql_num_rows($result) == 1){
                $_SESSION['user']=$_POST['username'];
                $_SESSION['pw']=$_POST['passwd'];
                 
                header('location: selectView.php');
                } else {
                echo 'failed to login';
                }
                }
                 
                mysql_free_result($result);
                mysql_close($conn);
                ?>
                </head>
                <body>
                <form method='post'>
                <p><input type='text' id='username' name='username' /> <br/>
                <input type='password' id='passwd' name='passwd' /></p>
                <p> <input type='submit' /> </p><input type="reset"  value="Reset" />
                </form>
                </body>
                </html>
                i need help please

                Comment

                • Ciary
                  Recognized Expert New Member
                  • Apr 2009
                  • 247

                  #9
                  ok my fault i think. try this:
                  Code:
                  if($_SERVER['REQUEST_METHOD'] == 'post'){
                  i forgot to use double 'equals' :S

                  Comment

                  Working...