Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: cookie i header
Forum PHP.pl > Forum > PHP
Danveld
Kombinuje nad tym już 2 dni i nie moge nic wymyśleć;/ Zdecydowałem się prosić o pomoc.
Otóż w poniższym kodzie przy logowaniu ciągle wyskakuje mi
Kod
Warning: Cannot modify header information - headers already sent in /var/www/localhost/htdocs/gregads/core/cookie.inc on line 43

Już nie mam pomysłu gdzie mam błąd;/ Żadnych pustych wierszy na koncu pliku nie mam.

Sprawa związana jest z linijką
Kod
$cookie->set();
w pliku login php.

Oto pliki:

login.php
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. include "core/cookie.inc";
  7.  
  8. include_once "HTML/Template/Flexy.php";
  9.  
  10. include_once "PEAR.php";
  11.  
  12. include_once "DB.php";
  13.  
  14. require_once 'DB/DataObject.php';
  15.  
  16.  
  17.  
  18. $options = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
  19.  
  20. $config = parse_ini_file('./core/config.ini',TRUE);
  21.  
  22. $options = $config['HTML_Template_Flexy'];
  23.  
  24.  
  25.  
  26. $options = &PEAR::getStaticProperty('DB_DataObject','options');
  27.  
  28. $config = parse_ini_file('./core/config.ini',TRUE);
  29.  
  30. $options = $config['DB_DataObject'];
  31. $originating_uri = $_GET["originating_uri"];
  32.  
  33. class LoginPage{
  34.  
  35. public $originating_uri;
  36.  
  37. function authentication(){
  38.  
  39. $this->originating_uri = isset($_GET['originating_uri']) ? $_GET['originating_uri'] : 'index.php';
  40.  
  41. if(!isset($_POST['uid']))
  42.  
  43. {
  44.  
  45. cookie::logout();
  46.  
  47. }
  48.  
  49. if (isset($_POST['uid'])) {
  50.  
  51. $usr = DB_DataObject::Factory('users');
  52.  
  53. $usr->whereAdd('login="'.$_POST['uid'].'" and password="'.$_POST['psw'].'"');
  54.  
  55. $i = $usr->find();
  56.  
  57.  
  58. if ($i == 1){
  59.  
  60.  $usr->fetch();
  61.  
  62.  $cookie = new cookie($_POST['uid']);
  63.  
  64. $cookie->set();
  65.  
  66.  session_id($cookie->userid);
  67.  
  68.  $_SESSION['user'] = $usr->forename.' '.$usr->surname;
  69.  
  70.  $_SESSION['admin'] = $usr->admin == 1 ? true : false;
  71.  
  72. print 'zalogowany <a href="index.php">ok</a>';
  73.  
  74.  exit; 
  75.  
  76. } 
  77.  
  78. else
  79.  
  80. {
  81.  
  82. echo 'Wrong login or password';
  83.  
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90. }
  91.  
  92.  
  93.  
  94. $flexy = new HTML_Template_Flexy();
  95.  
  96. $flexy->compile("login.html");
  97.  
  98. $outputObject = new LoginPage();
  99.  
  100. $outputObject->authentication();
  101.  
  102. $flexy->outputObject($outputObject);
  103.  
  104. ?>



cookie.inc
  1. <?php
  2.  
  3. class AuthException extends Exception{}
  4.  
  5. class cookie {
  6.  
  7. private $created;
  8.  
  9. public $userid;
  10.  
  11. private $version;
  12.  
  13. private $td;
  14.  
  15.  
  16.  
  17. static $cypher = 'blowfish';
  18.  
  19. static $mode = 'cfb';
  20.  
  21. static $key  = '12345';
  22.  
  23.  
  24.  
  25. static $cookiename = 'USERAUTH';
  26.  
  27. static $myversion  = '1';
  28.  
  29.  
  30.  
  31. static $expiration = '900';
  32.  
  33.  
  34.  
  35. static $warning  = '300';
  36.  
  37. static $glue = '|';
  38.  
  39.  
  40.  
  41. static $resettime = '300';
  42.  
  43.  
  44.  
  45. public function __construct($userid = false){
  46.  
  47. $this->td = mcrypt_module_open(cookie::$cypher, '', cookie::$mode, '');
  48.  
  49. if($userid){
  50.  
  51. $this->userid = $userid;
  52.  
  53. return;
  54.  
  55. }
  56.  
  57. else{
  58.  
  59. if(array_key_exists(self::$cookiename, $_COOKIE)){
  60.  
  61. $buffer = $this->_unpackage($_COOKIE[self::$cookiename]);
  62.  
  63. }
  64.  
  65. else{
  66.  
  67. throw new AuthException('brak pliku cookie');
  68.  
  69. }
  70.  
  71. }
  72.  
  73. }
  74.  
  75. public function __destruct(){
  76.  
  77. mcrypt_module_close($this->td);
  78.  
  79. }
  80.  
  81. public function set(){
  82.  
  83. $cookie = $this->_package();
  84.  
  85. setcookie(self::$cookiename, $cookie);
  86.  
  87. } 
  88.  
  89. public function validate(){
  90.  
  91. if(!$this->version || !$this->created || !$this->userid){
  92.  
  93. throw new AuthException('Zly format pliku cookie');
  94.  
  95. }
  96.  
  97. if($this->version != self::$myversion){
  98.  
  99. throw new AuthException('Niezgodnosc wersji');
  100.  
  101. }
  102.  
  103. if(time() - $this->created > self::$expiration){
  104.  
  105. throw new AuthException('Plik cookie jest nieważzny');
  106.  
  107. }else if(time() - $this->created > self::$resettime){
  108.  
  109. $this->set();
  110.  
  111. }
  112.  
  113. }
  114.  
  115. public static function logout(){
  116.  
  117. setcookie(self::$cookiename, '', 0);
  118.  
  119. }
  120.  
  121. public function _package(){
  122.  
  123. $parts = array(self::$myversion, time(), $this->userid);
  124.  
  125. $cookie = implode(self::$glue, $parts);
  126.  
  127. return $this->_encrypt($cookie);
  128.  
  129. }
  130.  
  131. private function _unpackage($cookie){
  132.  
  133. $buffer = $this->_decrypt($cookie);
  134.  
  135. list($this->version, $this->created, $this->userid) = explode(self::$glue, $buffer);
  136.  
  137. if($this->version != self::$myversion || !$this->created || !$this->userid){
  138.  
  139. throw new AuthException();
  140.  
  141. }
  142.  
  143. }
  144.  
  145. private function _encrypt ($plaintext) {
  146.  
  147. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND);
  148.  
  149. mcrypt_generic_init($this->td, self::$key, $iv);
  150.  
  151. $crypttext = mcrypt_generic($this->td, $plaintext);
  152.  
  153. mcrypt_generic_deinit($this->td);
  154.  
  155. return $iv.$crypttext;
  156.  
  157. }
  158.  
  159. private function _decrypt($crypttext) {
  160.  
  161. $ivsize = mcrypt_enc_get_iv_size($this->td);
  162.  
  163. $iv = substr($crypttext, 0, $ivsize);
  164.  
  165. $crypttext = substr($crypttext, $ivsize);
  166.  
  167. mcrypt_generic_init($this->td, self::$key, $iv);
  168.  
  169. $plaintext = mdecrypt_generic($this->td, $crypttext);
  170.  
  171. mcrypt_generic_deinit($this->td);
  172.  
  173. return $plaintext;
  174.  
  175. }
  176.  
  177. private function _reissue(){
  178.  
  179. $this->created = time();
  180.  
  181. }
  182.  
  183. }
  184. ?>
Slawx
ob_start(); wrzuć na sam poczatek skryptu - przed zainicjalizowaniem sesji smile.gif
LamaMASTER
Cookies można ustawić dopiero zanim wyświetla się zawartość strony, jakikolwiek znak. To samo ma się do funkcji header.
A wstawienie ob_start z tego co mi wiadomo nic nie wyświetli, bo trzeba by:
ob_start();
skrypt
$content = ob_get_contents();
ob_end_clean();
echo $content;
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.