重定向后会话丢失(仅限实时服务器)

时间:2015-05-14 11:55:37

标签: php html

我已经创建了一个登录页面供用户登录。代码在 localhost 中正常运行,但在实时服务器中无效。用户无法在实时服务器上登录,我发现每次重定向到index.php时,会话都会丢失,因此用户无法登录会话。你对此有什么想法吗?



<?php 
session_start();
include "header.php";
if (!empty($_POST)){
	include 'database_connect.php';
	$username = $_POST['username'];
	$password = $_POST['password'];	
	$username = stripslashes($username);
	$password = stripslashes($password);
	$username = mysql_real_escape_string($username);
	$password = mysql_real_escape_string($password);
	
	$sql = "SELECT * FROM users where username='$username' and password='$password'";
	$result = mysql_query($sql, $con);
	$rows = mysql_num_rows($result);
	if ($rows == 1){
		$_SESSION['username'] = $username;
		echo "<script>window.location = \"index.php\";</script>";
		//header("Location: index.php?" . SID);
	}else{
		echo "<div class='col-md-12 col-xs-8 alert alert-danger' align='center'>Invalid username and password. Please try again</div>";
	}
	mysql_close($con);
}

?>
&#13;
&#13;
&#13;

的index.php

&#13;
&#13;
<? session_start();?>
<script>alert ('<?php echo $_SESSION['username']; ?>');</script>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

检查index.php页面中的会话。启动会话和回显用户会话。如果你得到,那么尝试条件。

<?php  session_start(); 
echo $_SESSION["username"];

if($_SESSION["username"]){


echo "bla bla";


}?>

答案 1 :(得分:-1)

<?php 
session_start();
include "header.php";
if (!empty($_POST)){
	include 'database_connect.php';
	$username1 = $_POST['username']; // change it
	$password1 = $_POST['password'];	   // change it
	$username = stripslashes($username1); // change it
	$password = stripslashes($password1); // change it
	$username = mysql_real_escape_string($username);
	$password = mysql_real_escape_string($password);
	
	$sql = "SELECT * FROM users where username='$username' and password='$password'";
	$result = mysql_query($sql, $con);
	$rows = mysql_num_rows($result);
	if ($rows == 1){
		$_SESSION['username'] = $username;
		echo "<script>window.location = \"index.php\";</script>";
		//header("Location: index.php?" . SID);
	}else{
		echo "<div class='col-md-12 col-xs-8 alert alert-danger' align='center'>Invalid username and password. Please try again</div>";
	}
	mysql_close($con);
}

?>

您在删除用户名/密码之前写入相同名称的用户名/密码,因此请更改名称并尝试一次

答案 2 :(得分:-2)

在下面的代码中,

<?php session_start(); // your header.php file should not have a session start in that file
include "header.php";
if (!empty($_POST)){
    include 'database_connect.php';

    $username = $_POST['username'];
    $password = $_POST['password']; 
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

    $sql = "SELECT * FROM users where username='$username' and password='$password'";
    $result = mysql_query($sql, $con);
    $rows = mysql_num_rows($result);
    if ($rows == 1){
        $_SESSION['username'] = $username;

        ?><script>alert ('<?php echo $_SESSION['username']; ?>');</script><?
        echo "<script>window.location = \"index.php\";</script>";
        exit();
        //header("Location: index.php?" . SID);
    }else{
        echo "<div class='col-md-12 col-xs-8 alert alert-danger' align='center'>Invalid username and password. Please try again</div>";
    }
    mysql_close($con);
}

?>

php脚本启动后,您需要在页面的第一行使用session_start()函数