Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] gubi sesje
Forum PHP.pl > Forum > PHP
Balon
Prosze znowu o pomoc. Robie wlasnie formularz logowania ktory mozna zobaczyc tutaj www.sl.scx.pl (login: user pass: user) i logowanie itd dziala ale sesja jakby znika bo wystarczy odswiezyc i znowu widac formularz logowania....

kod index.php:

  1. <?
  2. ob_start();//Tworzenie sesji i sprawdzanie czy user zalogowal sie
  3. session_cache_limiter('nocache'); ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <? require('inc/func.php'); ?>
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Language" content="pl">
  9. <title>Smieszne linki</title>
  10. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
  11. <link href="style.css" rel="stylesheet" type="text/css">
  12. </head>
  13. <body bgcolor="#FFFFFF" topmargin="0">
  14.  
  15. <table border="0" cellpadding="0" cellspacing="0" width="760">
  16. <tr>
  17. <td width="100%" align="center">
  18. &nbsp;</td>
  19. </tr>
  20. <tr>
  21. <td width="100%" align="center">
  22. <table border="0" cellpadding="0" cellspacing="0" width="719">
  23. <tr>
  24. <td width="350">
  25. <table border="0" cellpadding="0" cellspacing="0" width="350">
  26. <tr>
  27. <td colspan="7" width="350">
  28. <a href="index.html">
  29. <img id="img_03" src="images/img_03.gif" width="104" height="46" alt="" border="0" /><img id="img_04" src="images/img_04.gif" width="101" height="46" alt="" border="0" /></a></td>
  30. </tr>
  31. <tr>
  32. <td width="350" colspan="7">&nbsp;</td>
  33. </tr>
  34. <tr>
  35. <table border="0" cellpadding="0" cellspacing="0">
  36. <tr>
  37. <td>
  38. <a href="wyslij_link.html"><img border="0" src="images/img_08.gif" width="77" height="20" alt="" /></a></td>
  39. <td>
  40. &nbsp;</td>
  41. <td>
  42. <a href="top50.html"><img border="0" src="images/img_10.gif" width="77" height="20" alt="" /></a></td>
  43. <td>
  44. &nbsp;</td>
  45. <td>
  46. <a href="statystyki.html"><img border="0" src="images/img_12.gif" width="77" height="20" alt="" /></a></td>
  47. <td>
  48. &nbsp;</td>
  49. <td>
  50. <a href="kontakt.html">
  51. <img id="img_14" src="images/img_14.gif" width="77" height="20" border="0" /></a></td>
  52. </tr> </tr>
  53. </table>
  54. </td>
  55. <td valign="top" width="369">
  56. <? include "inc/login_form.php" ?>
  57. </td>
  58. </tr>
  59. </table>
  60.  
  61.  
  62. <table border="0" cellpadding="0" cellspacing="0" width="100%" height="390">
  63. <tr>
  64. <td width="394" height="390" valign="top" align="left">
  65. <?
  66.  
  67. if($co=="reklama") { include "inc/reklama.php"; }
  68. elseif($co=="kontakt") { include "inc/kontakt.php"; }
  69. elseif($co=="statystyki") { include "inc/statystyki.php"; }
  70. elseif($co=="top") { include "inc/top.php"; }
  71. elseif($co=="wyslij") { include "inc/wyslij.php"; }
  72. elseif($co=="wspolpraca") { include "inc/wspolpraca.php"; }
  73. elseif($co=="linki_kat") { include "inc/linki_kat.php"; }
  74. else { include "inc/linki.php"; }
  75.  
  76. ?>
  77.  
  78. </td>
  79. <td width="353" height="390" valign="top" align="center">
  80. <? include('inc/news.php'); ?>
  81.  
  82. <table border="0" cellpadding="0" cellspacing="0">
  83. <tr>
  84. <td>
  85. <a href="wspolpraca.html">
  86. <img border="0" src="images/wspolpraca.gif" width="77" height="20" alt="" /></a></td>
  87. <td>
  88. &nbsp;</td>
  89. <td>
  90. <a href="reklama.html">
  91. <img border="0" src="images/reklama.gif" width="77" height="20" alt="" /></a></td>
  92. <td>
  93. &nbsp;</td>
  94. <td>
  95. <a href="redakcja.html">
  96. <img border="0" src="images/redakcja.gif" width="77" height="20" alt="" /></a></td>
  97. <td>
  98. &nbsp;</td>
  99. <td>
  100. <a href="kontakt.html">
  101. <img src="images/kontakt_2.gif" width="77" height="20" border="0" /></a></td>
  102. </tr>
  103. </table>
  104. </td>
  105. </tr>
  106. </table>
  107.  
  108. <table width="640" height="20" border="0" cellpadding="0" cellspacing="0">
  109. <tr>
  110. <td>&nbsp;</td>
  111. </tr>
  112. </table>
  113.  
  114. <table width="640" height="30" border="0" cellpadding="0" cellspacing="0">
  115. <tr>
  116. <td>
  117. <img src="images/stopka_01.gif" width="6" height="30" alt=""></td>
  118. <td width="628" background="images/stopka_tlo.gif" align="center">
  119.  <font face="Tahoma" size="1"><font color="#D5F0FB">Copyright 2003-2005 <b>śmieszne linki</b>. All rights reserved. 
  120.  Project: </font> <a href="http://www.scx.pl"><font color="#D5F0FB">Scx.pl</font></a></font></td>
  121. <td>
  122. <img src="images/stopka_03.gif" width="6" height="30" alt=""></td>
  123. </tr>
  124. </table>
  125. </td>
  126. </tr>
  127. </table>
  128. </body>
  129. </html>
  130. <? ob_end_flush(); ?>


