dsdt66064367 2017-03-11 15:53
浏览 270
已采纳

不能对表达式的结果使用isset()(可以使用“null!== expression”代替)[复制]

I'm making a form using PHP OOP and now I have faced a problem which is this: Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) on line 3

Basically this is the index.php which contains the html form and PHP action:

<?php
session_start();
if(isset(($_POST['login']))
{
 include 'class.login.php';
 $login = new Login();
 if($login->isLoggedIn()){
    echo "Success";
 }else{
    $login->showErrors();
 }
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" class="user" name="username" placeholder="Username">
<input type="password" class="lock" name="password" placeholder="Password">
<div class="forgot-top-grids">
    <div class="forgot-grid">
        <ul>
            <li>
                <input type="checkbox" id="brand1" value="">
                <label for="brand1"><span></span>Remember me</label>
            </li>
        </ul>
    </div>
    <div class="forgot">
        <a href="forgotpass.php">Forgot password?</a>
    </div>
    <div class="clearfix"> </div>
    </div>
    <input type="hidden" name="token" value="<? $token; ?>">
    <input type="submit" name="login" value="Log In">
</form>

I wonder whats going on with this error cause everything's looks fine. And also here's the login.class.php but I don't think the problem is related to this:

    <?php 
class Login{
    private $_id;
    private $_username;
    private $_password;
    private $_passmd5;
    private $_errors;
    private $_access;
    private $_login;
    private $_token;

    public function __construct(){
        $this->_errors = array();
        $this->_login = isset($_POST['login']) ? 1 : 0;
        $this->_access = 0;
        $this->_token = $_POST['token'];
        $this->_id      = 0;
        $this->_username= ($this->_login) ? $this->filter($_POST['username']) : $_SESSION['username'];
        $this->_password= ($this->_login) ? $this->filter($_POST['password']) : '';
        $this->_passmd5= ($this->_login) ? md5($this->_password) : $_SESSION['password'];
    }
    public function isLoggedIn(){
        ($this->_login) ? $this->verifyPost() : $this->verifySession();
        return $this->_access;
    }
    public function filter($var){
        return preg_replace('/[^a-zA-z0-9]/','',$var);
    }
    public function verifyPost(){
        try{
            if(!$this->isTokenValid())
                throw new Exception("Invalid Form Submission");

            if(!$this->isDataValid())
                throw new Exception("Invalid Form Data");

            if(!$this->verifyDatabase())
                throw new Exception("Invalid Username/Password");

            $this->_access = 1;
            $this->registerSession();
        }
        catch(Exception $e){
            $this->_errors[] = $e->getMessage();
        }
    }
    public function verifySession(){
        if($this->sessionExist() && $this->verifyDatabase())
            $this->_access = 1;
    }
    public function verifyDatabase(){
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("example") or die(mysql_error());
        $data = mysql_query("SELECT id FROM users WHERE username = '($this->_username)' AND password = '($this->_passmd5)'");
        if(mysql_num_rows($data)){
            list($this->_id) = @array_values(mysql_fetch_assoc($data));
            $row = mysql_fetch_assoc($data);
            $this->_id = $row['id'];
        }else{
            return false;
        }
    }
    public function isDataValid(){
        return (preg_match('/^[a-zA-z0-9](5,12)$/',$this->_username) && preg_match('/^[a-zA-z0-9](5,12)$/',$this->_password)) ? 1:0;
    }
    public function isTokenValid(){
        return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token']) ? 0 : 1;
    }
    public function registerSession(){
        $_SESSION['id'] = $this->_id;
        $_SESSION['username'] = $this->_username;
        $_SESSION['password'] = $this->_passmd5;
    }
    public function sessionExist(){
        return (isset($_SESSION['username']) && isset($_SESSION['password'])) ? 1 : 0; 
    }
    public function showErrors(){
        echo "
            <h3>Errors</h3>
        ";

        foreach($this->_errors as $key=>$value)
            echo $value."</br>";
    }
}
?>
</div>
  • 写回答

1条回答 默认 最新

  • doubi1624 2017-03-11 15:56
    关注

    if(isset(($_POST['login'])) should be if(isset($_POST['login'])).
    There was one ( too much in line 3.

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

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示