?
Solved

looping through mysql for php session

Posted on 2011-02-10
10
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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
 

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 84

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 2000 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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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