login page

Discussion in 'PHP' started by ediri, Nov 21, 2007.

  1. #1
    I write code for login page like this and it direct to other page .but it not work i send my code.pls help me.
    
    <?php
    // start the session
    session_start();
    
    $_SESSION['sessvar'] = $userId;
    $errorMessage = '';
    if(isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
    //if (isset($_POST['submit'])) { // if form has been submitted
    
    
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbname = 'detail';
    
    
    if (!($db = mysql_connect($dbhost, $dbuser)))
    { 
      die("Can't connect to mysql.");     
    }
    else
    { 
      if (!(mysql_select_db("$dbname",$db)))  
    	{ 
        die("Can't connect to db."); 
      	} 
    } 
       $user = $_POST['txtUserId'];
       $pass = $_POST['txtPassword'];
    
    	$userId   = addslashes($user);
      	$password = addslashes($pass);
    
    
       // check if the user id and password combination exist in database
       $sql = "SELECT username
               FROM detail
               WHERE username = '$userId' 
                     AND password = '$password'";
    
       $result = mysql_query($sql) 
                 or die('Query failed. ' . mysql_error()); 
    
    
       if (mysql_num_rows($result) == 1) {
          // the user id and password match, 
          // set the session
         
          // after login we move to the main page
          header('Location: che.php');
          exit;
       } else {
          $errorMessage = 'Sorry, wrong user id or password';
       }
    mysql_close($conn);
    }
    
    ?>
    
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="master.css"/>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body >
    <form action="" method="post">
    <table border="1"  height="500" align="center" width="800">
    <tbody>
    
    <tr>
    <td>
    <table width="700" border="0" cellspacing="2" cellpadding="2" align="center"height="200" >
      <tr height="30">
        <td width="299" align="center" > Login </td>
       <td width="387">&nbsp; </td>
      </tr>
     
      <tr height="30">
        <td width="299" align="center" >
    	<?php
    if ($errorMessage != '') {
    ?>
    <p align="center"><strong><font color="#990000">
    <?php 
    echo $errorMessage; 
    ?>
    </font></strong></p>
    <?php
    }
    ?>
    	</td>
       <td width="387">&nbsp; </td>
      </tr>
      <tr height="30">
        <td width="299" align="center" >User Id</td>
        <td width="387"><input name="txtUserId" type="text" ></td>
      </tr>
      <tr height="30">
        <td align="center" >Password</td>
        <td><input name="txtPassword" type="password"></td>
      </tr>
      <tr height="30">
        <td>&nbsp;</td>
        <td><input name="submit" type="submit" value="Login"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    
    
    
    </td>
    </tr>
    
    </tbody>
    </table>
    </form>
    </body>
    </html>
    
    Code (markup):

     
    ediri, Nov 21, 2007 IP