Help needed with Php session variable..!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hilary comber
    New Member
    • Jan 2012
    • 2

    Help needed with Php session variable..!

    the code does not head me to the index page instead it head me to (itself) even though true user name and password is set
    Code:
    <?php
    session_start();
    if (isset($_SESSION['pass'])){
    	header("location:index.php");
    	exit();
    	}
        //Connection to database
    	  if(isset($_POST['enter']))
    		  { 	
    			   if($_POST['name']!=NULL && $_POST['pass']!=NULL)
    					{
    						   	//...
    							$pass1=preg_replace('#[*0-9]#i','',$_POST['pass']);
    							$manager1=preg_replace('#[*A-Za-z0-9]#i','',$_POST['name']);
    							//connect to the databases
    								include "../ex/connect_to_mysql.php";
    							//Query database
    						   $sql=mysql_query("select password from admin where password='$pass' and name='$manager' limit 1");
    						   $adminCount=mysql_num_rows($sql);
    						   if($adminCount==1)
    							   {
    								   while($row=mysql_fetch_array($sql))
    								   		{
    										   $pass=$row['password'];
    										}
    										$_SESSION['pass']=$pass1;
    										$_SESSION['manager']=$manager1;
    										//send to index page..
    									header("location:index.php");
    									exit();
    							   }
    						   else
    							   {
    									 echo 'The Information Is Not Correct: <a href="index.php">Click Here...!';
    									  exit();  
    								}
    					}
    				else
    				   {
    					   header("location:login.php");
    					   exit();
    				   }
    		  }
    ?>
    <!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=utf-8" />
    <title>login</title>
    </head>
    
    <body>
    <form action="login.php" name="frmlog" method="post">
    <table width="45%" border="1" align="center">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        </tr>
      <tr>
        <td colspan="2" align="center"><h3>Enter Login Details:</h3></td>
        </tr>
      <tr>
        <td width="32%" align="right">User Name:</td>
        <td width="56%"><label for="name"></label>
          <input name="name" type="text" id="name" size="30" /></td>
      </tr>
      <tr>
        <td align="right">password:</td>
        <td><label for="pass"></label>
          <input name="pass" type="password" id="pass" size="30" /></td>
      </tr>
      <tr>
        <td></td>
        <td align="left"><input type="submit" name="enter" id="enter" value="Login" /></td>
      </tr>
      <tr>
        <td colspan="2" align="center">hilaryComber &copy;2012</td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    Last edited by Dormilich; Jan 17 '12, 09:54 PM.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    I dont know anything about preg_replace, i dont know how it work, so I wonder what does that function returns.

    Besides your Query is very much unsafe. Your server will be cracked very fast.

    learn about the SQL Injection. and also look at mysql_real_esca pe_string function

    and do not put your password into session. keep the username.

    I wonder which one is index.php????

    if your user logged in already then he will be forwarded to index.php file using location header. But if user login failed then user is suggested to click on certain link that will forward to index.php? what is your intension buddy?

    Comment

    • hilary comber
      New Member
      • Jan 2012
      • 2

      #3
      When user directed to index.php the session will automatic realize that user session was not created, then he/she will be directed to login.php page,Since user session was not created

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I dont know anything about preg_replace, i dont know how it work, so I wonder what does that function returns.
        preg_replace() returns an array if the subject parameter is an array, or a string otherwise.

        If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.

        Comment

        Working...