在给出正确的登录凭据时,会话无法启动

时间:2013-12-20 20:29:33

标签: php sql session

好的,SO。经过大约五个小时筛选可能的重复项并将可能的解决方案应用于我的项目甚至下载PHP IDE以确保我的语法对每个人都很好和整洁..我终于到了需要一些建议的地步

我的两个问题(可能是相关的):

当有人使用我存储在数据库中的测试参数成功登录时,它们不会被重定向(也许我的if语句不正确?)

如果页面未经首次加载加载,则会显示“错误密码 - 用户名组合”消息。我非常肯定为什么但不太确定如何解决它

<?php session_start(); // this line of code has been added by the instruction of a comment.
    if(isset($submit)) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    }
$con = mysqli_connect("***","***","***","***"); 
$S_username = mysqli_real_escape_string($con, $username);
$S_password = mysqli_real_escape_string($con, $password);

if(mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql = mysqli_query($con, "SELECT * FROM `users` WHERE `username` = '$S_username' AND `password` = '$S_password'");

if(!$sql) {
die(mysqli_error($con)) ; 
}

$check_again = mysqli_num_rows($sql);

    if($check_again == 1) {
    session_start(); // this line of code has been deleted 
    $_SESSION['logged in'] = TRUE;
    $_SESSION['username'] = $S_username;
    header("Location: http://terrythetutor.com/submitvideo.php");
    }
    else {
    echo "Your username and password combination was not recognised. Please try again." ;
    }

?>
<html>
<head> 
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
    <?php include_once 'googleanalytics.php'; ?>
<body>
<a href="http://terrythetutor.com">
    <div class="banner"> </div>
</a>
<?php include 'menu.php'; ?>
<h1 align="center">Please login to access restricted files</h1>
</br>
</br>
</br>
</br>
<div align="center">
<form action = "login.php" method = "post">

Username: <input type = "text" name = "username"></br></br>

Password: <input type = "password" name = "password"></br></br>

<input type = "submit" value = "Login" name="submit">

</form>
</div>
</body>

</html>

欢迎任何和所有反馈。谢谢。

1 个答案:

答案 0 :(得分:0)

仅使用session_start();一次并位于顶部..