Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Sesja
Forum PHP.pl > Forum > Przedszkole
rivesca
Witam mam problem a mianowicie:
robię sobie 2 pliki index.php i admin.php i powiedzmy mam taki kod if( isset($_SESSION['test']) ){ print 'Zalogowany' }
normalnie to wszystko ładnie działa ale jeżeli robie routing i zmieniam wartość zmiennej na 1 to traktuje jako zalogowany ale jak wpiszę każdą inna wartość to już nie, wie ktoś dla czego tak się dzieje i jak temu zaradzić?
Tomplus
Ogólnie isSet to funkcja mówiąca czy zmienna istnieje, została zadeklarowana. Więc nawet mająca wartość zero, true, false powinna przepuścić Cię. A wartość NULL powinna zwrócić jako FALSE.

sprawdź czy gdzieś nie usuwasz sesji, albo nie nadpisujesz jej po jakimś warunku.
rivesca
Witam ponownie, przesyłam mój kod może z nim jest coś nie tak bo to dzieje się tylko gdy jest routing więc zapewne tam tkwi problem.

  1. Plik controller
  2. <?php
  3.  
  4. class Controller extends Database{
  5.  
  6. function model($param){
  7. require_once('models/'.$param.'.php');
  8. }
  9. function view($param){
  10. require_once('views/'.$param.'/'.'index.php');
  11. }
  12. function post($param){
  13. return htmlentities($_POST[$param]);
  14. }
  15. function session_start(){
  16. }
  17. function session_unset($name){
  18. unset($_SESSION[$name]);
  19. }
  20. function getUrl($url){
  21. header('Location: '.$url);
  22. }
  23.  
  24.  
  25. }
  26. new Controller;
  27.  
  28. ?>






  1. controller login
  2. <?php
  3.  
  4. class Login extends Controller {
  5.  
  6. function __model(){
  7. $this->model('LoginModel');
  8. $this->model = new LoginModel;
  9. $this->session_start();
  10. if( isset($_SESSION['admin']) ){ $this->getUrl('/admin'); exit(); }
  11. }
  12.  
  13. function __content(){
  14. if($_POST){
  15. $this->query('SELECT * FROM administratorzy WHERE login=:login AND password=PASSWORD(:password)');
  16. $this->bind(':login', $this->post('login') );
  17. $this->bind(':password', $this->post('password') );
  18. $this->execute();
  19. if($this->count() == 1 ){
  20. $_SESSION['admin'] = true;
  21. $this->getUrl('/admin'); exit();
  22. }
  23. else{ $this->error_login = $this->model->error_login($error); }
  24. }
  25.  
  26. }
  27. function __template(){
  28. $this->view('Login');
  29. }
  30.  
  31. }
  32. ?>







  1. controller admin
  2. <?php
  3.  
  4. class Admin extends Controller {
  5.  
  6. function __model(){
  7. $this->model('AdminModel');
  8. $this->model = new AdminModel;
  9. $this->session_start();
  10. }
  11. function __content(){
  12. if( !isset($_SESSION['admin']) && empty($_SESSION['admin']) ){ $this->getUrl('/'); exit(); }
  13.  
  14. }
  15. function __template(){
  16. $this->view('Admin');
  17. }
  18.  
  19. }
  20. ?>






  1. No i oczywiście routing
  2. <?php
  3.  
  4. class Router{
  5. function __construct(){
  6. $this->rewrite = $_GET['page'];
  7. $this->request = explode("/", $this->rewrite);
  8.  
  9. if( !empty($this->request[1]) ){
  10. header('Location: http://'.$_SERVER['HTTP_HOST']);
  11. exit();
  12. }
  13. else{
  14. if( $this->request[0] == null){ $this->request[0] = 'login'; }
  15.  
  16. if( !file_exists('controllers/'.$this->request[0].'.php') ){
  17. if( is_dir($this->request[0]) ){
  18. header('Location: /access');
  19. new Access;
  20. exit();
  21. }
  22. require_once 'controllers/Found.php';
  23. $this->controler = new Found;
  24. $this->controler->__model();
  25. $this->controler->__template();
  26. exit();
  27. }
  28. else{
  29. require_once 'controllers/'.$this->request[0].'.php' ;
  30. $this->controler = new $this->request[0];
  31.  
  32. $this->controler->__model();
  33. $this->controler->__content();
  34. $this->controler->__template();
  35. exit();
  36. }
  37. }
  38.  
  39. }
  40. }
  41. $router = new Router;
  42.  
  43.  
  44. ?>
  45.  
  46.  




  1. Htaccess gdyby ktoś pytał
  2. <IfModule mod_deflate.c>
  3. SetOutputFilter DEFLATE
  4. </IfModule>
  5.  
  6. php_flag magic_quotes_gpc off
  7.  
  8. RewriteEngine on
  9. RewriteRule ^([^./]{2}[^.]*)$ /index.php?page=$1 [QSA]
  10.  





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.