Reputation: 1400
I am getting error:
Notice: Undefined variable: _SESSION in C:\xampp\htdocs\vts\include\functions.php on line 53
while accessing the session variable. Below is my code:
<?php
include_once('include/config.php');
class User
{
//Database connect
public function __construct(){
$db = new DB_Class();
}
//Registration process
public function register_user($fname, $lname, $username, $email, $password){
$password = md5($password);
$sql = mysql_query("SELECT u_id from vt_user WHERE u_username = '$username' or u_email = '$email'");
$no_rows = mysql_num_rows($sql);
if ($no_rows == 0){
$result = mysql_query("INSERT INTO vt_user(u_fname, u_lname, u_username, u_email, u_password) values ('$fname', '$lname', '$username', '$email','$password')") or die(mysql_error());
return $result;
}else{
return FALSE;
}
}
// Login process
public function check_login($emailusername, $password)
{
$password = md5($password);
$result = mysql_query("SELECT u_id from vt_user WHERE u_email = '$emailusername' and u_password = '$password'");
$user_data = mysql_fetch_array($result);
$no_rows = mysql_num_rows($result);
if ($no_rows == 1){
$_SESSION['login']=TRUE;
$_SESSION['uid'] = $user_data['uid'];
return TRUE;
}else{
return FALSE;
}
}
// Getting name
public function get_session_details()
{
if(isset($_SESSION['login'])){
$id = $_SESSION['uid'];
$result = mysql_query("SELECT u_id, u_fname, u_lname, u_username, u_email from vt_user where u_id = $id");
$user_data = mysql_fetch_array($result);
echo "Hello ".$user_data['name'];
}
}
// Getting session
public function get_session()
{
return $_SESSION['uid'];
}
// Logout
public function user_logout()
{
$_SESSION['login'] = FALSE;
session_destroy();
}
}
?>
Is there any other way to do this? How can I access this variable in get_session()
function?
Upvotes: 1
Views: 826
Reputation: 68476
You get this Notice: Undefined variable: _SESSION
error because you have not added session_start();
on the top of your PHP code.
Add like this..
<?php
session_start();//<----------- Add here
include_once('include/config.php');
class User
{
//... your rest of the code
Secondary Sidenote : This (mysql_*
) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi
or PDO_MySQL
extension should be used.
Upvotes: 2