PHP - UserRole - Function - OOP -


i trying make own custom cms, can register users , can login aswel, trying make function user roles,

file: class.user.php

function getuserrole() {     $username = htmlentities($_session['user_session']);     $stmt = $this->db->prepare('select * users user_name = :username');     $stmt->bindparam(':user_name', $username);     $stmt->execute();      $row = $stmt->fetch(pdo::fetch_assoc);      $userrole = $row['user_role'];      if($userrole == 3) {         return $userrole = 3;     }      if($userrole == 2) {         return $userrole = 2;     }      if($userrole == 1) {         return $userrole = 1;     }      if($userrole == 0) {         return $userrole = 0;     } }  

file: home.php

<?php      $userrole = getuserrole();     if($userrole == 1) {         echo "hi admin";     }      else {         echo "you not admin";     } ?> 

when try this, error shows up:

fatal error: call undefined function getuserrole() in /applications/mamp/htdocs/test/home.php on line 24 

i can see wrong , hoping guys me out here:)

entire class.user.php :

<?php class user {     private $db;      function __construct($db_con)     {         $this->db = $db_con;     }      public function register($uname,$umail,$upass)     {         try         {             $new_password = password_hash($upass, password_default);              $stmt = $this->db->prepare("insert users(user_name,user_email,user_pass)                                                         values(:uname, :umail, :upass)");              $stmt->bindparam(":uname", $uname);             $stmt->bindparam(":umail", $umail);             $stmt->bindparam(":upass", $new_password);                                                      $stmt->execute();                 return $stmt;            }         catch(pdoexception $e)         {             echo $e->getmessage();         }                    }      public function login($uname,$umail,$upass)     {         try         {             $stmt = $this->db->prepare("select * users user_name=:uname or user_email=:umail limit 1");             $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));             $userrow=$stmt->fetch(pdo::fetch_assoc);             if($stmt->rowcount() > 0)             {                 if(password_verify($upass, $userrow['user_pass']))                 {                     $_session['user_session'] = $userrow['user_id'];                     return true;                 }                 else                 {                     return false;                 }             }         }         catch(pdoexception $e)         {             echo $e->getmessage();         }     }      public function is_loggedin()     {         if(isset($_session['user_session']))         {             return true;         }     }      public function redirect($url)     {         header("location: $url");     }      public function logout()     {         session_destroy();         unset($_session['user_session']);         return true;     }   function getuserrole() {     $username = htmlentities($_session['user_session']);     $stmt = $this->db->prepare('select * users user_name = :username');     $stmt->bindparam(':user_name', $username);     $stmt->execute();      $row = $stmt->fetch(pdo::fetch_assoc);      $userrole = $row['user_role'];      if($userrole == 3) {         return $userrole = 3;     }      if($userrole == 2) {         return $userrole = 2;     }      if($userrole == 1) {         return $userrole = 1;     }      if($userrole == 0) {         return $userrole = 0;     } }   } ?> 

require class within home.php, init , call function

<?php   require_once 'class.user.php'; $userclass = new user(<yourdbcon>);  $userrole = $userclass->getuserrole(); if($userrole == 1) {     echo "hi admin"; }  else {     echo "you not admin"; } 

?>


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -