2011-06-02 66 views
0

在我试图去工作的程序中,我找不到为什么我必须登录两次才能设置会话。我要前往不同的页面的方法如下OOP问题设置会话

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

,我有一个页面,用户与一类在登录如下它返回session_var一个叫adminlogin.php页面。 User类做了它应该做的事情,但由于某种原因我必须登录两次。我使用相同的代码,但没有类,它工作正常。新的PHP和试图学习面向对象和可以使用帮助。 感谢

public function CheckUser ($username, $hashedpassword) 
{ 
    $query = "select count(*) from user where username = ? and password = ?"; 
    $dbConnection = $this->connection; 
    $stmt = mysqli_prepare($dbConnection,$query); 
    mysqli_stmt_bind_param($stmt, 'ss', $username, $hashedpassword); 
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_bind_result($stmt,$userCount); 
    mysqli_fetch($stmt); 
    if ($userCount > 0) 
    { 
     //$_SESSION['gallery_admin'] = $username; 
     //$user = $this->username; 
     //$this->session_var = $_SESSION['gallery_admin']; 
     $this->session_var = $username; 
     return $this->session_var; 
     //include ("adminmain.inc.php"); 
     //header("Location: admin.php"); 
     } 
    else { 
     return "in the else"; 
    } 
} 

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

请用花括号来格式化你的代码 – 2011-06-02 15:55:01

+0

不知道你的意思花括号什么? – user747796 2011-06-02 16:05:01

回答

1

使用OOP不会改变会议的工作方式。

采取任何你喜欢的方式:你的示例代码非常混乱。如果我发现我的一位同事编写这样的代码,我会给他们一个星期来改善自己或让他们解雇。您很可能只是在某处发生了错误,但可能不在您提供的代码示例中。如果有的话,我会建议你采用更强大的编程风格。

在任何情况下,我已经遇到类似的问题跑,以及这可能会帮助您:

  • 确保你调用session_start(),永远只有一次(即它应该是一个。放置在你的整个应用程序中)。

  • 确保您在设置SESSION值之前检查它。

  • 抽象与会议的任何事情。不要自己检查SESSION变量,写一个能为你做的类。像:$user->setGalleryAdmin(true)$user->isGalleryAdmin()