Php 面向对象的问题设置会话
在我试图开始工作的程序中,我不明白为什么我必须登录两次才能设置会话。我导航到不同页面的方式如下Php 面向对象的问题设置会话,php,oop,session,Php,Oop,Session,在我试图开始工作的程序中,我不明白为什么我必须登录两次才能设置会话。我导航到不同页面的方式如下 <?php if (!isset($_REQUEST['content'])) { if (isset($_SESSION['gallery_admin'])) include("edit.inc.php"); else
<?php
if (!isset($_REQUEST['content']))
{
if (isset($_SESSION['gallery_admin']))
include("edit.inc.php");
else
include("adminlogin.php");
}
else
{
$content = $_REQUEST['content'];
$nextpage = $content . ".inc.php";
include($nextpage);
} ?>
adminlogin.php
<?php
if (isset($_POST['submit'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$hashedpassword = md5($password);
$instanceOfUserClass = new User();
$found_user = $instanceOfUserClass->checkUser($username, $hashedpassword);
//$instanceOfUserClass->checkUser($username, $hashedpassword);
//echo $instanceOfUserClass->session_var;
if ($found_user ) {
//$user = $instanceOfUserClass->session_var;
$_SESSION['gallery_admin'] = $found_user;
include ("edit.inc.php");
//header("Location: admin.php");
} else {
//echo "No User";
}
}
?>
使用OOP不会改变会话的工作方式 随便你怎么想都可以:你的示例代码非常混乱。如果我发现我的一个同事在写这样的代码,我会给他们一周的时间来提高自己,否则就解雇他们。您很可能只是在某个地方出错,但它可能不在您提供的代码示例中。如果有的话,我建议您采用一种更健壮的编程风格 无论如何,我也遇到过类似的问题,这可能会帮助您:
- 确保您始终且仅调用一次(即,它应该在整个应用程序中的一个位置)调用
会话\u start()
- 检查之前,请确保正在设置
值会话
- 抽象掉与会话有关的任何内容。不要自己检查
变量,编写一个类来完成它。例如:会话
和$user->setGalleryAdmin(true)
$user->isGalleryAdmin()
<?php
if (isset($_POST['submit'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$hashedpassword = md5($password);
$instanceOfUserClass = new User();
$found_user = $instanceOfUserClass->checkUser($username, $hashedpassword);
//$instanceOfUserClass->checkUser($username, $hashedpassword);
//echo $instanceOfUserClass->session_var;
if ($found_user ) {
//$user = $instanceOfUserClass->session_var;
$_SESSION['gallery_admin'] = $found_user;
include ("edit.inc.php");
//header("Location: admin.php");
} else {
//echo "No User";
}
}
?>