Cannot Start a Session with Class

Sunday, May 11, 2014

I create a class called login.class.php



<?php
class Login
{

public $errors = array();
public $messages = array();

private $db_connection = null;
private $username = '';
private $password = '';
private $logged_in = false;

public function __construct()
{
session_start();
if (isset($_GET["logout"]))
{
$this->doLogout();
}elseif(isset($_POST['login']))
{
$this->doLogin($_POST['username'],$_POST['password']);
}
}

// KONEKSI KE DATABASE
private function dbConn()
{
if ($this->db_connection != null)
{
return true;
}else{
try
{
$this->db_connection = new PDO('mysql:host='.HOST.';dbname='.NAME.';charset=utf8', USER, PASS);
return true;
}catch (PDOException $e){
$this->errors[] = "Terdapat kesalahan : ".$e->getMessage();
}
}
return false;
}

// METHOD LOGIN
public function doLogin($username, $password)
{
if ($this->dbConn())
{
$query_user = $this->db_connection->prepare('SELECT * FROM login WHERE username = ? LIMIT 1');
$query_user->bindValue(1, $username, PDO::PARAM_INT);
$query_user->execute();

$hasil = $query_user->fetchObject();
}

if(!isset($hasil->username))
{
$this->errors[] = "Username tidak terdaftar";
}elseif($hasil->password != md5($password)){
$this->errors[] = "Password yang Anda masukkan salah";
}else{
$_SESSION['username'] = $hasil->username;
$_SESSION['logged_in'] = true;

$this->logged_in = $_SESSION['logged_in'];
$this->username = $hasil->username;
$this->password = $hasil->password;
}
}

public function doLogout()
{
$_SESSION = array();
session_destroy();
$this->logged_in = false;
header('Location: index.php');
}

public function UserLoggedIn()
{
return $this->logged_in;
}
} ?>


and then I create two other pages called login.php



<?php

require_once 'includes/config.php';
require_once 'classes/login.class.php';

$login = new Login();

if ($login->UserLoggedIn() == true) {
header('Location: admin.php');
} ?>

<form action="" method="post">

<h1>Halaman Login</h1>

<div class="login-fields">
<?php
if(isset($login))
{
if($login->errors)
{
foreach($login->errors as $error) { echo"$error</p>"; }
}else{
echo'<p>Masukkan detail</p>';
}
}
?>
<div class="field">
<label for="username">Username</label>
<input type="text" id="username" name="username" value="" placeholder="Username" class="login username-field" required>
</div> <!-- /field -->

<div class="field">
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="" placeholder="Password" class="login password-field" required>
</div> <!-- /password -->

</div> <!-- /login-fields -->

<div class="login-actions">
<button type="submit" name="login" class="button btn btn-success btn-large">Masuk</button>

</div> <!-- .actions -->
</form>


and admin.php:



<?php
error_reporting(0);
require_once 'includes/config.php';
require_once 'classes/login.class.php';
$login = new Login();

if ($login->UserLoggedIn() == true) {
//PROTECTED PAGE HERE
}
?>


But the sessions were has been saved by doLogin() in login.class.php is gone.


I am sorry with my bad exlpanation, I hope you know what I mean.







http://ift.tt/1iDpZDk