Search
Left Quote    It is possible to store the mind with a million facts and still be entirely uneducated.
- Alec Bourne    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: Session login problem!!
  This user is online  Guest
  Subject: "Session login problem!!" Posted: @ 1:28 pm on Aug 03 2006   
Unregistered
I used the members tutorial. Got most things working. Register and login works fine. But when i redirect the logged in user, and echo the $user name the user is echoed as guest. What am i doing wrong?

This is the login.php file:
--------------- ------------

<?
ob_start();

//Database conn
$connect = mysql_connect(& quot;secret&quo t;, "secret&qu ot;, "secret&qu ot;
$select= mysql_select_db ("1769800& quot;

if(isset($_SESS ION['username'] ) && isset($_SESSION ['password'])) {
//REDIRECT TO USERS PROFILE...
header("Lo cation: admin_main.php& quot;
} //end if logged in

//IF SUBMIT BUTTON PRESSED
if(isset($_POST ['submit'])) {

if(!$_POST['use rname']) die("Error : You must enter your username before logging in.";
if(!$_POST['pas sword']) die("Error : You must enter your password before logging 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');
} //end if

//verify user...
$get_user = mysql_query(&qu ot;SELECT * FROM `members` WHERE username = '".$_POST[ 'username'].&qu ot;' AND

user_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);
if(!$q) die("Login Failure: 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: admin_main.php& quot;

} else {
//show login form
?>
<form name="logi n" method="po st" action="&l t;? $_SERVER['PHP_S ELF']; ?>">
<table> <tr>
<td>Usern ame:<input type="text " id="userna me" name="user name">& lt;/td>
</tr>
<tr>
<td>Passw ord:<input type="pass word" id="passwo rd" name="pass word">& lt;/td>
</tr>
<tr>
<td>Submi t: <input type="subm it" value="Sub mit" name="subm it" id="submit "></ td>
</tr>
<tr>
<td>Remem ber? <input type="chec kbox" name="stay _in[]" checked="y es">< ;/td>
</tr>
</table>< br /> </form> <?
}//end else
?>

And then the admin_main.php file
--------------- --------------- --

<?

ob_start();
session_start() ;

$connect = mysql_connect(& quot;secret&quo t;, "secret&qu ot;, "secret&qu ot;
$select= mysql_select_db ("database ";

//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 `user_password` FROM `members` WHERE `username` = '".addslas hes($user).&quo t;'";
if (mysql_num_rows ($qu) == 1) {
$passw = mysql_fetch_obj ect($qu);
if ($passw->use r_password == $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;;
}
echo($user);

?>




    Viewed: 13,392 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Session login problem!!" Posted: @ 5:15 pm on Aug 03 2006    

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

try putting a session_start on login.php




-bs0d | AllSyntax.com

    Viewed: 13,384 Times | Reply to This | To top
  This user is online  Guest
  Subject: "re: Session login problem!!" Posted: @ 11:01 am on Aug 07 2006    
Unregistered

The Login works but why can't i "echo($us er);" I just get a blank page



    Viewed: 13,358 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Session login problem!!" Posted: @ 11:40 pm on Aug 07 2006    

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

Well, echo $user through each step of the code, and see where you lose it




-bs0d | AllSyntax.com

    Viewed: 13,349 Times | Reply to This | To top
  This user is online  Guest
  Subject: "re: Session login problem!!" Posted: @ 1:50 pm on Aug 09 2006    
Unregistered

Hello again!

Thanks for helping me. I try to echo $user everywhere i can in the admin_main page wher i do the login-check. But it won't work. Everything else works fine. Like i have to login to get to the pwd-protected area. Strange that i can login but not echo who's logged in.



    Viewed: 13,339 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: Session login problem!!" Posted: @ 2:13 pm on Aug 09 2006    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

you might also want to try print_r($user) just in case it's something that converts to an empty string but actually has a value.




please note that the above post is likely made up in its entirety.

    Viewed: 13,336 Times | Reply to This | To top
  This user is online  Guest
  Subject: "re: Session login problem!!" Posted: @ 8:12 am on Aug 10 2006    
Unregistered

Thanks for the reply but it still won't show the $user with:

print_r($user) ;



    Viewed: 13,326 Times | Reply to This | To top
  This user is online  Guest
  Subject: "re: Session login problem!!" Posted: @ 10:21 pm on Sep 11 2006    
Unregistered

I used the members tutorial. Got most things working. Register and login works fine. But when i redirect the logged in user, and echo the $user name the user is echoed as guest. What am i doing wrong?

This is the login.php file:
--------------- ------------

Code:


<?
ob_start();

//Database conn
$connect = mysql_connect( "secret&q uot;, "secret& quot;, "secret& quot;
$select= mysql_select_db ("1769800 ";

if(isset($_SESS ION['username ']) && isset($_SESSION ['password']) ) {
//REDIRECT TO USERS PROFILE...
header("L ocation: admin_main.php ";
} //end if logged in

//IF SUBMIT BUTTON PRESSED
if(isset($_POST ['submit'])) {

if(!$_POST['us ername']) die("Erro r: You must enter your username before logging in.";
if(!$_POST['pa ssword']) die("Erro r: You must enter your password before logging in.";

//set cookie if checked
if(!empty($_POS T['stay_in']) ) {
$joined =''.$_POST[' username'].'[ ]'.md5($_POST[ 'password']). '';
setcookie('log in_cookie', $joined, 2147483647, '/', '.mysite');
} //end if

//verify user...
$get_user = mysql_query(&q uot;SELECT * FROM `members` WHERE username = '".$_POS T['username'] ."' AND

user_password = '".md5($ _POST['passwor d'])."' ";
$q = mysql_fetch_obj ect($get_user);
if(!$q) die("Logi n Failure: An error occured, please verify your username and password are correct."< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;

//set session variables
$_SESSION['log ged_in'] = 1;
$_SESSION['use rname'] = $_POST['userna me'];
$_SESSION['pas sword'] = $_POST['passwo rd'];
session_write_c lose();

header("L ocation: admin_main.php ";

} else {
//show login form
?>
<form name="log in" method="p ost" action="& lt;? $_SERVER['PHP_ SELF']; ?>"> ;
<table>
<tr>
<td>Usern ame:<input type="tex t" id="usern ame" name="use rname"> ;</td>
</tr>
<tr>
<td>Passw ord:<input type="pas sword" id="passw ord" name="pas sword"> ;</td>
</tr>
<tr>
<td>Submi t: <input type="sub mit" value="Su bmit" name="sub mit" id="submi t">< ;/td>
</tr>
<tr>
<td>Remem ber? <input type="che ckbox" name="sta y_in[]" checked=" yes">& lt;/td>
</tr>
</table>
</form>
<?
}//end else
?>


And then the admin_main.php file
--------------- --------------- --

Code:

<?

ob_start();
session_start() ;

$connect = mysql_connect( "secret&q uot;, "secret& quot;, "secret& quot;
$select= mysql_select_db ("databas e";

//check cookie
if ($_SESSION['lo gged_in'] != 1 && isset($_COOKIE[ 'login_cookie '])) {
list($user, $pass) = explode('[]', $_COOKIE['logi n_cookie']);
$qu = mysql_query(&q uot;SELECT `user_password` FROM `members` WHERE `username` = '".addsl ashes($user).& quot;'";
if (mysql_num_rows ($qu) == 1) {
$passw = mysql_fetch_obj ect($qu);
if ($passw->use r_password == $pass) {
$_SESSION['log ged_in'] = 1;
$_SESSION['use rname'] = $user;
$_SESSION['pas sword'] = $pass;
}
}

}

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

?>



Edited at 10:56:23 pm on 09/11/06

    Viewed: 13,243 Times | Reply to This | To top
  This user is offline  missyani
  Subject: "re: Session login problem!!" Posted: @ 6:17 am on Nov 19 2006    
Member #: 53
Rank: User - (2)
Since: 06/25/05
Posts: 2

To echo the user name, I'm using $_SESSION['use rname']. The variable $user is in reference to "Guest&q uot;, not the name of the user that is logged in.



    Viewed: 13,083 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