Topic: Checking if user is logged in (Read 2471 times)
Total Zero
Posts: 5
34 credits Members referred : 0
« on: May 03, 2009, 03:01:32 am »
I have got this class set up and the login page is working great. Thanks for making a well-documented class!Once I login, I load a new page, panel.php, which needs to load some info about the user from the SQL db. Below is the code that I am using in panel.php, as well as the warning.
Code:
<?php require_once 'access.class.php'; $user = new flexibleAccess(); if ( $_GET['logout'] == 1 ) $user->logout('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); if ( !$user->is_loaded() ) { echo '<a href="'.$_SERVER['PHP_SELF'].'?logout=1">logout</a>'; } else { echo 'You need to login !'; } ?>
Warning:Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/danielricciotti/Sites/phpUserClass/panel.php:7) in /Users/danielricciotti/Sites/phpUserClass/access.class.php on line 130
Total Zero
Posts: 5
34 credits Members referred : 0
« Reply #1 on: May 03, 2009, 04:15:17 am »
Ok. I read another post and saw that I need to put the PHP code outside the HTML content. I put the following code at the top of my panel.php page, however the isLoaded() statement returns false every time, despite logging in successfully on the previous page.
Code:
<?php require_once 'access.class.php'; $user = new flexibleAccess(); if ( $_GET['logout'] == 1 ) $user->logout('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); if ( !$user->is_loaded() ) {
Any ideas?
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #2 on: May 03, 2009, 12:35:44 pm »
It looks that output is started before including the file. Even the tabs before the php tag are output so this is probably your problem.
Total Zero
Posts: 5
34 credits Members referred : 0
« Reply #3 on: May 04, 2009, 09:26:25 pm »
Yes, that was the problem. Now, I have it working, however I am having problems accessing the user class from a different page. Is this the correct way to access information from another page, once the user has logged in?
Total Zero
Posts: 5
34 credits Members referred : 0
« Reply #5 on: May 05, 2009, 10:13:14 pm »
Thanks for the reply.
I moved all the PHP code to beginning and still have problems. Here is my current code:
Code:
<?php require_once 'access.class.php'; require_once('rb_user_functions.php');// my own library of functions/classes
$user = new flexibleAccess(); echo "\nUserID = ".$user->get_property('userID')."<br>\n"; if ( $_GET['logout'] == 1 ) $user->logout('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); if ( $user->is_loaded() == false) { echo 'X1: NEED TO REGISTER!'; } else { echo 'Loaded!'; }
I am logging in first using a different php file (login.php) which works and is copies directly from some sample code that I found. Then, I click on a link to open this page, panel.php, and this is the output of that page in the browser:
Code:
Error: No user is loaded Line: 215 Error: Unknown property userID Line: 216 UserID = X1: NEED TO REGISTER!
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #6 on: May 06, 2009, 07:48:32 am »
It appears that you don't have a userID field in your table
Total Zero
Posts: 5
34 credits Members referred : 0
« Reply #7 on: May 07, 2009, 08:55:31 pm »
I found the solution.
If anyone is interested, what happened was I am using localhost to do all of my development. Unfortunately, the browser won't save cookies from localhost because it does not have a url extension. Im gonna set up a VM and see if I can find a way around this.
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #8 on: May 08, 2009, 03:10:07 am »
That's a common problem with development environments. You can solve this with "fake" domains. You can create one in /etc/hosts on linux or the /etc/hosts file in the windows/system32 (I am not sure about the path) in windows.
After that you will need to set a virtual host in apache for that domain. Hope that helps
Just another rainy day
Posts: 1
6 credits Members referred : 0
« Reply #9 on: May 13, 2009, 07:13:01 am »
Your problem may be occurring in part because you are trying to redirect the user if they are logging out, but you can't do this if you have already output html code. So what you need to do is call ob_start() at the start of your page so no output is sent to the browser and then end with ob_end_flush()
You can apply for a free subdomain at dyndns.org and then have it link to your localhost installation, that is what I currently have for my dev environment
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=8707