login problem

Discussion in 'PHP' started by rhodarose, Sep 5, 2010.

  1. #1
    I have a sample code of session condition if the user is already login and i have a separate code for login page.. I don't know how can i add the code of seesion to my login code

    here is the code for session:
    
    <?php  
    session_start(); 
    
    //require_once 'conn.php';  
    $db_name="dspi"; 
    
    mysql_connect("localhost", "root", "") or die("Cannot connect to server");
    mysql_select_db("$db_name")or die("Cannot select DB");   
    
    
            $department = mysql_real_escape_string($_POST['department']);   
            $username = mysql_real_escape_string($_POST['username']); 
    
            $sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error()); 
            $ct = mysql_num_rows($sql); 
         
            if($ct == 1) { 
                $row = mysql_fetch_assoc($sql);  
             
                if($row['Department']=='Accounting') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='Engineering') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='Finishing_Goods') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='HRAD') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='MIS') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Packaging_and_Design') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Production') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Purchasing_Logistic') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='QA_and_Technical') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Supply_Chain') { 
                    header('location:Company.php'); 
                } 
                else {
                header('location:index.php');
                echo"Incorrect Username or Department"; 
                
                  }  
       }
    ?> 
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>DSPI LOGIN</title>
    <script type="text/javascript">                
    function handleEnter (field, event) {
          var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
          if (keyCode == 13) {
             var i;
             for (i = 0; i < field.form.elements.length; i++)
                if (field == field.form.elements[i])
                   break;
             i = (i + 1) % field.form.elements.length;
             field.form.elements[i].focus();
             return false;
          } 
          else
          return true;
       }      
    
    </script>
    <style type="text/css"> 
    <!--
    BODY { 
    background-image: url(layout_image/bgroundv09.png);
    background-attachment: fixed; 
    } 
    #Dept_Frame {
       position:absolute;
       width:229px;
       height:49px;
       z-index:1;
       left: 441px;
       top: 262px;
    }
    #Department_Option {
       position:absolute;
       width:186px;
       height:32px;
       z-index:2;
       left: 453px;
       top: 275px;
    }
    #Submit_Frame {
       position:absolute;
       width:82px;
       height:35px;
       z-index:3;
       left: 516px;
       top: 320px;
    }
    #Submit_Button {
       position:absolute;
       width:60px;
       height:29px;
       z-index:4;
       left: 524px;
       top: 328px;
    }
    #Username_ImageText {
       position:absolute;
       width:130px;
       height:55px;
       z-index:5;
       left: 319px;
       top: 208px;
    }
    #User_Frame {
       position:absolute;
       width:230px;
       height:46px;
       z-index:6;
       left: 441px;
       top: 216px;
    }
    #Username_Textbox {
       position:absolute;
       width:182px;
       height:23px;
       z-index:7;
       left: 455px;
       top: 228px;
    }
    --> 
    </style>  
    </head>
    <body onload="document.form1.username.focus();">
    <form id="form1" name="form1" method="post" action="">
      <div id="Department_Option">
        <select name="department" onkeypress="return handleEnter(this, event)">
          <option>Choose your Department. . . . . . </option>
          <option value="Accounting" <?php if($_POST['department'] == 'Accounting') echo "selected='selected'"; ?>>Accounting</option>
          <option value="Engineering" <?php if($_POST['department'] == 'Engineering') echo "selected='selected'"; ?>>Engineering</option>
          <option value="Finishing_Goods" <?php if($_POST['department'] == 'Finishing_Goods') echo "selected='selected'"; ?>>Finishing Goods</option>
          <option value="HRAD" <?php if($_POST['department'] == 'HRAD') echo "selected='selected'"; ?>>HRAD</option>
          <option value="MIS" <?php if($_POST['department'] == 'MIS') echo "selected='selected'"; ?>>MIS</option>
          <option value="Packaging_and_Design" <?php if($_POST['department'] == 'Packaging_and_Design') echo "selected='selected'"; ?>>Packaging and Design</option>
          <option value="Production" <?php if($_POST['department'] == 'Production') echo "selected='selected'"; ?>>Production</option>
          <option value="Purchasing_Logistic" <?php if($_POST['department'] == 'Purchasing_Logistic') echo "selected='selected'"; ?>>Purchasing and Logistics</option>
          <option value="QA_and_Technical" <?php if($_POST['department'] == 'QA_and_Technical') echo "selected='selected'"; ?>>QA and Technical</option>
          <option value="Supply_Chain" <?php if($_POST['department'] == 'Supply_Chain') echo "selected='selected'"; ?>>Supply Chain</option>
        </select>
      </div>
      <div id="Submit_Button">
        <input type="Submit" name="submit" value="Submit" id="submit" onclick="doSomething();"/>
      </div>
      <div id="Dept_Frame"><img src="layout_image/subframev02.png" width="229" height="50" /></div>
      <div id="Submit_Frame"><img src="layout_image/subframev02.png" width="80" height="46" /></div>
    
    <div id="Username_ImageText"><img src="layout_image/userv01.png" width="131" height="62" /></div>
    <div id="User_Frame"><img src="layout_image/subframev02.png" width="229" height="50" /></div>
    <div id="Username_Textbox">
      <input name="username" type="text" size="30" onkeypress="return handleEnter(this, event)"/>
    </div>
    </form>
    </body>
    </html>
    
    PHP:

     
    rhodarose, Sep 5, 2010 IP
  2. Cycl0ne

    Cycl0ne Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php  
    session_start(); 
    
    //require_once 'conn.php';  
    $db_name="dspi"; 
    
    mysql_connect("localhost", "root", "") or die("Cannot connect to server");
    mysql_select_db("$db_name")or die("Cannot select DB");   
    
    if ( isset($_POST['submit']) )
    {
           $department = mysql_real_escape_string($_POST['department']);   
           $username = mysql_real_escape_string($_POST['username']); 
    
            $sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error()); 
            $ct = mysql_num_rows($sql); 
         
            if( $sc > 0 ) { 
                $row = mysql_fetch_assoc($sql);  
             
                if($row['Department']=='Accounting') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='Engineering') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='Finishing_Goods') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='HRAD') { 
                    header('location: Company.php'); 
                } elseif($row['Department']=='MIS') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Packaging_and_Design') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Production') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Purchasing_Logistic') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='QA_and_Technical') { 
                    header('location:Company.php'); 
                } elseif($row['Department']=='Supply_Chain') { 
                    header('location:Company.php'); 
                } 
                else {
                header('location:index.php');
                echo"Incorrect Username or Department"; 
                  }  
       }
       }
    ?> 
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>DSPI LOGIN</title>
    <script type="text/javascript">                
    function handleEnter (field, event) {
          var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
          if (keyCode == 13) {
             var i;
             for (i = 0; i < field.form.elements.length; i++)
                if (field == field.form.elements[i])
                   break;
             i = (i + 1) % field.form.elements.length;
             field.form.elements[i].focus();
             return false;
          } 
          else
          return true;
       }      
    
    </script>
    <style type="text/css"> 
    <!--
    BODY { 
    background-image: url(layout_image/bgroundv09.png);
    background-attachment: fixed; 
    } 
    #Dept_Frame {
       position:absolute;
       width:229px;
       height:49px;
       z-index:1;
       left: 441px;
       top: 262px;
    }
    #Department_Option {
       position:absolute;
       width:186px;
       height:32px;
       z-index:2;
       left: 453px;
       top: 275px;
    }
    #Submit_Frame {
       position:absolute;
       width:82px;
       height:35px;
       z-index:3;
       left: 516px;
       top: 320px;
    }
    #Submit_Button {
       position:absolute;
       width:60px;
       height:29px;
       z-index:4;
       left: 524px;
       top: 328px;
    }
    #Username_ImageText {
       position:absolute;
       width:130px;
       height:55px;
       z-index:5;
       left: 319px;
       top: 208px;
    }
    #User_Frame {
       position:absolute;
       width:230px;
       height:46px;
       z-index:6;
       left: 441px;
       top: 216px;
    }
    #Username_Textbox {
       position:absolute;
       width:182px;
       height:23px;
       z-index:7;
       left: 455px;
       top: 228px;
    }
    --> 
    </style>  
    </head>
    <body onload="document.form1.username.focus();">
    <form id="form1" name="form1" method="post" action="">
      <div id="Department_Option">
        <select name="department" onkeypress="return handleEnter(this, event)">
          <option>Choose your Department. . . . . . </option>
          <option value="Accounting" <?php if($_POST['department'] == 'Accounting') echo "selected='selected'"; ?>>Accounting</option>
          <option value="Engineering" <?php if($_POST['department'] == 'Engineering') echo "selected='selected'"; ?>>Engineering</option>
          <option value="Finishing_Goods" <?php if($_POST['department'] == 'Finishing_Goods') echo "selected='selected'"; ?>>Finishing Goods</option>
          <option value="HRAD" <?php if($_POST['department'] == 'HRAD') echo "selected='selected'"; ?>>HRAD</option>
          <option value="MIS" <?php if($_POST['department'] == 'MIS') echo "selected='selected'"; ?>>MIS</option>
          <option value="Packaging_and_Design" <?php if($_POST['department'] == 'Packaging_and_Design') echo "selected='selected'"; ?>>Packaging and Design</option>
          <option value="Production" <?php if($_POST['department'] == 'Production') echo "selected='selected'"; ?>>Production</option>
          <option value="Purchasing_Logistic" <?php if($_POST['department'] == 'Purchasing_Logistic') echo "selected='selected'"; ?>>Purchasing and Logistics</option>
          <option value="QA_and_Technical" <?php if($_POST['department'] == 'QA_and_Technical') echo "selected='selected'"; ?>>QA and Technical</option>
          <option value="Supply_Chain" <?php if($_POST['department'] == 'Supply_Chain') echo "selected='selected'"; ?>>Supply Chain</option>
        </select>
      </div>
      <div id="Submit_Button">
        <input type="Submit" name="submit" value="Submit" id="submit" onclick="doSomething();"/>
      </div>
      <div id="Dept_Frame"><img src="layout_image/subframev02.png" width="229" height="50" /></div>
      <div id="Submit_Frame"><img src="layout_image/subframev02.png" width="80" height="46" /></div>
    
    <div id="Username_ImageText"><img src="layout_image/userv01.png" width="131" height="62" /></div>
    <div id="User_Frame"><img src="layout_image/subframev02.png" width="229" height="50" /></div>
    <div id="Username_Textbox">
      <input name="username" type="text" size="30" onkeypress="return handleEnter(this, event)"/>
    </div>
    </form>
    </body>
    </html>
    PHP:
    Can I see your table . I do not understand your language for more than.
     
    Cycl0ne, Sep 6, 2010 IP
  3. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    probably worth updating session_start() to

    
    if (!isset($_SESSION) {
     session_start(); 
    }
    
    PHP:
    and not quite sure what you are asking
     
    themullet, Sep 6, 2010 IP
  4. rhodarose

    rhodarose Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    my table is consist of
    tbllogin and it has 2 fields the Username and Department

    sample:
    Username XXX Department MIS
     
    rhodarose, Sep 6, 2010 IP
  5. rhodarose

    rhodarose Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i ask about what is the solution on my problem in login page,because when i already login and i accidentally press the back button the login page appear and when i try to login again, i login again, I want that when i was already login I can't login again when i accidentally press the back button
     
    rhodarose, Sep 6, 2010 IP
  6. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #6
    add before your calling
    $username = $_POST['username'];

    same for department less you are dictating the department.

    Or you will have to use a register globals emulator.
    then it will pull your posted information.
     
    lowridertj, Sep 6, 2010 IP