plik z formularzem

  1. <?
  2.  
  3. lacz();
  4. ?>
  5.  
  6. <table border="0" cellpadding="0" cellspacing="0" height="61">
  7. <tr>
  8. <td width="172" rowspan="3" height="61">
  9. &nbsp;</td>
  10. <td height="29">
  11. <a href="index.html"><img src="images/home.gif" width="63" height="29" alt="" border="0"></a></td>
  12. <td height="29">
  13. <img src="images/archiwum.gif" alt="" width="81" height="29"></td>
  14. <td height="29">
  15. <a href="kontakt.html"><img src="images/kontakt.gif" width="71" height="29" alt="" border="0"></td>
  16. </tr>
  17.  
  18. <!-- formularz logowania -->
  19. <?
  20.  
  21. if(isset($HTTP_POST_VARS['login']) && isset($HTTP_POST_VARS['pass']))
  22. {
  23. // Skrypt sprawdzajacy prawdziwosc danych podczas logowania
  24. $user = $HTTP_POST_VARS['login'];
  25. $pass = $HTTP_POST_VARS['pass'];
  26.  
  27. $zapytanie = 'select * from users '
  28.  ."where name='$user' "
  29.  ." and pass='$pass'";
  30. $wynik = mysql_query($zapytanie);
  31. if(mysql_num_rows($wynik) > 0)
  32. {
  33. // Jezeli dane sa prawdziwe nastepuje zarejestrowanie identyfikatora usera
  34. $HTTP_SESSION_VARS['prawid_uzyt'] = $user;
  35. echo '<tr>
  36. <td colspan="3" align="center" height="32">
  37. Zalogowany jako: <b>'.$user.'</b>.
  38. </td>
  39. </tr>
  40. </table>';
  41.  
  42. } else {
  43. echo '<tr>
  44. <td colspan="3" align="center" height="32">
  45. <b>Nieprawidłowe dane.</b>
  46. </td>
  47. </tr>
  48. </table>';
  49. }
  50. } elseif(isset($HTTP_SESSION_VARS['prawid_uzyt'])) {
  51.  
  52.  
  53. menu_user();
  54.  
  55. }
  56. ?>



  1. <?
  2. // Tworzenie linkow dla zalogowanego usera
  3. function menu_user() {
  4. if (isset($user))
  5. {
  6.  
  7. echo '<tr>
  8. <td colspan="3" align="center" height="32">
  9. Zalogowany jako: <b>'.$user.'</b>.
  10. </td>
  11. </tr>
  12. </table>';
  13.  
  14. } else {
  15. echo'<td colspan="3" align="center" height="32">
  16. <form action="index.html" method="post">
  17. <input type="text" name="login" size="10" class="t1">&nbsp;&nbsp;
  18. <input type="password" name="pass" size="10" class="t1"></td>
  19. </tr>
  20. <tr>
  21. <td colspan="3" align="center">
  22. <input type="image" src="images/zaloguj.gif"></form>&nbsp;&nbsp;<a href="rejestracja.html"><img src="images/zarejestruj.gif" alt="" width="67" height="16" border="0"></a></td>
  23. </tr>
  24. </table>';
  25. }
  26. }
  27. ?>


