CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   session state help (http://www.codingforums.com/showthread.php?t=284054)

dekon 12-12-2012 09:59 PM

session state help
 
this is part of the code that i have on my login screen
PHP Code:

$sql="SELECT * FROM user WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword,table is 1 row
if($count==1){

$_SESSION['myusername'] = $myusername;
header("location:.........");
}
else {
echo 
"Wrong Username or Password";
}
?> 

and this is the other part of the code where i want it so that if a user is not logged in that they will be redirected to the login page to either register their account or login could someone give me some help as to what i'm doing wwong here since it still lets you access the page without being logged on
PHP Code:

<?PHP
session_start
();

if(isset(
$_SESSION['myusername'])){ 
//session is set, user is logged in 
}else{ 
header("location:........."); 
}

?>


AndrewGSW 12-13-2012 01:57 PM

PHP Code:

else { 
    unset(
$_SESSION['myusername']);
    echo 
"Wrong Username or Password"


..is one way. Or, preferably:
PHP Code:

else {  
    if (isset(
$_SESSION['myusername'])) {
        unset(
$_SESSION['myusername']);
    }
    echo 
"Wrong Username or Password";  



dekon 12-13-2012 11:15 PM

you are still able to open the other page without being logged on since it don't redirect to the login page if not logged in

dekon 12-14-2012 01:57 AM

i managed to get it to redirect but it don't when i type in the correct username and password and click on the page it still redirects me to login page

EarthToHeaven 12-14-2012 05:49 PM

Quote:

Originally Posted by dekon (Post 1299437)
this is part of the code that i have on my login screen
PHP Code:

$sql="SELECT * FROM user WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword,table is 1 row
if($count==1){

$_SESSION['myusername'] = $myusername;
header("location:.........");
}
else {
echo 
"Wrong Username or Password";
}
?> 

and this is the other part of the code where i want it so that if a user is not logged in that they will be redirected to the login page to either register their account or login could someone give me some help as to what i'm doing wwong here since it still lets you access the page without being logged on
PHP Code:

<?PHP
session_start
();

if(isset(
$_SESSION['myusername'])){ 
//session is set, user is logged in 
}else{ 
header("location:........."); 
}

?>


Hi Decon,

1)
Did you try to delete your cookies. press Ctrl + Shift + Del and then re-try same thing.

2)
Also try if($count=='1') instead of if($count==1)

3)
Try to echo your sql queries. This will get you valuable information.

Let me know what happens.

Regards,
Nehal


All times are GMT +1. The time now is 09:51 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.