drpmazn9021 2018-06-16 15:44
浏览 63
已采纳

获取错误严格标准:只应在第20行的register.php中通过引用传递变量

I downloaded login php script from GitHub (Link to it) and when I try to register, it displays me this error: Strict Standards: Only variables should be passed by reference in /homework/register.php on line 20 On login page it doesn't show any errors.

Code that I was using:

 <?php

    session_start();

    if( isset($_SESSION['user_id']) ){
        header("Location: /");
    }

    require 'database.php';

    $message = '';

    if(!empty($_POST['email']) && !empty($_POST['password'])):

        // Enter the new user in the database
        $sql = "INSERT INTO users (email, password) VALUES (:email, :password)";
        $stmt = $conn->prepare($sql);

        $stmt->bindParam(':email', $_POST['email']);
        $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));

        if( $stmt->execute() ):
            $message = 'Successfully created new user';
        else:
            $message = 'Sorry there must have been an issue creating your account';
        endif;

    endif;

    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>Register Below</title>
        <link rel="stylesheet" type="text/css" href="assets/css/style.css">
        <link href='http://fonts.googleapis.com/css?family=Comfortaa' rel='stylesheet' type='text/css'>
    </head>
    <body>

        <div class="header">
            <a href="/">Homework</a>
        </div>

        <?php if(!empty($message)): ?>
            <p><?= $message ?></p>
        <?php endif; ?>

        <h1>Register</h1>
        <span>or <a href="login.php">login here</a></span>

        <form action="register.php" method="POST">

            <input type="text" placeholder="Enter your email" name="email">
            <input type="password" placeholder="and password" name="password">
            <input type="password" placeholder="confirm password" name="confirm_password">
            <input type="submit">

        </form>

    </body>
    </html>

database.php (if needed):

<?php
$server = 'localhost';
$username = 'my_username';
$password = 'my_password';
$database = 'my_dbname';

try{
    $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
    die( "Connection failed: " . $e->getMessage());
}

Database is working fine. Not getting errors.

Can someone explain what is going wrong with registration?

  • 写回答

1条回答 默认 最新

  • doubi2145 2018-06-16 15:48
    关注

    When passing a value to bind_param(), you need to pass a variable and not the return value from a function...

    $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
    

    to...

    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
    $stmt->bindParam(':password', $password);
    

    When you see a parameter with a & in front of it as in &$variable in...

    public bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )

    You need to pass a variable.

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

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