亲爱的,我正在为课程开发应用程序而且我被卡住了。问题是如何在保持全局变量的同时转到其他页面。
成功登录/注册(Ctr_Security-> process_login()或Ctr_Security-> process_register())后,用户应返回ctrl_main,同时保持全局$ messenger与之前相同,因为此变量存储各种错误,如“错误的密码“或像”注册成功“的信息。但是当我使用标题时(“位置:http://localhost:80/php_mine”); $ messenger再次为空,之前存储的消息将丢失。如何重定向到丢失它们的其他页面?
的index.php
<?php //echo 'otwarto index<br>';
require_once 'init.php';
require_once 'app/ctrl_main.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
<?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
<?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
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."/");
}
}
?>
答案 0 :(得分:0)
我用
解决了这个问题include_once get_conf()->root_path.'/app/security/security_page.php';
exit();
exit()阻止无限循环