Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z sesja - Object of class User could not be converted to string.
Forum PHP.pl > Forum > PHP
rahul
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 ?!
nospor
zamiast
echo $_SESSION['user'];
daj:
var_dump($_SESSION['user']);

To może doznasz olśnienia. Pomijam już fakt, że komunikat błędu podaje ci wszystko jak na tacy
gizmo1989
Mam podobny problem z tym, że na serwerze nazwa.pl działa poprawnie i dump zwraca mi string(2)->pl
natomiast na innym serwerze zagranicznym dostaję takie coś....

  1. object(Language)#6 (3) { ["db"]=> &object(DB)#7 (3) { ["error"]=> NULL ["conn"]=> resource(10) of type (mysql link) ["utils"]=> object(Utils)#8 (1) { ["configSettings"]=> array(11) { ["language"]=> array(3) { [0]=> string(2) "pl" [1]=> string(2) "en" [2]=> string(2) "de" } ["db_user"]=> string(6) "XXX" ["db_pass"]=> string(8) "XXX" ["db_host"]=> string(9) "localhost" ["db_db"]=> string(12) "XXX" ["base_url"]=> string(18) "XXX" ["base_dir"]=> string(32) "XXX" ["photo_blog"]=> string(13) "XXX" ["photo_banner"]=> string(15) "XXX" ["photo_company"]=> string(16) "XXX" ["photo_product"]=> string(16) "XXX" } } } ["message"]=> NULL ["languageList"]=> array(5) { [0]=> array(2) { ["name"]=> string(2) "pl" ["visible"]=> string(1) "1" } [1]=> array(2) { ["name"]=> string(2) "en" ["visible"]=> string(1) "1" } [2]=> array(2) { ["name"]=> string(2) "de" ["visible"]=> string(1) "1" } [3]=> array(2) { ["name"]=> string(2) "it" ["visible"]=> string(1) "1" } [4]=> array(2) { ["name"]=> string(2) "fr" ["visible"]=> string(1) "1" } } }


W miejsce XXX znajdują się dane, których nie mogę podać.W zasadzie nie wiem nawet czemu je dumpuje!Znajdują się one w innej klasie.
Czy to możliwe, że to wina serwera albo jego konfiguracji.Jeśli tak to jak mogę to zfixować np. w php.ini, htaccessie?
qrzysztof
Zrób tak:

  1. echo '<pre>';
  2. var_dump(...);
  3. echo '</pre>';


Będzie czytelniej. Prawdopodobnie obiekt języka zawiera odwołanie do obiektu db, który zarządza bazą danych.
gizmo1989
  1. object(Language)#5 (3) {
  2. ["db"]=>
  3. &object(DB)#6 (3) {
  4. ["error"]=>
  5. NULL
  6. ["conn"]=>
  7. resource(10) of type (mysql link)
  8. ["utils"]=>
  9. object(Utils)#7 (1) {
  10. ["configSettings"]=>
  11. array(11) {
  12. ["language"]=>
  13. array(3) {
  14. [0]=>
  15. string(2) "pl"
  16. [1]=>
  17. string(2) "en"
  18. [2]=>
  19. string(2) "de"
  20. }
  21. ["db_user"]=>
  22. string(6) "XXX"
  23. ["db_pass"]=>
  24. string(8) "XXX"
  25. ["db_host"]=>
  26. string(9) "XXX"
  27. ["db_db"]=>
  28. string(12) "XXX"
  29. ["base_url"]=>
  30. string(18) "XXX"
  31. ["base_dir"]=>
  32. string(32) "XXX"
  33. ["photo_blog"]=>
  34. string(13) "XXX"
  35. ["photo_banner"]=>
  36. string(15) "XXX"
  37. ["photo_company"]=>
  38. string(16) "XXX"
  39. ["photo_product"]=>
  40. string(16) "XXX"
  41. }
  42. }
  43. }
  44. ["message"]=>
  45. NULL
  46. ["languageList"]=>
  47. array(5) {
  48. [0]=>
  49. array(2) {
  50. ["name"]=>
  51. string(2) "pl"
  52. ["visible"]=>
  53. string(1) "1"
  54. }
  55. [1]=>
  56. array(2) {
  57. ["name"]=>
  58. string(2) "en"
  59. ["visible"]=>
  60. string(1) "1"
  61. }
  62. [2]=>
  63. array(2) {
  64. ["name"]=>
  65. string(2) "de"
  66. ["visible"]=>
  67. string(1) "1"
  68. }
  69. [3]=>
  70. array(2) {
  71. ["name"]=>
  72. string(2) "it"
  73. ["visible"]=>
  74. string(1) "1"
  75. }
  76. [4]=>
  77. array(2) {
  78. ["name"]=>
  79. string(2) "fr"
  80. ["visible"]=>
  81. string(1) "1"
  82. }
  83. }
  84. }


