0

I've looked at existing answers for my problem.

I've echo'd the value right through the process and right up until the "header('Location" instruction the values remain intact.

I don't think it's a serialization problem as suggested for similar problems...

Here are the relevant bits of the class:

class clsSetUser {
  protected $UserID = 0;

  public function initUser($id) {
     // get user details from database

     $this->setUserID($id);
     // etc...
  }

  private function setUserID($value) { $this->UserID = $value; }
  public function getUserID() { return $this->UserID; }
}

common.php:

if(unset($clsUser)) $clsUser = new clsSetUser;

login-exec.php:

$clsUser->initUser($id);   

header("Location: somewhere.php");

somewhere.php:

echo $clsUser->getUserID();

// here it equals 0

any ideas? does "header" serialize everything?

LaurentG
  • 11,128
  • 9
  • 51
  • 66
Jane Alford
  • 51
  • 1
  • 2
  • 10

1 Answers1

0

This is because PHP is actually starting from a clean slate in somewhere.php.

header("Location: somewhere.php"); sends a command the browser to connect to a different page. In this page non of variables of the previous page are available in PHP.

You need to set the userId in the $_SESSION so that you can reload the user from the database when he visits somewhere.php.

login-exec.php

$clsUser->initUser($id);   
$_SESSION['user_id'] = $id;
header("Location: somewhere.php");

somewhere.php

$clsUser->initUser($_SESSION['user_id']);
EWit
  • 1,954
  • 13
  • 22
  • 19
  • Blast - I had hoped to avoid using $_SESSION too much. It's illogical to keep hitting the database every time I open a new page. Thanks for the information. – Jane Alford Jun 23 '13 at 16:09
  • Why do you want to avoid hitting the database for the user object? If the user/session table is indexed properly getting the user object will take no time at all. Else you can try storing the required user information in the cookie but then that data will be transferred to the server on each request. Which may increase the loading time of the page. – EWit Jun 23 '13 at 16:26
  • It's not just the user object, it's the database object, and various values that never change and come from the ini file. It's all added processing time and should only be performed once. I have a VERY large application with hundreds of pages. I had hoped to build a full OO system, but obviously not in PHP, I can't do this if the class is destroyed every time a header instruction is issued... – Jane Alford Jun 24 '13 at 08:52
  • It's not just when the header instruction is used. It's whenever the website returns a page. PHP is stateless, every request he user sends to the server, i.e. every link the user clicks, is a complete new request with a clean slate. I'm not sure what kind of project you're working on but if you need it to be persistent I'm not sure PHP is the easiest way forward. Maybe this can give a hint: http://stackoverflow.com/questions/2940610/how-to-persist-objects-between-requests-in-php – EWit Jun 24 '13 at 12:46
  • I'm not now going to redevelop in one of the VB languages (ASP.NET) or Java. The overhead is too great. I'm just going to have to use $_SESSION. – Jane Alford Jun 24 '13 at 17:31