Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 面向对象的问题设置会话_Php_Oop_Session - Fatal编程技术网

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";
}
   }
  ?>