Заголовок PHP не работает на локальном сервере

Вот каждая из моих страниц .php. Я могу заставить его зарегистрироваться и зайти на мой локальный сервер. Однако, когда я перехожу на страницу входа в систему и на самом деле войти, он не будет перенаправлять на страницу участников. Я не уверен, в чем проблема. Я довольно новичок в PHP, и код выглядит достойно. Очень просто, но я пытаюсь заставить это работать. Любая помощь приветствуется. Благодарю.

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!";

?>

0

Решение

1) попробуйте добавить функцию закрытия сессии, это может помочь, так как сессия, возможно, еще не сохранена.

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

2) И, как упоминал Фред, попробуйте отладить с помощью отчетов об ошибках php.

3) Небольшое примечание: register.php => изменить ссылку на Login.php, а не members.php

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

PS: я протестировал ваш скрипт и он работал нормально даже без session_write_close ();

1

Другие решения

В качестве альтернативы вы можете использовать следующую функцию для перенаправления через скрипт Java. Это не решение, но вы можете использовать в качестве альтернативы.

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

По вопросам рекламы [email protected]