开发者

Problem setting session with OOP

In the program I am trying to get to work I can not figure out why I have to login in twice to set the session. The way i am navigating to different pages is as follows

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

and I have a page for user to login in with a class as follows which returns the session_var to a page called adminlogin.php. The User class does what it should but I have to login twice for some reason. I used the same code but with out classes and it works fine. New to php and trying to learn OOP and could use help. Thanks

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开发者_JAVA百科 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";
}
   }
  ?>


Using OOP doesn't change the way sessions work.

Take this any way you'd like: your sample code is very messy. If I caught one of my colleagues writing code like this I would give them a week to improve themselves or have them fired. You very likely just made an error somewhere but it's probably not in the code samples you provided. If anything, I would advise you to adopt a more robust programming style.

In any case, I've ran across similar issues as well and this might help you:

  • Make sure you're calling session_start(), always and only once (ie. it should be in one place in your whole application).

  • Make sure you're setting the SESSION value before you check it.

  • Abstract away anything to do with sessions. Don't check the SESSION variables yourself, write a class that does it for you. Like: $user->setGalleryAdmin(true) and $user->isGalleryAdmin()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