No dokładnie, ale czemu na nazwa.pl zachowuje się to inaczej niż na zagranicznym serwerze.

  1. $path = dirname(__FILE__);
  2.  
  3. define ('LANGUAGE_FILE_PATH', $path.'/../../includes/language/all.php');
  4.  
  5.  
  6.  
  7. include_once('Baza.class.php');
  8.  
  9. include_once(LANGUAGE_FILE_PATH);

To mam na początku klasy language.
qrzysztof
Reszta klasy Language byłaby chyba ciekawsza.

Bo z tego co widzę, to żeby otrzymać "pl" musiałbyś się odwołać do

  1. $obiektLanguage->languageList[0]->name;


Nie przesądzam bo nie znam kodu tej klasy ani nie wiem do czego służy.
gizmo1989
  1. class Language {
  2.  
  3. var $db;
  4.  
  5.  
  6.  
  7. /**
  8.  
  9. * konstruktor klasy
  10.  
  11. * inicjuje uchwyt do bazy
  12.  
  13. **/
  14.  
  15. function Language(){
  16.  
  17. $this->db =& new DB();
  18.  
  19. global $languageList;
  20.  
  21. $this->languageList = $languageList;
  22. echo $languagelist;
  23. }
  24.  
  25.  
  26.  
  27. /**
  28.  
  29. * count all user
  30.  
  31. * @param
  32.  
  33. * @return int
  34.  
  35. */
  36.  
  37. function getFileWithVariable($lang){
  38.  
  39. $likToFIle = fopen('./../includes/language/'.$lang.'.php', "r");
  40.  
  41. while($wiersz = @fgets($likToFIle, 1024))
  42.  
  43. {
  44.  
  45. $fileContent .= $wiersz;
  46.  
  47. }
  48.  
  49. fclose($likToFIle);
  50.  
  51. return $fileContent;
  52.  
  53. }
  54.  
  55.  
  56.  
  57. /**
  58.  
  59. * count all user
  60.  
  61. * @param
  62.  
  63. * @return int
  64.  
  65. */
  66.  
  67. function getAllVisibleLanguage(){
  68.  
  69. if(is_array($this->languageList) AND count($this->languageList)>0){
  70.  
  71. foreach ($this->languageList as $i => $x){
  72.  
  73. if($this->languageList[$i]['visible'] == 1){
  74.  
  75. $temp[] = $this->languageList[$i]['name'];
  76.  
  77. }
  78.  
  79. }
  80.  
  81. return $temp;
  82.  
  83. }
  84.  
  85. }
  86.  
  87.  
  88.  
  89. /**
  90.  
  91. * count all user
  92.  
  93. * @param
  94.  
  95. * @return int
  96.  
  97. */
  98.  
  99. function getAllLanguage(){
  100.  
  101. return $this->languageList;
  102.  
  103. }
  104.  
  105.  
  106.  
  107. /**
  108.  
  109. * get all user by range
  110.  
  111. * @param from
  112.  
  113. * @param range
  114.  
  115. * @return array of objects
  116.  
  117. */
  118.  
  119. function getAllUsersByLimit($from, $to){
  120.  
  121. $sql = "SELECT cms_user.*, (SELECT _inserted FROM cms_audit_login WHERE cms_audit_login.id_user = cms_user.id ORDER BY _inserted ASC LIMIT 0, 1 ) as last_login FROM cms_user ORDER BY first_name, last_name ASC LIMIT $from, $to ";
  122.  
  123. $tab = $this->db->query($sql);
  124.  
  125. return $tab;
  126.  
  127. }
  128.  
  129.  
  130.  
  131. /**
  132.  
  133. * write new user
  134.  
  135. * @param
  136.  
  137. * @return int
  138.  
  139. */
  140.  
  141. function writeNewLanguage(){
  142.  
  143. if (copy('./../includes/language/base.php', './../includes/language/temp_base.php')){
  144.  
  145. if(rename('./../includes/language/temp_base.php', './../includes/language/'.strtolower($_POST['formNazwa']).'.php'))
  146.  
  147. {
  148.  
  149. $newKey = end(array_keys($this->languageList))+1;
  150.  
  151. $this->languageList[$newKey]['name'] = $_POST['formNazwa'];
  152.  
  153. $this->languageList[$newKey]['visible'] = 0;
  154.  
  155.  
  156.  
  157. $this->writeToFile($this->languageList);
  158.  
  159.  
  160.  
  161. return true;
  162.  
  163. }
  164.  
  165. else
  166.  
  167. {
  168.  
  169. return false;
  170.  
  171. }
  172.  
  173. }
  174.  
  175. else
  176.  
  177. {
  178.  
  179. return false;
  180.  
  181. }
  182.  
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189. /**
  190.  
  191. * change language visible
  192.  
  193. * @language id
  194.  
  195. * @return bool result
  196.  
  197. */
  198.  
  199. function changeLanguageVisible($id){
  200.  
  201. if(array_key_exists($id, $this->languageList))
  202.  
  203. {
  204.  
  205. $this->languageList[$id]['visible']==1 ? $visible=0 : $visible=1;
  206.  
  207. $this->languageList[$id]['visible'] = $visible;
  208.  
  209. $this->writeToFile($this->languageList);
  210.  
  211. return true;
  212.  
  213. }
  214.  
  215. else
  216.  
  217. {
  218.  
  219. return false;
  220.  
  221. }
  222.  
  223. }
  224.  
  225.  
  226.  
  227. /**
  228.  
  229. * change language priority
  230.  
  231. * @param id
  232.  
  233. * @param where
  234.  
  235. * @param priority
  236.  
  237. */
  238.  
  239. function changeLanguagePriority($id, $where, $priority){
  240.  
  241. if($where=="up"){
  242.  
  243. $temp = $this->languageList[$id];
  244.  
  245. $this->languageList[$id] = $this->languageList[$id-1];
  246.  
  247. $this->languageList[$id-1] = $temp;
  248.  
  249. }
  250.  
  251. else{
  252.  
  253. $temp = $this->languageList[$id];
  254.  
  255. $this->languageList[$id] = $this->languageList[$id+1];
  256.  
  257. $this->languageList[$id+1] = $temp;
  258.  
  259. }
  260.  
  261. $this->writeToFile($this->languageList);
  262.  
  263. }
  264.  
  265.  
  266.  
  267. /**
  268.  
  269. * change language visible
  270.  
  271. * @language id
  272.  
  273. * @return bool result
  274.  
  275. */
  276.  
  277. function updateLanguage($id){
  278.  
  279. if(array_key_exists($id, $this->languageList))
  280.  
  281. {
  282.  
  283. unlink('./../includes/language/'.strtolower($_POST['formNazwa']).'.php');
  284.  
  285. touch ('./../includes/language/'.strtolower($_POST['formNazwa']).'.php'); //utworz plik jeżeli go nie ma
  286.  
  287. chmod ('./../includes/language/'.strtolower($_POST['formNazwa']).'.php',0666);
  288.  
  289.  
  290.  
  291. $this->languageList[$id]['visible'] = $_POST['formWidoczna'];
  292.  
  293. $this->writeToFile($this->languageList);
  294.  
  295.  
  296.  
  297. $linkToFile = fopen ('./../includes/language/'.strtolower($_POST['formNazwa']).'.php',"r+");
  298.  
  299. fwrite ($linkToFile, ""); //zapisujemy zawartosc
  300.  
  301. fwrite ($linkToFile, "<?php ".stripslashes($_POST['formZmienne'])." ?>"); //zapisujemy zawartosc
  302.  
  303. fclose ($linkToFile); //zamknij plik
  304.  
  305.  
  306.  
  307. return true;
  308.  
  309. }
  310.  
  311. else
  312.  
  313. {
  314.  
  315. return false;
  316.  
  317. }
  318.  
  319. }
  320.  
  321.  
  322.  
  323. /**
  324.  
  325. * change language visible
  326.  
  327. * @language id
  328.  
  329. * @return bool result
  330.  
  331. */
  332.  
  333. function deleteLanguage($id){
  334.  
  335. if(array_key_exists($id, $this->languageList))
  336.  
  337. {
  338.  
  339. unlink('./../includes/language/'.strtolower($this->languageList[$id]['name']).'.php');
  340.  
  341.  
  342.  
  343. $name = $this->languageList[$id]['name'];
  344.  
  345.  
  346.  
  347. $sql = "DELETE FROM cms_company_text WHERE language = '".$name."'";
  348.  
  349. $this->db->exec($sql);
  350.  
  351. $sql = "DELETE FROM cms_photo_text WHERE language = '".$name."'";
  352.  
  353. $this->db->exec($sql);
  354.  
  355. $sql = "DELETE FROM cms_post_text WHERE language = '".$name."'";
  356.  
  357. $this->db->exec($sql);
  358.  
  359. $sql = "DELETE FROM cms_product_text WHERE language = '".$name."'";
  360.  
  361. $this->db->exec($sql);
  362.  
  363. $sql = "DELETE FROM cms_wysiwyg_text WHERE language = '".$name."'";
  364.  
  365. $this->db->exec($sql);
  366.  
  367.  
  368.  
  369. unset($this->languageList[$id]);
  370.  
  371.  
  372.  
  373. if(is_array($this->languageList) AND count($this->languageList)>0){
  374.  
  375. foreach ($this->languageList as $i => $x){
  376.  
  377. $temp[] = $this->languageList[$i];
  378.  
  379. }
  380.  
  381. }
  382.  
  383.  
  384.  
  385. $this->writeToFile($temp);
  386.  
  387. return true;
  388.  
  389. }
  390.  
  391. else
  392.  
  393. {
  394.  
  395. return false;
  396.  
  397. }
  398.  
  399. }
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407. /**
  408.  
  409. * write language into file
  410.  
  411. * @language tab
  412.  
  413. * @return
  414.  
  415. */
  416.  
  417. function writeToFile($tab){
  418.  
  419. $stringConntent = "<?php\n";
  420.  
  421. $stringConntent .= "/* ----------------- lista dostępnych języków */\n";
  422.  
  423.  
  424.  
  425. if(is_array($tab) AND count($tab)>0){
  426.  
  427. foreach ($tab as $i => $x){
  428.  
  429. $stringConntent .= "$"."languageList"."[".$i."]['name'] = \"".$tab[$i]['name']."\";\n";
  430.  
  431. $stringConntent .= "$"."languageList"."[".$i."]['visible'] = \"".$tab[$i]['visible']."\";\n";
  432.  
  433. }
  434.  
  435. }
  436.  
  437.  
  438.  
  439. $stringConntent .= "?>";
  440.  
  441.  
  442.  
  443. touch (LANGUAGE_FILE_PATH); //utworz plik jeżeli go nie ma
  444.  
  445. chmod (LANGUAGE_FILE_PATH,0666);
  446.  
  447. $linkToFile = fopen (LANGUAGE_FILE_PATH,"r+") or die("Nie mogę otworzyć pliku ".LANGUAGE_FILE_PATH); //otwórz plik
  448.  
  449. flock ($linkToFile,2);
  450.  
  451. ftruncate($linkToFile,0);
  452.  
  453. rewind($linkToFile);
  454.  
  455. fwrite ($linkToFile, $stringConntent); //zapisujemy zawartosc
  456.  
  457. fclose ($linkToFile); //zamknij plik
  458.  
  459.  
  460.  
  461. }
  462.  
  463. }


często w plikach tpl i php używana jest zmienna $_SESSION['language'] jako element url'a (panel admina jest w kilku językach a przepisywanie urli jest w htaccessie).
Z tym, że na nazwa.pl ta zmienna zwraca string w postaci "pl" i w podstawieniach działa to poprawnie.Na zagranicznym serwie podstawia to co wypluł var_dump.
qrzysztof
Teraz pytanie czy do sesji ($_SESSION['language']) jest na pewno zapisywany obiekt klasy Language? Czy po prostu nazwa języka typu "pl", "fr"?

Bo z obiektu klasy Language po var_dump() raczej nie będzie "pl" - no chyba, że użyjemy magicznej metody __toString()
gizmo1989
Cytat(qrzysztof @ 9.09.2011, 15:25:54 ) *
Teraz pytanie czy do sesji ($_SESSION['language']) jest na pewno zapisywany obiekt klasy Language? Czy po prostu nazwa języka typu "pl", "fr"?

Bo z obiektu klasy Language po var_dump() raczej nie będzie "pl" - no chyba, że użyjemy magicznej metody __toString()

No właśnie na serwerze nazwa.pl zwraca "pl".
Nie jestem autorem tego kodu tylko staram się to wdrożyć na inny serwer więc zaraz odszukam, w którym miejscu zapisuje to do $_SESSION
qrzysztof
A jeśli użyjemy metody __toString() a na serwerach są różne wersje PHP to mogą się tu pojawić pewne różnice. Nie wiem nawet czy potrzebne jest użycie __toString() do tego. Być może sam fakt próby konwersji obiektu na tekstowe wyjście powoduje zjawisko, o którym piszesz.
gizmo1989
Hmm.No to mam jeszcze jedna dziwna wskazówkę.
Wyczyściłem cookies i var_dump na tym zagranicznym serwie dla $_SESSION['language'] zwrócił string(2) "pl" a jak odświeżyłem stronę to wywala to co wcześniej wklejałem.
Dziwne...
Również patrząc przez firebug'a na cookie widzę za 1 razem PHPSESSID=jakies id; path=/
a po odświeżeniu znika path=/
Czy to może mieć jakieś znaczenie?
Rid
  1. if ($_POST) {
  2. $user = new User;
  3. $user_id = $user -> login(@$_POST['user_name'] , @$_POST['user_password']);
  4. $status = $user -> checkCurrentSession();
  5.  
  6.  
  7. class User {
  8. function __construct() {
  9. }
  10.  
  11. function checkCurrentSession() {
  12.  
  13. if ( isset($_SESSION['user'])) {
  14.  
  15. echo $_SESSION['user']; // NO I TU DUPA :/
  16. }else{
  17.  
  18. return false;
  19. }
  20.  
  21. }
  22.  
  23. function login($user, $password) {
  24.  
  25. if ( isset ($user) && isset ($password) ) {
  26. $user_id = $this -> checkLoginData($user, $password);
  27.  
  28. if( $user_id != false) {
  29.  
  30. $_SESSION['user'] = $user_id;
  31.  
  32.  
  33. }else{
  34.  
  35. echo "wrong user";
  36. }
  37.  
  38. }
  39.  
  40. }
  41.  
  42. function checkLoginData($user,$password) {
  43.  
  44. // conecting to the database.
  45. // check for user id and return it.
  46.  
  47. // hardCoded validation ->
  48. if ($user == "dupa" && $password == "dupa") {
  49.  
  50.  
  51. return $user;
  52.  
  53. }elseif ($user == "admin" && $password =="admin" ) {
  54.  
  55.  
  56. return $user;
  57.  
  58. }else {
  59.  
  60. return false;
  61.  
  62.  
  63.  
  64. }
  65. }
  66.  
  67. function logout () {
  68.  
  69.  
  70. if ( isset ($_SESSION['user']) ) {
  71.  
  72. $_SESSION['user'];
  73. unset($_SESSION['user']);
  74. echo "sucesfully loged out";
  75. }else{
  76.  
  77. echo "Sesion does not exist. Error.";
  78.  
  79. }
  80. }
  81.  
  82. }}

Wydaje mi się ,że nie zostało sprawdzone czy formularz został wysłany i po odświeżeniu pobiera puste zmienne przez co wywala błędy>proszę spróbować dodać if($_POST)
gizmo1989
Ok.
Problem rozwiązałem.Raz zwracało string->"pl" a raz resztę klasy np. po odświeżeniu.
Zmieniłem w notepad++ funkcją zamień w plikach zmienną $_SESSION['language'] na $_SESSION['jezykw'] i panel zadziałał.
Z tego taki wniosek, że pewne metody na zagranicznym serwerze były niedozwolone z powodu kolizji nazw.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.