为什么我的if语句中的'{'会导致错误? PHP

时间:2013-12-18 17:30:29

标签: php

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

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

<?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>

我的目的是让登录用户能够访问未登录的用户无法访问的页面。我有一个第二个问题:如果我只想让登录的个人显示不同页面的部分,我应该放在哪个代码?

谢谢大家。你总是很棒。

5 个答案:

答案 0 :(得分:6)

您缺少右括号:

if(isset($_SESSION['logged in']){

应该是:

if(isset($_SESSION['logged in'])){

答案 1 :(得分:2)

if(isset($_SESSION['logged in']){

应该是

if(isset($_SESSION['logged in'])){

您还需要更改

if($check_again) == 1 {

if($check_again == 1) {

还有许多其他问题会导致您的脚本出现错误,但要回答您的问题,请参阅上述内容。

答案 2 :(得分:1)

String / Const错误:

if(isset($_SESSION['logged in']){
//-----------------------------^
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
//-----^       ^
}

应该是

if (isset($_SESSION['logged in'])) {
    header("Location:http://terrythetutor.com/passwordprotectedpage.php");
}

如果

if($check_again) == 1 {

应该是

if ($check_again == 1) {

我找到了第4名:

$sql = "SELECT * FROM tutors WHERE username = ' . $.......
//-----^--------------------------------------^

应该是

$sql = "SELECT * FROM tutors WHERE username = " . $.......

我建议改进您的代码风格并清除这些语法错误。

答案 3 :(得分:1)

您缺少一个括号。

if(isset($_SESSION['logged in']){
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
}

应该是

if(isset($_SESSION['logged in'])){
header(Location:"http://terrythetutor.com/passwordprotectedpage.php");
}

答案 4 :(得分:0)

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

你错过了结束括号