Czy ktos wie w czym tkwi blad? Prosze o pomoc winksmiley.jpg
zulus
php nie gubi twojej sesji tylko nie zdązy jej zapisać.
Jeżeli utorzysz jakąś zmienną sesyjną jeszcze przed logowaniem (na innej stronie) będzie ona dostępna jak już wejdziesz w logowanie i po wywołaniu header().
Nie wiem czy to błąd php ale jak wywołujesz header() skrypt przerywa działanie (coś jak exit()), a php nie zdąży zapisać na nowo sesji do swojego /tmp.

Rozwiązaniem jest wywoływanie session_write_close(); przed każdym header(location:);
Balon
tyle ze ja nie mam zadnego headera... nigdzie.... a wczesniej korzystalem z podobnego skryptu przy projekcie panelu admina dosystemu newsowego i wszystko ladnie dzialalo. Prosze jeszcze raz zobaczyc na to po sylwestrze i moze ktos pomoze mi rozwiazac to winksmiley.jpg
zulus
sorry pisałem to dla kogoś innego.
W pliku index.php na początku wywołujesz funkcję ob_start() która według komentarza inicjuje sesje.
Zaraz po niej pojawia się session_start() które faktycznie w php inicjuje sesje.
Jeżeli w funkcji ob_start() ma być faktyczna inicjalizacja sesji to w niej umieść session_start(), ew daj session_start() przed uruchomieniem ob_start()

Jeszcze raz sorry biggrin.gif
Balon
probowalem na wszystkie mozliwe sposoby ale efekt mozecie zobaczyc nadal na stronie.... nic sie nie dzieje www.sl.scx.pl login: user pass: user.... mozna zobaczyc ze nic to nie dalo... ale dziwne bo w poprzednim projekcie wszystko dziala ok a jest zrobione na tej samej zasadzie
zulus
czy ten ostatni plik jest w klejone prosto z pliku bo w ni masz błąd
w funkcji menu_user() sprawdzasz czy jest stworzona zmienna $user, ale nie pojawia się ani
  1. <?php
  2. global $user;
  3. ?>

ani nic podobnego przyjżyj się tej funkcji i sprawdź z kąd ma pobierać tą zmienną i czy na pewno jest inicjowana
Balon
Wiesz w sumie pierwszy raz proboje cos z funkcjami ... Czy moglbys powiedziec w ktorym miejscu powinien sie znalezc ten kod ?
  1. <?php
  2. global $user;
  3. ?>

bo ja bym to niby dal w momencie kiedy podczas logowania nastepuje tworzenie sesji...
zulus
w tej funkcji szukasz zmiennej $user i jest pytanie gdzie ją inicjujesz, bo widziałem że po zalogowaniu, podejrzewam że też w tej funkcji ob_start()

w obydwu zara po
function nazwa_funkcji()
wklep global $user;

