Search the Community
Showing results for tags 'pdo session'.
-
Hey, I'm trying to echo out user info based on user $_SESSION['user_id'], but session is not set, it says "Undefined index: user_id". Login works, but user_id is not set. Class class User { public $uid = ""; public function userInfo($user_id) { global $db; $query = $db->prepare("SELECT `user_id`, `username`, `email` FROM `users` WHERE `user_id` = :id"); $query->bindValue(':id', $user_id); $query->execute(); return $query->fetch(); } public function login($login, $password){ global $db; $query = $db->prepare("SELECT COUNT(*) FROM `users` WHERE `username` = :user AND `password` = :pass OR `email` = :user AND `password` = :pass"); $query->bindValue(':user', $login); $query->bindValue(':pass', $password); $query->execute(); $result = (bool) $query->fetchColumn(0); if($result) { $this->uid = $result['user_id']; } return $result; } } Login <?php if (isset($_POST['login'], $_POST['password'])) { $login = $_POST['login']; $password = sha1($_POST['password']); $errors = array(); if (empty($login) || empty($password)) { $errors[] = 'All fields required!'; } else { $user = new User; $log_in = $user->login($login, $password); if ($log_in) { $_SESSION['user_id'] = $user->uid; header('Location: account.php'); exit(); } else { $errors[] = 'Username/Email or password incorrect!'; } } if (!empty($errors)){ foreach ($errors as $error) { echo '<div id="error"><strong>', $error, '</strong></div><br />'; } } } ?> Account page $user_id = $_SESSION['user_id']; $user = new User; $data = $user->userInfo($user_id); echo $data['username'],'<br />'; echo $data['email'],'<br />'; echo '<pre>'; print_r($_SESSION['user_id']); echo '</pre>'; if(isset($_SESSION['user_id'])){ $user_id = $_SESSION['user_id']; $data = $user->userInfo($user_id); echo '<pre>'; print_r($data); echo '</pre>'; echo $data['username'],'<br />'; }else { echo 'bla'; }