doutao4938 2018-03-10 18:24
浏览 109
已采纳

PHP注册/登录错误[重复]

This question already has an answer here:

I'am trying to create register/login system. However, I've faced some problems. I can't understand where's the mistake in my code. Here's my server.php & register.php. Browser shows that mistake is in line 65. "Parse error: syntax error, unexpected ';'". In my opinion ; must be there.

    <?php
session_start();
    $username = "";
    $email = "";
    $errors = array();
// Connect to the database
$db = mysqli_connect('localhost', 'root', '', 'lead2pro');
// If the register button is clicked
    if(isset($_POST['register'])) {
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $email = mysqli_real_escape_string($db, $_POST['email']);
        $password_1 = mysqli_real_escape_string($db ,$_POST['password_1']);
        $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
        // Ensure that form fields are filled properly
    if(empty($username)) {
            array_push($errors, "Username is required!");
        }
    if(empty($email)) {
            array_push($errors, "Email is required!");
        }
    if(empty($password_1)) {
            array_push($errors, "Password is required!");
        }
    if($password_1 != $password_2) {
        array_push($errors, "The two passwords do not match");
        }
        // If there are no errors, save user to database
        if(count($errors) == 0) {
            $password = md5($password_1); // Hashin the password before storing in database
            $sql = "INSERT INTO users (username, email, password) VALUES('$username', '$email', '$password')";
            mysqli_query($db, $sql);
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: ../system.php'); // Redirect to game location
        }
    }
// log user in from login page 
    if(isset($_POST['login'])) {
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $password = mysqli_real_escape_string($db, $_POST['password']);
        // Ensure that form fields are filled properly
    if(empty($username)) {
            array_push($errors, "Username is required!");
        }
    if(empty(password)) {
            array_push($errors, "Password is required!");
        }
        if(count($errors) == 0){
            $password = md5($password); // Encrypt password before comparing this one with the one in database
            $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
            $result = mysqli_query($db, $query);
            $if (mysqli_num_rows($result) == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: ../system.php'); // Redirect to main page location
            } else {
                array_push($errors, "Wrong username/password combination");
                    header('location: ../php/login.php');
            }
        }
    }
//logout
    if(isset($_GET['logout'])) {
        session_destroy();
        unset($_SESSION['username']);
        header('location: ../php/login.php');
    }
?>

Here's my register.php

<?php include('../includes/server.php');?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Manager | Register</title>
    <link rel="stylesheet" href="../css/reg.css">
</head>
<body>
    <div class="header">
        <h2>Register</h2>
    </div>
<!--    Display validation errors here!     -->
   <?php include('../includes/errors.php');   ?>
    <form action="register.php" method="post">
        <div class="input-group">
            <label>Username</label>
            <input type="text" name="username" value="<?php echo $username; ?>">
        </div>
        <div class="input-group">
            <label>Email</label>
            <input type="text" name="email" value="<?php echo $email; ?>">
        </div>
        <div class="input-group">
            <label>Password</label>
            <input type="password" name="password_1">
        </div>
        <div class="input-group">
            <label>Confirm Password</label>
            <input type="password" name="password_2">
        </div>
        <div class="input-group">
        <button type="submit" name="register" class="btn">Register</button>
        </div>
        <p>
            Already a member? <a href="login.php">Sign in</a>
        </p>
    </form>
</body>
</html>
</div>
  • 写回答

1条回答 默认 最新

  • doushichun9409 2018-03-10 18:31
    关注

    The problem is on a different line:

    $if (mysqli_num_rows($result) == 1) {
        $_SESSION['username'] = $username;
        $_SESSION['success'] = "You are now logged in";
        header('location: ../system.php'); // Redirect to main page location
    }
    

    That $ should not be there in front of the if.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分