Redirection avec la fonction header
Bonsoir,
j'ai écrit ce script qui me permet de récupérer les données de connexion de l'utilisateur depuis la base, les stocker dans une variable de session puis de le rediriger sur une autre page.
Malheureusement, et je ne comprends pas pourquoi, ce script refuse de s'exécuter correctement!!
Voici les messages d'erreur :
Code:
1 2 3 4 5 6
|
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/obit/projet/pelican/core/Database.class.php:138) in /home/obit/projet/pelican/core/login.php on line 9
Warning: Cannot modify header information - headers already sent by (output started at /home/obit/projet/pelican/core/Database.class.php:138) in /home/obit/projet/pelican/core/login.php on line 11
Warning: Cannot modify header information - headers already sent by (output started at /home/obit/projet/pelican/core/Database.class.php:138) in /home/obit/projet/pelican/core/login.php on line 15 |
Voici le script que je lance :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?php
include_once 'User.class.php';
if(!empty($_POST['login']) && !empty($_POST['password'])) {
$conn = Database::getInstance();
if($conn) {
$user = User::getUser($_POST['login'], $_POST['password']);
if($user) {
session_start();
$_SESSION['user'] = serialize($user);
header('Location:home.php');
}
}
}
header('Location:../htm/index.htm');
?> |
Voici le script 'User.class.php' :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| <?php
include_once 'Database.class.php';
class User {
private $id;
private $login;
private $password;
private $role;
private $ip;
public function __construct($i='', $l='', $p='', $r='', $ip='') {
$this->id = $i;
$this->login = $l;
$this->password = $p;
$this->role = $r;
$this->ip = $ip;
}
public static function getUser($log, $pwd) {
$conn = Database::getInstance();
if($conn) {
$res = $conn->execQuery(marequetefonctionne);
if(pg_num_rows($res) == 1) {
$row = pg_fetch_array($res, NULL, PGSQL_ASSOC);
$user = new User(jajoutebienmeschampsaussi);
if($user) {
pg_free_result($res);
return $user;
}
}
pg_free_result($res);
}
return NULL;
}
public function toString() {
return "{".$this->id.";".$this->login.";".$this->password.";".$this->role.";".$this->ip."}";
}
public function setId($id) {
$this->id = $id;
}
public function setLogin($login) {
$this->login = $login;
}
public function setPassword($pass) {
$this->password = $password;
}
public function setRole($r) {
$this->role = $r;
}
public function setIp($ip) {
$this->ip = $ip;
}
public function getId() {
return $this->id;
}
public function getLogin() {
return $this->login;
}
public function getPassword() {
return $this->password;
}
public function getRole() {
return $this->role;
}
public function getIp() {
return $this->ip;
}
}
?> |
et voici le dernier script qu'il vous manque pour avoir tous les éléments :)
Database.class.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| <?php
include_once '../lib/php/defines.php';
class Database {
private $host;
private $port;
private $dbName;
private $user;
private $password;
private $lastQuery;
private $connexion;
private static $_instance;
/* Constructeur */
public function __construct($h, $db, $u, $p) {
$this->host = $h;
$this->dbName = $db;
$this->user = $u;
$this->password = $p;
$this->_instance = $this;
}
public static function getInstance() {
if (!isset(self::$_instance)) {
$c = __CLASS__;
self::$_instance = new $c(PG_HOST, PG_DBNAME, PG_USER, PG_PASSWORD);
}
return self::$_instance;
}
public function showParams() {
echo "host=".$this->host." dbname=".$this->dbName." user=".$this->user." password=".$this->password;
}
public function connect() {
try {
$c = "host=".$this->host." dbname=".$this->dbName." user=".$this->user." password=".$this->password;
$cnx = pg_connect( $c );
$this->connexion = $cnx;
} catch(Exception $e) {
throw new Exception(pg_last_error());
}
}
public function execQuery($query) {
if(!$this->connexion)
$this->connect();
try {
return pg_query($query);
} catch(Exception $e) {
throw new Exception(pg_last_error());
}
}
public function close() {
if($this->connexion)
pg_close($this->connexion);
}
public function setParams($h, $db, $u, $p) {
self::setHost($h);
self::setDbName($db);
self::setUser($u);
self::setPassword($p);
}
public function setHost($h) {
if($h != '')
$this->host = $h;
}
public function setPort($p) {
if($p != '')
$this->port = $p;
}
public function setDbName($dn) {
if($dn != '')
$this->dbName = $dn;
}
public function setUser($u) {
if($u != '')
$this->user = $u;
}
public function setPassword($p) {
if($p != '')
$this->password = $p;
}
public function setLastQuery($q) {
if($q != '')
$this->lastQuery = $q;
}
public function setConnexion($c) {
$this->connexion = $c;
}
public function getHost() {
return $this->host;
}
public function getPort() {
return $this->port;
}
public function getDbName() {
return $this->dbName;
}
public function getUser() {
return $this->user;
}
public function getPassword() {
return $this->password;
}
public function getLastQuery() {
return $this->lastQuery;
}
public function getConnexion() {
return $this->connexion;
}
}
?> |
Petit ajout du script defines.php :
Code:
1 2 3 4 5 6 7
| <?php
define ('PG_HOST', 'localhost');
define ('PG_PORT', '5432');
define ('PG_DBNAME', 'db_test');
define ('PG_USER', 'user');
define ('PG_PASSWORD', 'password');
?> |
Merci beaucoup à celles et ceux qui prendront le temps de m'aider!
:)