TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-15-2008, 03:12 PM   #1 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default Starting Session Problem

Im having a small problem with a login script. When a user logs in the session is not stored, however if they log in again their session is started. I do not understand why, it is not every single time but almost every time.

On my login page it works something like this after the user hits the submit button.

PHP Code:
session_start();
...
//query to check user
...
$hour time() + 3600
setcookie(iSearchNotes$UserName$hour); 
setcookie(iSearchNotes$pass$hour);

$_SESSION["UserName"] = $UserName;
$_SESSION["UserId"] = $uID;
//forward to main page. 
then on the rest of the pages it will look like

PHP Code:
session_start();
...
if(isset(
$_SESSION['UserName']) and $_SESSION['UserName'] != '' and isset($_SESSION['UserId'])) {
echo 
'Welcome '.$_SESSION['UserName'].'- <a href="user_logout.php">Log out</a>';
.. 
This check almost always fails on the first login. Anyone see why this is? Also you can try it out at iSearchNotes.com - Make Your Notes Searchable
CoryMathews is offline  
Reply With Quote
Old 10-15-2008, 10:44 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I'm not sure where the problem exists within that code. Perhaps there is a flaw in your logic somewhere. Take a peak at the simple session code I have written for you. See if that helps you.
Attached Files
File Type: zip Session_Basics.zip (905 Bytes, 13 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
CoryMathews (10-15-2008)
Old 10-15-2008, 11:26 PM   #3 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Thanks, I will take a look at that in the morning and see what I can figure out from it.
CoryMathews is offline  
Reply With Quote
Old 10-27-2008, 11:30 PM   #4 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Well I finally figured it out.

The problem came down to a www and no www problem. Not a problem with my sessions.

It would work if the user came in to the page with www in front of the url because my form was forwarding to the homepage with a www. however someone without www on the url would log in and then be forwarded to a www and they would no longer be logged in unless they took the www back off.

One hell of an annoying bug, that I finally figured out on accident. Just thought I would let others know if they are having the same problems as well.
CoryMathews is offline  
Reply With Quote
Old 10-27-2008, 11:45 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Thanks. Isn't that down to the cookie path though? You set the path that is valid for the cookie in the fourth argument in setcookie. Alternatively, however, you could simply forward all www to non-www, and vice versa. That's the best way, I would say, because it's also better for SEO.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-27-2008, 11:48 PM   #6 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Ye gonna have to take a look at doing that, that is with the .htaccess correct?
CoryMathews is offline  
Reply With Quote
Old 10-28-2008, 01:19 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Yes, that's the one.

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^talkphp.com
RewriteRule (.*) http://www.talkphp.com/$1 [R=301,L]
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:33 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design