[edit] Poczytaj sobie: "zasięg zmiennych"
Balon
Wyedytowalem tak jak mi sie wydawało ze bedzie dobrze

func.php
  1. <?php
  2. // Tworzenie linkow dla zalogowanego usera
  3. function menu_user() {
  4.  
  5. global $user;
  6. if (isset($HTTP_SESSION_VARS['zarej_user']))
  7. {
  8.  
  9. echo '<table><tr>
  10. <td colspan="3" align="center" height="32">
  11. Zalogowany jako: <b>'.$user.'</b>.
  12. </td>
  13. </tr>
  14. </table>';
  15.  
  16. } else {
  17. echo'<table><tr><td colspan="3" align="center" height="32">
  18. <form action="index.html" method="post">
  19. <input type="text" name="login_log" size="10" class="t1">&nbsp;&nbsp;
  20. <input type="password" name="pass_log" size="10" class="t1"></td>
  21. </tr>
  22. <tr>
  23. <td colspan="3" align="center">
  24. <input type="image" src="images/zaloguj.gif"></form>&nbsp;&nbsp;<a href="rejestracja.html"><img src="images/zarejestruj.gif" alt="" width="67" height="16" border="0"></a></td>
  25. </tr>
  26. </table>';
  27. }
  28. }
  29. ?>


logowanie.php
  1. <table border="0" cellpadding="2" cellspacing="0" width="100%">
  2. <tr>
  3. <td width="4%" class="title">
  4. &nbsp;
  5. </td>
  6. <td class="title">
  7. <u><b>LOGOWANIE</b></u> // panel logowania
  8. </td>
  9. </tr>
  10.  
  11. <tr>
  12. <td width="100%" colspan="2" align="center">
  13. <img border="0" src="images/hr.gif" width="337" height="1" alt="" /></td>
  14. </tr>
  15. </table>
  16. <table border=0 cellpadding=3 cellspacing=0 width="100%">
  17. <tr>
  18. <td width="100%" align="center" class="tsc_m">
  19.  
  20. <?
  21. global $user;
  22. if(isset($HTTP_POST_VARS['login_log']) && isset($HTTP_POST_VARS['pass_log']))
  23. {
  24. // Skrypt sprawdzajacy prawdziwosc danych podczas logowania
  25. $user = $HTTP_POST_VARS['login_log'];
  26. $pass = $HTTP_POST_VARS['pass_log'];
  27.  
  28. $zapytanie = 'select * from users '
  29.  ."where name='$user' "
  30.  ." and pass='$pass'";
  31. $wynik = mysql_query($zapytanie);
  32. if(mysql_num_rows($wynik) > 0)
  33. {
  34. // Jezeli dane sa prawdziwe nastepuje zarejestrowanie identyfikatora usera
  35. $HTTP_SESSION_VARS['zarej_user'] = $user;
  36. echo 'Zalogowany jako '.$user.'';
  37. } else {
  38. echo 'Nieprawidłowe dane!';
  39. }
  40. } else {
  41.  
  42. menu_user();
  43. }
  44. ?>
  45.  
  46.  
  47.  
  48. </td>
  49. </tr>
  50.  
  51. </table>


