Search
Left Quote    If at first you don't succeed, redefine success.
- Anon.    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: Member System - $user problem
  This user is offline  feverpitch
  Subject: "Member System - $user problem" Posted: @ 4:51 am on Apr 07 2008   
Member #: 655
Rank: User - (5)
Since: 04/07/08
Posts: 5

Great system, btw. Just having one problem I can't seem to figure out. For whatever reason, when logged in, the $user variable is coming up blank... but the username is available is i call $_SESSION['user name'];

i've tried logging in both with and without the remember me cookie, and $user always remains blank. i've double and triple checked all my code and don't see any typos... which doesn't mean there aren't any. Logout works perfectly... so i'm assuming there's something wrong with either the page_header or the login script?

the only changes i really made were database field names (i used password instead of user_password).

my login:
ob_start();
session_start( );
require_once('d b_connect.php') ;
if(isset($_SESS ION['username'] ) && isset($_SESSION ['password'])) {
//REDIRECT TO USERS PROFILE...
header("Lo cation: home.php";
} //end if logged in
//IF SUBMIT BUTTON PRESSED
if(isset($_POST ['submit'])) {

// i created a custom error handler here
//

if(!$_POST['us ername']) form_error(&quo t;You must enter your username to log in.";
if(!$_POST['pas sword']) form_error(&quo t;You must enter your password to log in.";

//set cookie if checked
if(!empty($_POS T['stay_in'])) {
$joined =''.$_POST['use rname'].'[]'.md 5($_POST['passw ord']).'';
setcookie('logi n_cookie', $joined, 2147483647, '/', '.mysite.com');
} //end if

//verify user...
$get_user = mysql_query(&qu ot;SELECT * FROM `members` WHERE `username` = '".$_POST[ 'username'].&qu ot;' AND `password` = '".md5($_P OST['password'] )."'"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;
$q = mysql_fetch_obj ect($get_user);
$error = mysql_error();
if ($error){print $error; exit;}
if(!$q) form_error(&quo t;An error occured, please verify your username and password are correct.";
//set session variables
$_SESSION['logg ed_in'] = 1;
$_SESSION['user name'] = $_POST['usernam e'];
$_SESSION['pass word'] = $_POST['passwor d'];
session_write_c lose();

header("Lo cation: home.php";
} else {

//show login form




and my page_header

ob_start();
session_start( );
require_once('d b_connect.php') ;
//check cookie
if ($_SESSION['log ged_in'] != 1 && isset($_COOKIE[ 'login_cookie'] )) {
list($user, $pass) = explode('[]', $_COOKIE['login _cookie']);
$qu = mysql_query(&qu ot;SELECT `password` FROM `members` WHERE `username` = '".addslas hes($user).&quo t;'";
if (mysql_num_rows ($qu) == 1) {
$passw = mysql_fetch_obj ect($qu);
if ($passw->pas sword == $pass) {
$_SESSION['logg ed_in'] = 1;
$_SESSION['user name'] = $user;
$_SESSION['pass word'] = $pass;
}
}
}
if(!isset($_SES SION['username' ]) && !isset($_SESSIO N['password'])) {
$_SESSION['logg ed_in'] = 0;
$user = "Guest&quo t;;
}



been tearing my hair out... any help would be appreciated!

Edited at 05:03:18 am on 04/07/08


    Viewed: 12,761 Times | Reply to This | To top
  This user is offline  feverpitch
  Subject: "re: Member System - $user problem" Posted: @ 5:07 am on Apr 07 2008    
Member #: 655
Rank: User - (5)
Since: 04/07/08
Posts: 5

Some more information: i just logged in with the "remember me" option checked and then viewed the login_cookie that was set. The username is being written to that, along with the md5 password...

so why isn't it being put into $user?




    Viewed: 12,752 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Member System - $user problem" Posted: @ 2:03 pm on Apr 07 2008    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Check your cookie son:

setcookie(' login_cookie', $joined, 2147483647, '/', '.mysite.com' );

mysite.com should be YOUR SITE URL. Let me know if this fixed it. Good luck




-bs0d | AllSyntax.com

    Viewed: 12,739 Times | Reply to This | To top
  This user is offline  feverpitch
  Subject: "re: Member System - $user problem" Posted: @ 2:12 pm on Apr 07 2008    
Member #: 655
Rank: User - (5)
Since: 04/07/08
Posts: 5

oh, i know. both cookie calls use the actual information (ie, the main URL of the server). I just replaced them with mysite.com for the post of my code.




    Viewed: 12,736 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Member System - $user problem" Posted: @ 2:20 pm on Apr 07 2008    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

So if you login regularly without checking "remember me", then you are logged in and the form redirects to home.php but $user is not set? But $_SESSION['use rname'] is?

And if you check "remember me" then it works fine?

$_SESSION['use rname'] is set to $user from the cookie. The cookie is set only if someone checks "remember me"

So if someone just logs in without checking "remember me" then $user is not set to $_SESSION['use rname']. You will have to add that if that's what you want.



Edited at 02:34:36 pm on 04/07/08


-bs0d | AllSyntax.com

    Viewed: 12,733 Times | Reply to This | To top
  This user is offline  feverpitch
  Subject: "re: Member System - $user problem" Posted: @ 2:56 pm on Apr 07 2008    
Member #: 655
Rank: User - (5)
Since: 04/07/08
Posts: 5

No, $user is set to blank in BOTH cases - if the "remember me" cookie is set, or if it's not checked. It's very strange. $user will be Guest and display with no issues when signed out, but if i sign in, $user is set to blank.

To test, i put the following on my home.php:
user: <? echo $user; ?><br />
logged in: <? echo $_SESSION['log ged_in']; ?><br />
session user: <? echo $_SESSION['use rname']; ?><br />

When signed out, i get
user: Guest
logged in: 0
session user:

When signed in with either the cookie or without, i get:
user:
logged in: 1
session user: testmember



    Viewed: 12,727 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Member System - $user problem" Posted: @ 3:11 pm on Apr 07 2008    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

See if this fixes it:
Code:

if(!isset($_SES SION['username ']) && !isset($_SESSIO N['password'] )) {
$_SESSION['log ged_in'] = 0;
$user = "Guest&q uot;;
} else {
$user = $_SESSION['use rname'];
} //end if




-bs0d | AllSyntax.com

    Viewed: 12,723 Times | Reply to This | To top
  This user is offline  feverpitch
  Subject: "re: Member System - $user problem" Posted: @ 4:07 pm on Apr 07 2008    
Member #: 655
Rank: User - (5)
Since: 04/07/08
Posts: 5

That did the trick! Works perfectly now.

Thanks.




    Viewed: 12,717 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Member System - $user problem" Posted: @ 4:12 pm on Apr 07 2008    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Awesome!




-bs0d | AllSyntax.com

    Viewed: 12,714 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank