Nikola Atanasov
Nikola Atanasov

Reputation: 573

$_SESSION variables wont take values

Im building a Login function, and for some reason my $_SESSION variable dont get any values, and if they do, i try to echo them, but they wont show.

This is where i'm trying to set them:

function login()
{
$db=new Database();
session_start();
if(isset($_POST['login'])){
$username=$_POST['username'];
$password=md5($_POST['password']);

$query=("SELECT * FROM user
        WHERE username = '$username'
        AND password = '$password'");
$row=$db->select($query);
if(mysqli_num_rows($row)==1)
{
  $_SESSION['is_logged_in'] = true;
  $_SESSION['id'] = $row['id'];
  $_SESSION['username'] = $row['username'];
  $_SESSION['name'] = $row['name'];
  header('Location: index.php');
 }
}
}

And in my index.php i only have session_start(); and echo $_SESSION['username'];

What could be causing those errors?

Am i using them wrong? Am i using the $query variable wrong?

Any help is greatly apreciated

Edit: This is my config.php:

<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','PHPWizard');
?>

And this is my Database.php:

<?php
class Database{
public $host = DB_HOST;
public $username = DB_USER;
public $password = DB_PASS;
public $db_name = DB_NAME;

public $link;
public $error;

/*
 * Class Constructor
 */
public function __construct(){
    //Call Connect Function
    $this->connect();
}

/*
 * Connector
 */
 private function connect(){
    $this->link = new mysqli($this->host, $this->username, $this->password, $this->db_name);

    if(!$this->link){
        $this->error = "Connection Failed: ".$this->link->connect_error;
        return false;
    }
 }

 /*
  * Select
  */
  public function select($query){
    $result = $this->link->query($query) or die($this->link->error.__LINE__);
    if($result->num_rows > 0){
        return $result;
    } else {
        return false;
    }
  }

  /*
   * Insert
   */
   public function insert($query){
        $insert_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($insert_row){
            header("Location: index.php?msg=".urlencode('Record Added'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

   /*
   * Update
   */
   public function update($query){
        $update_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($update_row){
            header("Location: index.php?msg=".urlencode('Record Updated'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

    /*
   * Delete
   */
   public function delete($query){
        $delete_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($delete_row){
            header("Location: index.php?msg=".urlencode('Record Deleted'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

}

Could my connection be the reason why the variables dont get any values?

Upvotes: 3

Views: 72

Answers (1)

CJ Nimes
CJ Nimes

Reputation: 658

Before using $row as an array you have to fetch the data:

This:

$row = $db->select($query);
$_SESSION['id'] = $row['id'];

Should be:

$result = $db->select($query);
if ($result) {
    $row = $result->fetch_assoc();
    $_SESSION['id'] = $row['id'];
}

More info: http://php.net/manual/es/mysqli-result.fetch-assoc.php

Upvotes: 1

Related Questions