looping through mysql for php session

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

prowebinteractiveincAsked:
Who is Participating?
 
requeueConnect With a Mentor Commented:
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
 
Jason C. LevineNo oneCommented:
Hi prowebinteractiveinc,

What are you testing for in line 9?
0
 
prowebinteractiveincAuthor Commented:
im checking to see if the new session_id already exists
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jason C. LevineNo oneCommented:
Okay, try this:

if(mysql_num_rows($verifySessionIdQuery) > 0)
0
 
prowebinteractiveincAuthor Commented:
this is not where my problem is....  its the first loop, Im thinking
0
 
Jason C. LevineNo oneCommented:
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
 
prowebinteractiveincAuthor Commented:
can the same php_session be created ?

on the other hand the if will only return true if there is a record...
0
 
Jason C. LevineNo oneCommented:
>> 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
 
prowebinteractiveincAuthor Commented:
I tried: if(mysql_num_rows($verifySessionIdQuery) > 0) as you suggested, it didnt work !
0
 
Dave BaldwinFixer of ProblemsCommented:
"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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.