Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';{';在if语句中导致错误?PHP_Php - Fatal编程技术网

为什么';{';在if语句中导致错误?PHP

为什么';{';在if语句中导致错误?PHP,php,Php,我正在尝试为我的用户创建一个登录系统。像往常一样,我将在线找到的代码拼接在一起,并试图分析它是如何工作的 我的想法是,我有一个用户名和密码字段要填写,当他们提交时,定义的功能被激活。这是我的代码;只有一页: <?php if(isset($_SESSION['logged in']){ //<-- this '{' is causing an error, because it is apparently "unexpected" header(Location:"

我正在尝试为我的用户创建一个登录系统。像往常一样,我将在线找到的代码拼接在一起,并试图分析它是如何工作的

我的想法是,我有一个用户名和密码字段要填写,当他们提交时,定义的功能被激活。这是我的代码;只有一页:

<?php

    if(isset($_SESSION['logged in']){ //<-- this '{' is causing an error, because it is apparently "unexpected"
    header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
    }
    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 = "SELECT * FROM tutors WHERE username = ' . $S_username . ' AND password = ' . $S_password . ' LIMIT 1" ;
$check = mysql_query($sql, $con);
$check_again = mysql_num_rows($check);

    if($check_again) == 1 {
    session_start();
    $_SESSION['logged in'] = TRUE;
    $_SESSION['username'] = $S_username;
    }
    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"> //login.php is this page

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>

您缺少一个右括号:

if(isset($_SESSION['logged in']){
应该是:

if(isset($_SESSION['logged in'])){
if(设置($\u会话['logged in']){

应该是

if(isset($\u会话['logged in']){

你也需要改变

if($check_再次)==1{

if($check_再次==1){

还有许多其他问题会导致脚本中出现错误,但要回答您的问题,请参见上文

if(isset($_SESSION['logged in']))
                               ^--^

您错过了结束括号

字符串/常量错误:

if(isset($_SESSION['logged in']){
//-----------------------------^
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
//-----^       ^
}
if($check_again) == 1 {
$sql = "SELECT * FROM tutors WHERE username = ' . $.......
//-----^--------------------------------------^
应该是

if (isset($_SESSION['logged in'])) {
    header("Location:http://terrythetutor.com/passwordprotectedpage.php");
}
if ($check_again == 1) {
$sql = "SELECT * FROM tutors WHERE username = " . $.......
如果:

if(isset($_SESSION['logged in']){
//-----------------------------^
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
//-----^       ^
}
if($check_again) == 1 {
$sql = "SELECT * FROM tutors WHERE username = ' . $.......
//-----^--------------------------------------^
应该是

if (isset($_SESSION['logged in'])) {
    header("Location:http://terrythetutor.com/passwordprotectedpage.php");
}
if ($check_again == 1) {
$sql = "SELECT * FROM tutors WHERE username = " . $.......
我找到了第四个:

if(isset($_SESSION['logged in']){
//-----------------------------^
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
//-----^       ^
}
if($check_again) == 1 {
$sql = "SELECT * FROM tutors WHERE username = ' . $.......
//-----^--------------------------------------^
应该是

if (isset($_SESSION['logged in'])) {
    header("Location:http://terrythetutor.com/passwordprotectedpage.php");
}
if ($check_again == 1) {
$sql = "SELECT * FROM tutors WHERE username = " . $.......

我建议改进您的代码风格并消除那些语法错误。您缺少了一个括号。
if(isset($_SESSION['logged in']){
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
}
应该是
if(isset($_SESSION['logged in'])){
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
}

就这么简单?非常感谢!捕捉得很好。使用一个好的IDE并突出显示代码将有助于防止此类愚蠢的错误:)@ChristopherHarris很好的观点,没有看到太远您将需要移动
session\u start()
如果您希望使用会话变量,请转到页面顶部。此问题似乎与主题无关,因为问答论坛不是在线拼写检查器(他甚至忽略了错误消息和行号信息).@lvaroG.Vicario如果我知道这只是语法,我就不需要问这个问题了。很抱歉,我觉得有必要问那些有过我没有的语言经验的人。我相信当你学习时,你不需要任何建议。@user2666324-如果你正在学习,你仍然需要遵循网站指南。我们都喜欢bec因为它有高质量的内容,但我们想用垃圾填满它,这是非常自私的,不是吗?丹,我感谢你在这方面的帮助。你有什么建议可以积极改进我的代码风格?@user2666324了解一下伟大的项目,他们是如何编写代码的,以及