Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z sesja - Object of class User could not be converted to string.
rahul
post
Post #1





Grupa: Zarejestrowani
Postów: 71
Pomógł: 0
Dołączył: 5.03.2011

Ostrzeżenie: (0%)
-----


Witam. Stawiajac swoje pierwsze kroki w php OOP nie obylo sie dlugo zanim natknalem sie na pierwszy powazniejszy problem.
Otoz robie klase ktora jest wywolywana na kazdej stronie/podstronie - jest to klasa do Logowania i sprawdzania sesji. Jednak gdy ustawiam po zalogoaniu nowa sesjie - $_SESSION['user'] = $user_id; przy jakiejkolwiek probie wyechowania tej sesji dostaje blad "Object of class User could not be converted to string." Jest jeden wyjatek, moge ja wyechowac w funkcji w ktorej ja ustawiam. Tylko tyle. Probowalem troche z serializacja, ale nie wiem czy sie dobrze za to zabieralem.

Oto kod:


$user = new User;
$user_id = $user -> login(@$_POST['user_name'] , @$_POST['user_password']);
$status = $user -> checkCurrentSession();


class User {
function __construct() {
session_start();
}

function checkCurrentSession() {

if ( isset($_SESSION['user'])) {

echo $_SESSION['user']; // NO I TU DUPA :/
}else{

return false;
}

}

function login($user, $password) {

if ( isset ($user) && isset ($password) ) {
$user_id = $this -> checkLoginData($user, $password);

if( $user_id != false) {

$_SESSION['user'] = $user_id;


}else{

echo "wrong user";
}

}

}

function checkLoginData($user,$password) {

// conecting to the database.
// check for user id and return it.

// hardCoded validation ->
if ($user == "dupa" && $password == "dupa") {


return $user;

}elseif ($user == "admin" && $password =="admin" ) {


return $user;

}else {

return false;



}
}

function logout () {


if ( isset ($_SESSION['user']) ) {

$_SESSION['user'];
unset($_SESSION['user']);
session_destroy();
echo "sucesfully loged out";
}else{

echo "Sesion does not exist. Error.";

}
}

}
Dodatkowo dodam, przechodzac na strone admina - sprawdzanie typu if ( $_SESSION['user'] == "admin") tez nie dziala :/ Wtf ?!
Help ?!

Ten post edytował rahul 7.03.2011, 08:45:37
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 23:44