5 replies [Last post]
antibland
antibland's picture
Offline
Leader
Pittsburgh
Last seen: 12 years 24 weeks ago
Pittsburgh
Joined: 2005-01-17
Posts: 603
Points: 0

I've built this user authentication script with php and it all works smoothly. However, certain pages on the client's site need to be restricted to the login-only folks. A session variable is created if login is successful but I can't seem to access the existence of it in "deeper" pages.

For example, say I have a file: "books/reviews/0305.php"

The usual way my top (root) level files check for session existence is by a function called check_user_login(). The function works correctly at the root level. So, at the top of this (above) file I do this:

require_once('http://www.antibland.com/user_auth_fns.php');
check_valid_user();

The first line is there to include the file containing the function beneath it. However, I get this error:

"Call to undefined function: check_valid_user()"

It's not even recognized for some reason. I've been messing with this off and on for the better part of the evening. If anyone knows what I'm doing wrong, please chime in. There may be a prize in it for you. Then again, there may not be. I'm including this even though it's not being called in the scenario I posed.

check_valid_user()

function check_valid_user()
// see if somebody is logged in and notify them if not
{
  global $HTTP_SESSION_VARS;
  if (isset($HTTP_SESSION_VARS['valid_user']))
  {
      echo 'Logged in as '.$HTTP_SESSION_VARS['valid_user'].'.';
  }
  else
  {
     // excluded for brevity; display some error, etc...
  }  
}


AntiFilePath

Tags:
Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 1 day 4 hours ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

simple php question (no, really)

Hi antibland,
Have you tried changing the include path to something like ../../user_auth_fns.php

Hope that helps

antibland
antibland's picture
Offline
Leader
Pittsburgh
Last seen: 12 years 24 weeks ago
Pittsburgh
Joined: 2005-01-17
Posts: 603
Points: 0

simple php question (no, really)

Tony, thanks for the reply. Although the path wasn't the problem, it somehow led me to the partial answer. I was referring to (ugh) the wrong file. The worst mistake of all.

Even though the function is actually being called now, I can't access the session variable. I tried printing it to the screen to check it out but it's always blank, despite a user being logged in correctly. Any ideas?

Antibland

antibland
antibland's picture
Offline
Leader
Pittsburgh
Last seen: 12 years 24 weeks ago
Pittsburgh
Joined: 2005-01-17
Posts: 603
Points: 0

simple php question (no, really)

Got it. Good ol' google. I had to do a session_start() at the start of each file I want to check active session variable. I was led to believe that these were global animals. I thought that setting it once would persist throughout pages without doing another session_start(). I learned something all right. Tony, thanks again for the help.

Andrew

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 8 years 39 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

simple php question (no, really)

since PHP v4.1.0 you can use $_SESSION. Its a superglobal so you don't need to define global $_SESSION to access it from within functions. You do still need session_start().

For all things PHP, check out the manual at www.php.net. IMHO it is the best resource for PHP you'll find.

antibland
antibland's picture
Offline
Leader
Pittsburgh
Last seen: 12 years 24 weeks ago
Pittsburgh
Joined: 2005-01-17
Posts: 603
Points: 0

simple php question (no, really)

Thanks Chris. Very nutritional.

AntiStarvation