Solved

looping through mysql for php session

Posted on 2011-02-10
10
231 Views
Last Modified: 2012-05-11
I would like to validate new users email address, and the way I thought would be best is store a PHP session_id() in the mysql table users under the new user and then send an email, with a link containing the sessionId to click on: where my problem is, im looping through the result in the users to see if the sessionId already exists, and seems that I put myself in an infinite loop the code I have is below please help
function getSessionId()
	{
		$mySession = session_id();
		$goodSession = false;
		while(!$goodSession)
		{
			dbConnect("website.com","website","website","website");
			$verifySessionIdQuery = mysql_query("SELECT accountOpeningSessionId FROM accounts WHERE accountOpeningSessionId='" . $mySession . "'") or die(mysql_error());
			if(mysql_num_rows($verifySessionIdQuery))
			{
				$goodSession = false;
				$mySession = session_regenerate_id();					
			}
			else
			{
				$goodSession = true;
				break;
			}
			dbDisconnect();				
		}
		return $mySession;
	}

Open in new window

0
Comment
Question by:prowebinteractiveinc
10 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 34866968
Hi prowebinteractiveinc,

What are you testing for in line 9?
0
 

Author Comment

by:prowebinteractiveinc
ID: 34867024
im checking to see if the new session_id already exists
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 34867046
Okay, try this:

if(mysql_num_rows($verifySessionIdQuery) > 0)
0
 

Author Comment

by:prowebinteractiveinc
ID: 34867130
this is not where my problem is....  its the first loop, Im thinking
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 34867146
The while() loop? That looks correct.

What I'm thinking is your if() test as written always evaluates to true and that would send this process into a death spiral.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:prowebinteractiveinc
ID: 34867156
can the same php_session be created ?

on the other hand the if will only return true if there is a record...
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 34867169
>> on the other hand the if will only return true if there is a record...

I think mysql_num_rows() returns a value of 0, not null.  So even when there is no record, the if() still evaluates to true due to the presence of the integer.

Test it.  It's three keystrokes to prove me right or wrong.
0
 

Author Comment

by:prowebinteractiveinc
ID: 34867206
I tried: if(mysql_num_rows($verifySessionIdQuery) > 0) as you suggested, it didnt work !
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 34867660
"while(!$goodSession)" will never exit unless there is a match.  So if there isn't a match in your database, you're stuck right there.  It will repeatedly connect to the database and run the same query.
0
 
LVL 2

Accepted Solution

by:
requeue earned 500 total points
ID: 34868501
I agree with DaveBaldwin.

What's the purpose of the loop ?
the code below is enough, isn't it?

function getSessionId()
	{
		$mySession = session_id();
		dbConnect("website.com","website","website","website");
		$verifySessionIdQuery = mysql_query("SELECT accountOpeningSessionId FROM accounts WHERE accountOpeningSessionId='" . $mySession . "'") or die(mysql_error());
		if(mysql_num_rows($verifySessionIdQuery) == 0)
		{
			// no record exists, refresh session ID
			$mySession = session_regenerate_id();
		}
		dbDisconnect();				
		return $mySession;
	}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now