Enjoy unlimited access to all forum features for FREE! Optional upgrade available for extra perks.

PHP Session Help Please

Status
Not open for further replies.
Joined
Sep 23, 2007
Posts
841
Reaction score
64
I've been working on the following for hours and it defies all my current understanding and logic of PHP. Please Help :(

In its most basic form I have a form that determines what is saved in a session: (http://www.alexjordan.co.uk/session.php)

PHP:
<?php
session_start();
$session = $_SESSION['delivery'];
echo"<form action='http://www.alexjordan.co.uk/session_process.php' method='post'>
    <select name='delivery' id='delivery'>
	  <option value='Please Select'>Please Select</option>
	  <option value='Standard UK'>Standard UK (FREE)</option>
      <option value='Priority UK'>Priority UK (&pound;2.50)</option>
      <option value='International'>International (&pound;10.00)</option>
    </select>
<input id='action' name='action' value='add_delivery_method' type='hidden' />
<input name='submit' value='submit' type='submit' />
</form>";
echo "Session: $session<br/><br/>";
echo "[<a href='http://www.alexjordan.co.uk/session_process.php?action=remove'>End Session</a>]";
?>

When a value is chosen it is checked against a database and if it exists it will save the session, else it will not save the session.

PHP:
<?php
session_start();
include("mysql.php");//Connects to DB
$delivery = $_POST['delivery'];//Select Option from Session.php
$redirect = "Location: http://www.alexjordan.co.uk/session.php";
//
switch ($_REQUEST['action'])
	{
	case 'add_delivery_method':
		$query = "SELECT * FROM griptape_delivery WHERE title='$delivery' AND active='1' ";
		$result = mysql_query($query) or die(mysql_error());
		if (mysql_num_rows($result))//If delivery method is on DB
			{
			$_SESSION['delivery'] = "$delivery";
			header("$redirect?1");
			die();
			}
		else//Else Ignore and keep current session
			{
			header("$redirect?2");
			die();
			}
		break;
		
	case 'remove'://End session
		unset($_SESSION['delivery']);
		header("$redirect?3");
		die();
		break;
		
	default:
		header("$redirect?4");
		die();
	}
?>

If you choose 'Please Select' when no session exists the script works well and no session is created because 'Please Select' is not in the Database.

If you choose one of the other 3 values it also works, creating a session, as these values are in the database. They also replace each other, as intended, when selected.

HOWEVER once a session exists and you choose 'Please Select' it saves the value 'Please Select' as a session despite not being in the database.

Have I missed something here? I've been on it for about 5 hours and I've rewritten the script in many ways with the same result.

Any help is greatly appreciated.
 
Have you tried changing
PHP:
if (mysql_num_rows($result))//If delivery method is on DB
to
PHP:
if (mysql_num_rows($result) > 0)//If delivery method is on DB

Hi Alex, thanks for the suggestion, but unfortunately it makes no difference. The script with your alteration is live at http://www.alexjordan.co.uk/session.php so you can see it running.
 
What value to you get returned by mysql_num_rows ?

I get 0 for 'Please Select' and 1 for the other three values.

Thanks accelerator, I'll check it out. I posted on experts exchange, but no reply so far. Acorn members are much more reliable :p

Normally I see where I'm going wrong, but this has completely beaten my logic as I read it as if it should work, but it doesn't :(
 
Try changing
$query = "SELECT * FROM griptape_delivery WHERE title='$delivery' AND active='1' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result))//If delivery method is on DB

to
$query = 'SELECT * FROM griptape_delivery WHERE title="'.$delivery.'" AND active = "1" ';
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows) {
...
 
Hmm odd, it drops out with a ?2 on the url, so the mysql logic is obviously working.

Could you have register_globals enabled and the system is tripping up on having a session and post value of the same name?

Perfect! Rep coming your way! I don't know anything about servers, but checking register_globals on Google following your suggestion I uploaded a file called php.ini with register_globals = off as the script. It now works! Thank you! I just hope doing that doesn't negatively affect the rest of the site, but I'll soon find out.
 
Also as you're not actually using the result of the query use

$ query = 'SELECT count(title) FROM griptape_delivery WHERE title='$delivery' AND active='1' ";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);

print "rows[".$num_rows."]<br/>";

if ($num_rows) {
...
 
Try changing
$query = "SELECT * FROM griptape_delivery WHERE title='$delivery' AND active='1' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result))//If delivery method is on DB

to
$query = 'SELECT * FROM griptape_delivery WHERE title="'.$delivery.'" AND active = "1" ';
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows) {
...

Thanks for the suggestion, but it's working now.

Just to point out that Acorn Members are better than the members of the other forums this was posted on :p
 
Status
Not open for further replies.

The Rule #1

Do not insult any other member. Be polite and do business. Thank you!

Featured Services

Sedo - it.com Premiums

IT.com

Premium Members

AucDom
UKBackorder
Register for the auction
Acorn Domains Merch
MariaBuy Marketplace

Domain Forum Friends

Other domain-related communities we can recommend.

Our Mods' Businesses

Perfect
Service
Laskos
*the exceptional businesses of our esteemed moderators
Top Bottom