index.php

  1. <?
  2. ob_start();//Tworzenie sesji i sprawdzanie czy user zalogowal sie
  3.  
  4. ?>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <? require('inc/func.php'); ?>
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <meta http-equiv="Content-Language" content="pl">
  10. <title>Smieszne linki</title>
  11. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
  12. <link href="style.css" rel="stylesheet" type="text/css">
  13. </head>
  14. <body bgcolor="#FFFFFF" topmargin="0">
  15.  
  16. <table border="0" cellpadding="0" cellspacing="0" width="760">
  17. <tr>
  18. <td width="100%" align="center">
  19. &nbsp;</td>
  20. </tr>
  21. <tr>
  22. <td width="100%" align="center">
  23. <table border="0" cellpadding="0" cellspacing="0" width="719">
  24. <tr>
  25. <td width="350">
  26. <table border="0" cellpadding="0" cellspacing="0" width="350">
  27. <tr>
  28. <td colspan="7" width="350">
  29. <a href="index.html">
  30. <img id="img_03" src="images/img_03.gif" width="104" height="46" alt="" border="0" /><img id="img_04" src="images/img_04.gif" width="101" height="46" alt="" border="0" /></a></td>
  31. </tr>
  32. <tr>
  33. <td width="350" colspan="7">&nbsp;</td>
  34. </tr>
  35. <tr>
  36. <table border="0" cellpadding="0" cellspacing="0">
  37. <tr>
  38. <td>
  39. <a href="wyslij_link.html"><img border="0" src="images/img_08.gif" width="77" height="20" alt="" /></a></td>
  40. <td>
  41. &nbsp;</td>
  42. <td>
  43. <a href="top50.html"><img border="0" src="images/img_10.gif" width="77" height="20" alt="" /></a></td>
  44. <td>
  45. &nbsp;</td>
  46. <td>
  47. <a href="statystyki.html"><img border="0" src="images/img_12.gif" width="77" height="20" alt="" /></a></td>
  48. <td>
  49. &nbsp;</td>
  50. <td>
  51. <a href="kontakt.html">
  52. <img id="img_14" src="images/img_14.gif" width="77" height="20" border="0" /></a></td>
  53. </tr> </tr>
  54. </table>
  55. </td>
  56. <td valign="top" width="369">
  57. <? include "inc/login_form.php" ?>
  58. </td>
  59. </tr>
  60. </table>
  61.  
  62.  
  63. <table border="0" cellpadding="0" cellspacing="0" width="100%" height="390">
  64. <tr>
  65. <td width="394" height="390" valign="top" align="left">
  66. <?
  67.  
  68. if($co=="reklama") { include "inc/reklama.php"; }
  69. elseif($co=="kontakt") { include "inc/kontakt.php"; }
  70. elseif($co=="statystyki") { include "inc/statystyki.php"; }
  71. elseif($co=="top") { include "inc/top.php"; }
  72. elseif($co=="wyslij") { include "inc/wyslij.php"; }
  73. elseif($co=="wspolpraca") { include "inc/wspolpraca.php"; }
  74. elseif($co=="linki_kat") { include "inc/linki_kat.php"; }
  75. elseif($co=="rejestracja") { include "inc/rejestracja.php"; }
  76. elseif($co=="logowanie") { include "inc/logowanie.php"; }
  77. else { include "inc/linki.php"; }
  78.  
  79. ?>
  80.  
  81. </td>
  82. <td width="353" height="390" valign="top" align="center">
  83. <? include('inc/news.php'); ?>
  84.  
  85. <table border="0" cellpadding="0" cellspacing="0">
  86. <tr>
  87. <td>
  88. <a href="wspolpraca.html">
  89. <img border="0" src="images/wspolpraca.gif" width="77" height="20" alt="" /></a></td>
  90. <td>
  91. &nbsp;</td>
  92. <td>
  93. <a href="reklama.html">
  94. <img border="0" src="images/reklama.gif" width="77" height="20" alt="" /></a></td>
  95. <td>
  96. &nbsp;</td>
  97. <td>
  98. <a href="redakcja.html">
  99. <img border="0" src="images/redakcja.gif" width="77" height="20" alt="" /></a></td>
  100. <td>
  101. &nbsp;</td>
  102. <td>
  103. <a href="kontakt.html">
  104. <img src="images/kontakt_2.gif" width="77" height="20" border="0" /></a></td>
  105. </tr>
  106. </table>
  107. </td>
  108. </tr>
  109. </table>
  110.  
  111. <table width="640" height="20" border="0" cellpadding="0" cellspacing="0">
  112. <tr>
  113. <td>&nbsp;</td>
  114. </tr>
  115. </table>
  116.  
  117. <table width="640" height="30" border="0" cellpadding="0" cellspacing="0">
  118. <tr>
  119. <td>
  120. <img src="images/stopka_01.gif" width="6" height="30" alt=""></td>
  121. <td width="628" background="images/stopka_tlo.gif" align="center">
  122.  <font face="Tahoma" size="1"><font color="#D5F0FB">Copyright 2003-2005 <b>śmieszne linki</b>. All rights reserved. 
  123.  Project: </font> <a href="http://www.scx.pl"><font color="#D5F0FB">Scx.pl</font></a></font></td>
  124. <td>
  125. <img src="images/stopka_03.gif" width="6" height="30" alt=""></td>
  126. </tr>
  127. </table>
  128. </td>
  129. </tr>
  130. </table>
  131. </body>
  132. </html>
  133. <? ob_end_flush(); ?>


