Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2012, 09:59 PM   PM User | #1
dekon
New to the CF scene

 
Join Date: Dec 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
dekon is an unknown quantity at this point
Cool 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:........."); 
}

?>
dekon is offline   Reply With Quote
Old 12-13-2012, 01:57 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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";  

__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-13-2012 at 01:59 PM..
AndrewGSW is offline   Reply With Quote
Old 12-13-2012, 11:15 PM   PM User | #3
dekon
New to the CF scene

 
Join Date: Dec 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
dekon is an unknown quantity at this point
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 is offline   Reply With Quote
Old 12-14-2012, 01:57 AM   PM User | #4
dekon
New to the CF scene

 
Join Date: Dec 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
dekon is an unknown quantity at this point
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
dekon is offline   Reply With Quote
Old 12-14-2012, 05:49 PM   PM User | #5
EarthToHeaven
New to the CF scene

 
Join Date: Dec 2012
Location: Toronto, ON
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
EarthToHeaven is an unknown quantity at this point
Quote:
Originally Posted by dekon View Post
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
EarthToHeaven is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:33 PM.


Advertisement
Log in to turn off these ads.