简体   繁体   English

PHP:在保留SESSION / globals的同时,转到/重定向到其他URL

[英]PHP: Going/redirect to other url while keeping SESSION/globals

Dears, I'm developping an app for classes and I'm stuck. 亲爱的,我正在为班级开发一个应用程序,但我陷入了困境。 Issue is how to go to other page while keeping global variables. 问题是如何在保留全局变量的同时转到其他页面。

After successful login/register (Ctr_Security->process_login() or Ctr_Security->process_register()) the user should go back to ctrl_main while keeping global $messenger the same as before, because this variable store various errors like "wrong password" or infos like "register successfull". 成功登录/注册(Ctr_Security-> process_login()或Ctr_Security-> process_register())后,用户应返回ctrl_main,同时保持全局$ messenger与以前相同,因为此变量存储各种错误,例如“错误的密码”或信息例如“注册成功”。 But when I am using header("Location: http://localhost:80/php_mine "); 但是当我使用header(“ Location: http:// localhost:80 / php_mine ”); $messenger is empty again and previously stored messages are lost. $ messenger再次为空,并且先前存储的消息丢失。 How to redirect to other page woithout losing them? 如何重定向到其他页面而又不会丢失它们?

index.php index.php

<?php //echo 'otwarto index<br>';

require_once 'init.php';
require_once 'app/ctrl_main.php';

?>

init.php init.php

<?php //echo 'otwarto init<br>';

// Stworzenie konfiguracji
require_once dirname( __FILE__ ).'/config/Config.class.php';
$conf = new Config();
require_once dirname( __FILE__ ).'/config/config.php';
function get_conf() { global $conf; return $conf; }

// Stworzenie messengera
require_once dirname( __FILE__ ).'/lib/Messenger.class.php';
$messenger = new Messenger();
function get_messenger() { global $messenger; return $messenger; }

// Stworzenie handlera bazy danych
require_once dirname( __FILE__ ).'/lib/DBHandler.class.php';
$db_handler = new DBHandler( get_conf()->db_type, get_conf()->db_server, get_conf()->db_port, get_conf()->db_name, get_conf()->db_user, get_conf()->db_pass, get_conf()->db_charset );
function get_dbhandler() { global $db_handler; return $db_handler; }

require_once dirname( __FILE__ ).'/lib/functions.php';

?>

ctrl_main.php ctrl_main.php

<?php

require_once dirname( __FILE__ ).'/../init.php';

session_start();
$action = isset( $_REQUEST[ get_conf()->action_param ] ) ? $_REQUEST[ get_conf()->action_param ] : NULL;

switch( $action ) {

    case 'login':
        include_once get_conf()->root_path.'/app/security/Ctrl_Security.class.php';
        $ctrl = new Ctrl_Security();
        $ctrl->process_login();
        exit();
    break;

    case 'register':
        include_once get_conf()->root_path.'/app/security/Ctrl_Security.class.php';
        $ctrl = new Ctrl_Security();
        $ctrl->process_register();
        exit();
    break;

}

include $conf->root_path.'/app/security/security_check.php';

include $conf->root_path.'/app/events/events_page.php';

?>

security_check.php security_check.php

<?php //echo 'otwarto security_check<br>';

if( !isset( $_SESSION[ 'u_role' ] ) ) {
    include_once get_conf()->root_path.'/app/security/Ctrl_Security.class.php';
    $ctrl = new Ctrl_Security();
    include_once $conf->root_path.'/app/security/security_page.php';
    exit();
}

?>

Ctrl_Security.class.php Ctrl_Security.class.php

class Ctrl_Security {

    private $u_login;
    private $u_password;
    private $u_birthdate;   
    private $u_role;

    public function __construct() {
        $this->u_login = get_from_request( 'login' );
        $this->u_password = get_from_request( 'password' );
        $this->u_birthdate = format_date( get_from_request( 'birthdate' ) );
        $this->u_role = get_from_request( 'role' );
    }

    public function process_login() { // procesowanie logowania
        if( $this->validate_login_form() ) {
            if( $this->validate_login_user() ) {
                $query = "SELECT * FROM users WHERE u_login='".$this->u_login."' AND u_password='".$this->u_password."'";
                $result = get_dbhandler()->get_data( $query );
                $user = $result[ 0 ];
                //session_start();
                $_SESSION[ 'u_id' ] = $user[ 'u_id' ];
                $_SESSION[ 'u_login' ] = $user[ 'u_login' ];
                $_SESSION[ 'u_birthdate' ] = format_date( $user[ 'u_birthdate' ] );
                $_SESSION[ 'u_role' ] = $user[ 'u_role' ];
            }
        }
        header("Location: ".get_conf()->app_url."/");
    }

    public function process_register() { // proces rejestracji
        if( $this->validate_register_form() ) {
            if( $this->validate_register_user() ) {
                $query = "INSERT INTO users(u_login, u_password, u_birthdate, u_role) VALUES('".$this->u_login."', '".$this->u_password."', '".$this->u_birthdate."', '".$this->u_role."')";
                get_dbhandler()->set_data( $query );
                get_messenger()->add_info( 'Rejestracja udana' );
            }
        }
        header("Location: ".get_conf()->app_url."/");
    }   

}

?>

I resolve it with 我用解决

include_once get_conf()->root_path.'/app/security/security_page.php';
exit();

exit() prevents infinity loop exit()防止无限循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM