Fidel Castro Fidel Castro - 7 days ago 5
PHP Question

assigning a variable, but keep getting NULL

I'm working on a project, im trying to assign two variables but I keep getting NULL when i var_dump() the variables

my code:

User.php

<?php

class TheUser
{
public $isMember = 0;
public $userID = 0;
public $info = [];
public $user_name = 'guest';

public function __construct()
{
global $mysqli,$config;
if($_SESSION['the_user'] &&mysqli_num_rows($query = mysqli_query($mysqli,'SELECT * FROM `users` WHERE `username` = \''.$_SESSION['the_user'].'\' '))) {
{
$this->info = mysqli_fetch_assoc($query);
$this->user_name = $this->info['username'];
$this->isMember = 1;
$this->userID = $this->info['id'];
}
}}



function LoginUser()
{
global $mysqli,$config,$_SESSION;

if($_SESSION['the_user']) {die('go away');}
if(!$_POST['username']) { die('username already exist'); }
if(!$_POST['password']) { die('password is required...'); }

if(!mysqli_num_rows($query = mysqli_query($mysqli,'SELECT `id`, `username`, `email`, `password` FROM `users` WHERE `username` = \''.$_POST['username'].'\''))) { die('0: El usuario ingresado no existe'); }
$r = mysqli_fetch_row($query);
if($r[3] != $_POST['password']) { die('0: La contrase&ntilde;a es incorrecta'); }


//if($r[3] != $_POST['password']){ die(' incorrect password'); }
$_SESSION['the_user'] = $r[1];
$theUser = $_SESSION['the_user'];
$this->isMember = 1;
$this->userID = $r[0];
die('ok');




}





}


then my controller look like:

<?php
include __DIR__.'/../../header.php';
$templateToShow = 'login';

$WhatLevel = 0;

$isAjax = empty($_GET['ajax']) ? 0 : 1;

$KeepGoing = true;


$PageTitle = $config['title'];

if($_POST)
{
$ala = $LU->LoginUser();
}
//session_destroy();
var_dump($_SESSION['the_user']);
var_dump($isMember);
var_dump($userID);


if(empty($isAjax) && !$templateToShow)
{
echo $twig->render('index.twig.php');
}else{
echo $twig->render("$templateToShow.twig.php",[

]);
}


I know there is no security implemented YET... I'm just doing it this way first so I can fix it later.

What I'm trying to do is to

assign $isMember to 1

assign $userID to the user id that i got from the database

I do not know why but I keep getting this :

string(6) "admin" int(0) int(0)
I'm sorry if i do not make sense, but it is 3 am and im so frustrated :/

Answer

In your code you used $_SESSION, so you have to add session_start() at the begin of each php files. And you want to use variable in other class of another file, so you have to include that file and with this code,

$user = new TheUser;
$user->$isMember
Comments