Zrobilem to tak jak mowiles ale nic to nie daje ;( Czy moglbys mi pokazac jak to zrobic bo ja juz sie kompletnie pogubilem....

P.S. Wiem ze juz ostro zawracam Wam głowe, ale sam sobie nie poradze z tym.
zulus
wię po kolei
plik func.php
zmień 5 linię
  1. <?php
  2. global $user;
  3. ?>

na
  1. <?php
  2. global $HTTP_SESSION_VARS;
  3. ?>


w logowanie.php wywal 21 linię:
  1. <?php
  2. global $user;
  3. ?>


i powinno śmigać

A tak na marginesie to zamiast zmiennych HTTP_POST_VARS i tp lepiej używać
$_POST, $_SESSION;
wtedy można zapomnieć o global $HTTP_POST_VARS, te tablice są zawsze globalne
Balon
Ok działa teraz ale niestety jeśli się zaloguje to u gory formularz logowania nie zamienia sie na napis ze user jest zalogowany...

login_form.php
  1. <table border="0" cellpadding="0" cellspacing="0" height="61">
  2. <tr>
  3. <td width="172" rowspan="3" height="61">
  4. &nbsp;</td>
  5. <td height="29">
  6. <a href="index.html"><img src="images/home.gif" width="63" height="29" alt="" border="0"></a></td>
  7. <td height="29">
  8. <img src="images/archiwum.gif" alt="" width="81" height="29"></td>
  9. <td height="29">
  10. <a href="kontakt.html"><img src="images/kontakt.gif" width="71" height="29" alt="" border="0"></td>
  11. </tr>
  12.  
  13. <!-- formularz logowania -->
  14. <? if(isset($HTPP_SESSION_VARS['zarej_user'])) {
  15. echo 'Zalogowany jako '.$user.'.';
  16. } else {
  17. echo '<tr><td colspan="3" align="center" height="32">
  18. <form action="logowanie.html" method="post">
  19. <input type="text" name="login_log" size="10" class="t1">&nbsp;&nbsp;
  20. <input type="password" name="pass_log" size="10" class="t1"></td>
  21. </tr>
  22. <tr>
  23. <td colspan="3" align="center">
  24. <input type="image" src="images/zaloguj.gif"></form>&nbsp;&nbsp;<a href="rejestracja.html"><img src="images/zarejestruj.gif" alt="" width="67" height="16" border="0"></a></td>
  25. </tr>
  26. </table>';
  27. } ?>


A gdzie jest includowany to mozna zobaczyc u gory w index.php... prosze o pomoc winksmiley.jpg
zulus
masz literówkę w lini 15
zamiast $HPPT_SESSION_VARS powinno być $HTTP_SESSION_VARS
Vertical
Jesli nie zachowuje Ci sesji, radze skorzystac z session_id()
  1. <?php
  2. //kod php
  3. session_id('69');
  4. //dalszy kod php
  5. ?>
Balon
Dzieki wielkie winksmiley.jpg Dzieki Tobie zaczynam to rozumiec smile.gif
@Vertical - juz wszystko dziala winksmiley.jpg wina jest moja niespostrzegawczosc winksmiley.jpg
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.