14, February 2012

Checking if user is logged in - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  Php User Class
Topic: Checking if user is logged in
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Checking if user is logged in  (Read 2393 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'] == 
$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'] == 
$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: Male
Posts: 5779
46271 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.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
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?
Code:
?php
require_once 'access.class.php';
$user = new flexibleAccess();
echo "\nxUserID = ".$user->get_property('userID')."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Panel...</title>
<meta name="robots" content="noindex,nofollow" />
<link href="theme/default.css" rel="stylesheet" media="all" type="text/css" />
<script language="javascript" type="text/javascript" src="js/login.js"></script>
</head>
<body>
<!--<div id="err">Error</div>-->
<div id="wrapper">
<?php
if ( $_GET['logout'] == 
$user->logout('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
if ( $user->is_loaded() ) {
include ('rb_user_panel.php');
[
b]...[/b]

This code is printing "Error: No user is loaded", but I logged into the previous page!
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5779
46271 credits
Members referred : 3



« Reply #4 on: May 05, 2009, 10:29:21 am »

Again you should put all the code before any output, because this way the logout() function wont be able to send headers to erase the cookie

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
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'] == 
$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: Male
Posts: 5779
46271 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

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
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: Male
Posts: 5779
46271 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 Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
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
Tags : login session Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  Php User Class
Topic: Checking if user is logged in
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Feb 14, 2012, 02:32:50 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.