php - Session under class -
hello stuck in syntax, can me next code?
class user { protected $userid = preg_replace('#[^0-9]#i', '', $_session['user_id']); protected $useremail = preg_replace('#[^a-za-z0-9@_.-]#i', '', $_session['user']); protected $userpassword = preg_replace('#[^a-za-z0-9]#i', '', $_session['user_password']); public function checkuserlogin(){ if(!isset($_session['user'])){ header("location: login.php"); exit(); } //try find user session data in database $sql = "select * users id = '$this->userid' , email = '$this->useremail' , password = '$this->userpassword' limit 1"; $res = mysql_query($sql) or die(mysql_error()); $usermatch = mysql_numrows($res); if ($usermatch == 0) { header("location: login.php"); exit(); } } }
first of all, note when declaring property, cannot assign value that's not know @ compile-time -- means cannot call function initialize property.
this code :
protected $userid = preg_replace('#[^0-9]#i', '', $_session['user_id']);
is not valid.
reference, can read properties page of manual (quoting relevant sentence) :
they defined using 1 of keywords
public
,protected
, orprivate
, followed normal variable declaration.
this declaration may include initialization, this initialization must constant value -- is, must able evaluated @ compile time , must not depend on run-time information in order evaluated.
you should first declare property ; and, later (in constructor of class, instance), initialize :
class user { protected $userid; public function __construc() { $this->userid = preg_replace('#[^0-9]#i', '', $_session['user_id']); } }
Comments
Post a Comment