MitchFarmer
MitchFarmer

Reputation: 9

PHP header not working on local server

Here is each one of my .php pages. I can get it to register and go onto my local server. However when I go to the login page and actually login it will not redirect to the members page. I am not sure what the issue it. I am pretty new to PHP and the code looks decent. Very simple but I am trying to get this to work. Any help is appreciated. Thanks.

config.php

<?php
$host     = "localhost"; 
$username = "root"; 
$password = "root"; 
$db       = "motofoto"; 

//Connect to MySQL Server 
$con = mysqli_connect($host,$username,$password,$db) or die("Can not connect to Server.");

?> 

Login.php

<?php
    session_start(); 
require "config.php"; //Connection Script, include in every file! 

//Check to see if the user is logged in. 
if(isset($_SESSION['username'])){ 
   header( "Location: members.php" ); //isset check to see if a variables has been 'set' 
} 



if(isset($_POST['submit'])) 
{ 
   //Variables from the table 
   $user  = $_POST['user']; 
   $pass  = $_POST['pass']; 

   //Prevent MySQL Injections 
   $user  = stripslashes($user); 
   $pass  = stripslashes($pass); 

   $user  = mysqli_real_escape_string($con, $user); 
   $pass  = mysqli_real_escape_string($con, $pass); 

   //Check to see if the user left any space empty! 
   if($user == "" || $pass == "") 
   { 
      echo "Please fill in all the information!"; 
   } 

   //Check to see if the username AND password MATCHES the username AND password in the DB
   else 
   { 
      $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user' and password = '$pass'") or die("Can not query DB."); 
      $count = mysqli_num_rows($query); 

      if($count == 1){ 
         //YES WE FOUND A MATCH! 
         @$_SESSION['username'] = $user; //Create a session for the user! 
         header ("Location: members.php"); 
      } 

      else{ 
         echo "Username and Password DO NOT MATCH! TRY AGAIN!"; 
      } 
   } 

} 

?> 

<html> 
<table> 

<tr> 
<form name="register" method="post" action="login.php"> 
<td> 

<table> 

<tr> 
<td colspan="3"><strong><center>Login </center></strong></td> 
</tr> 

<tr> 
<td>Username</td> 
<td>:</td> 
<td><input autofocus name="user" type="text" id="user"></td> 
</tr> 

<tr> 
<td>Password</td> 
<td>:</td> 
<td><input name="pass" type="password" id="pass"></td> 
</tr> 

<tr> 
<td></td> 
<td></td> 
<td><input type="submit" name="submit" value="Login"></td> 
</tr> 

</table> 
</td> 
</form> 
</tr> 
</table> 

<table>
   <tr>
      <td>Not a Member? <a href="register.php">Register</a></td>
   </tr>
</table>


</html>

register.php

<?php
session_start();  //Must Start a session. 

require "config.php"; //Connection Script, include in every file! 

//Check to see if the user is logged in. 
//'isset' check to see if a variables has been 'set' 
if(isset($_SESSION['username'])){ 
   header("location: members.php"); 
} 

//Check to see if the user click the button 
if(isset($_POST['submit'])) 
{ 
   //Variables from the table 
   $user  = $_POST['user']; 
   $pass  = $_POST['pass']; 
   $rpass = $_POST['rpass']; 

   //Prevent MySQL Injections 
   $user  = stripslashes($user); 
   $pass  = stripslashes($pass); 
   $rpass = stripslashes($rpass); 

   $user  = mysqli_real_escape_string($con, $user); 
   $pass  = mysqli_real_escape_string($con, $pass); 
   $rpass = mysqli_real_escape_string($con, $rpass); 

   //Check to see if the user left any space empty! 
   if($user == "" || $pass == "" || $rpass == "") 
   { 
      echo "Please fill in all the information!"; 
   } 

   else 
   { 
      //Check too see if the user's Passwords Matches! 
      if($pass != $rpass) 
      { 
         echo "Passwords do not match! Try Again"; 
      } 

      //CHECK TO SEE IF THE USERNAME IS TAKEN, IF NOT THEN ADD USERNAME AND PASSWORD INTO THE DB 
      else 
      { 
         //Query the DB 
         $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user'") or die("Can not query the TABLE!"); 

         //Count the number of rows. If a row exist, then the username exist! 
         $row = mysqli_num_rows($query); 
         if($row == 1) 
         { 
            echo "Sorry, but the username is already taken! Try again."; 
         } 

         //ADD THE USERNAME TO THE DB 
         else 
         { 
            $add = mysqli_query($con,"INSERT INTO members (id, username, password) VALUES (null, '$user' , '$pass') ") or die("Can't                Insert! "); 
            echo "Successful! <a href='members.php'> Click Here </a> to log In."; 
         } 


      }       

   } 

} 
?> 

<html> 
<table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black"> 

<tr> 
<form name="register" method="post" action="register.php"> 
<td> 

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 

<tr> 
<td colspan="3"><strong><center>Registration</center></strong></t
d> 
</tr> 

<tr> 
<td width="78">Username</td> 
<td width="6">:</td> 
<td width="294"><input name="user" type="text" id="user"></td> 
</tr> 

<tr> 
<td>Password</td> 
<td>:</td> 
<td><input name="pass" type="password" id="pass"></td> 
</tr> 

<tr> 
<td>Repeat Password</td> 
<td>:</td> 
<td><input name="rpass" type="password" id="rpass"></td> 
</tr> 

<tr> 
<td></td> 
<td></td> 
<td><input type="submit" name="submit" value="Register"></td> 
</tr> 

</table> 
</td> 
</form> 
</tr> 
</table> 

</html>

members.php

<?php
session_start(); 
require "config.php"; 

//Check to see if the user is logged in. 
if(isset($_SESSION['username'])){ 
   echo "Hello ".$_SESSION['username'].", you are logged in. <br /> This the member's page! Nothing here :(. <a href='logout.php'>Click Here </a>to log out."; 
} 

else{ 
   echo "Please <a href='login.php'>Log In </a> to view the content on this page!"; 
} 

?>

logout.php

<?php
session_start(); 
require "config.php"; 
session_destroy(); 

echo "You have successfully logged out. <a href='login.php'> Click here </a> to login!"; 

?> 

Upvotes: 0

Views: 286

Answers (2)

Alfred
Alfred

Reputation: 346

1)try to add session close function, this may help as session is possibly not saved yet.

 @$_SESSION['username'] = $user; //Create a session for the user! 
 session_write_close();
 header ("Location: members.php"); 

2) And as Fred mentioned try to debug with php error reporting.

3) Small note: register.php => change link to Login.php not members.php

echo "Successful! <a href='Login.php'> Click Here </a> to log In.";

PS: I tested your script and it worked fine even without session_write_close();

Upvotes: 1

Amol
Amol

Reputation: 343

Alternatively you can use following function to redirect through java script. It's not the solution but you can use as alternative.

function redirect($url)
{
    echo $data= "<script type='text/javascript'> window.location.href = '".$url."'; </script>";
    break;
}

Upvotes: 0

Related Questions