PHP Headache

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

PHP Headache

Postposted on Tue Mar 22, 2011 3:22 pm

I'm new to PHP and I'm only working with it because of pre-existing constraints. I've got some basics down but I've been stuck on what I feel is a really simple problem for a long time now. Here goes:

I have two pages. Page one dynamically echo's the html necessary to generate 2 radio buttons as a group. The onclick even of either button passes the value of the button to my second page which then calculates a value, updates a database, and passes back to the first page. Now, since the buttons were dynamically generated, based on what they chose before going to page two, it'd be nice to set that again for them so that it's not confusing.

Anyhow, I got the radio buttons generated just fine, they pass the parameters I expect, and I can echo out on page two the values, and they are what I expect. After I perform the calculation and database update, I echo the values yet again and again I get what I expect. Now here's the part I can't figure out: I am setting a session value on page2 for a calculated value that effects how the radio buttons on page1 are generated. If I echo this session variable out on page 2, I get results. if I call the same session variable (yes I'm aware PHP is strongly typed) on page1 I get nothing. Likewise the isset() function returns false.

Here's the code to create 2 radio buttons. The two situations are that the user has not ever selected a shipping option, or that they have and the button should be pre-selected.

Code: Select all

 <?php       
                     
                     if(!isset($_SESSION['ShipType'])){                  
                        echo "<input type='radio' name='shipping' id='standard' value='Standard' onClick=\"location.href='addShip.php?shipping=standard&numItems=$numProducts'\"> Standard Shipping<br/>";
                        echo "<input type='radio' name='shipping' id='rush' value='Rush' onClick=\"location.href='addShip.php?shipping=rush&numItems=$numProducts'\"> Rush Shipping<br/>";
                     }
                     else{                  
                        if($_SESSION['ShipType'] = "standard"){   
                           echo "<input type='radio' name='shipping' id='standard' value='Standard'  onClick=\"location.href='addShip.php?shipping=standard&numItems=$numProducts'\"> Standard Shipping $".$_SESSION['ShipCost']."<br/>";
                           echo "<input type='radio' name='shipping' id='rush' value='Rush' onClick=\"location.href='addShip.php?shipping=rush&numItems=$numProducts'\"> Rush Shipping $".$_SESSION['ShipCost']."<br/>";                        
                        }
                        else{
                           echo "<input type='radio' name='shipping' id='standard' value='Standard'  onClick=\"location.href='addShip.php?shipping=standard&numItems=$numProducts'\"> Standard Shipping $".$_SESSION['ShipCost']."<br/>";
                           echo "<input type='radio' name='shipping' id='rush' value='Rush' checked='true' onClick=\"location.href='addShip.php?shipping=rush&numItems=$numProducts'\"> Rush Shipping $".$_SESSION['ShipCost']."<br/>";
                        }
                     }
                  ?>


So reading through that, my intent was that if the session variable is not set, create two buttons with neither selected. If It is set, determine which should be checked. My second page echos the ShipType session variable every time. I have session_start on both pages. Admittedly I'm new to php and I don't really care for it, it's too much like classic asp. At any rate, I'd be grateful for any insight into what is probably inexperience on my part. Thanks.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11884
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA

Re: PHP Headache

Postposted on Tue Mar 22, 2011 4:22 pm

Code: Select all
if($_SESSION['ShipType'] = "standard") {.....}


Comparing the result of the assignment, or were you meaning to do a equality comparison ( == ) ? :)

http://www.php.net/manual/en/language.o ... arison.php
Amiga 1200, 68020@28MHz, 4MB+2MB RAM, Conner 80MB harddrive
FubbHead
Grand Gerbil Poohbah
 
Posts: 3483
Joined: Mon Apr 08, 2002 8:04 pm
Location: Borås, Sweden

Re: PHP Headache

Postposted on Tue Mar 22, 2011 4:27 pm

Disregard my post, I'm an idiot. Quality in PHP works like Javascript ==, not =. Bah.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11884
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA

Re: PHP Headache

Postposted on Tue Mar 22, 2011 4:28 pm

FubbHead wrote:
Code: Select all
if($_SESSION['ShipType'] = "standard") {.....}


Comparing the result of the assignment, or were you meaning to do a equality comparison ( == ) ? :)

http://www.php.net/manual/en/language.o ... arison.php

I figured this out a few minutes before you posted. I feel silly.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11884
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA

Re: PHP Headache

Postposted on Tue Mar 22, 2011 4:31 pm

steelcity_ballin wrote:
FubbHead wrote:
Code: Select all
if($_SESSION['ShipType'] = "standard") {.....}


Comparing the result of the assignment, or were you meaning to do a equality comparison ( == ) ? :)

http://www.php.net/manual/en/language.o ... arison.php

I figured this out a few minutes before you posted. I feel silly.


Been there, done that. :lol:
Amiga 1200, 68020@28MHz, 4MB+2MB RAM, Conner 80MB harddrive
FubbHead
Grand Gerbil Poohbah
 
Posts: 3483
Joined: Mon Apr 08, 2002 8:04 pm
Location: Borås, Sweden

Re: PHP Headache

Postposted on Wed Mar 23, 2011 9:23 am

You really should consider using a template engine like Smarty.

Mixing PHP and HTML might seem enticing at first, but will quickly become a convoluted mess.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9865
Joined: Fri Dec 27, 2002 7:51 pm
Location: Portugal (that's next to Spain)

Re: PHP Headache

Postposted on Wed Mar 23, 2011 12:14 pm

In compiled languages like C this type of issue will either cause a warning at compile time or be caught by a static analysis tool. A quick Google for "php static analysis" gave a number of results, you might want to try some of those on your code and see how good they are at catching things like this.
notfred
Grand Gerbil Poohbah
 
Posts: 3650
Joined: Tue Aug 10, 2004 9:10 am
Location: Ottawa, Canada

Re: PHP Headache

Postposted on Wed Mar 23, 2011 12:43 pm

I'm just doing this as a bandaid; in the meantime I'm having them transfer to .Net for a complete site rewrite. What struck me odd is that Dreamweaver (not known as a syntax nazi) would catch errors in code if it was something it didn't recognize, or undefined, etc. It wouldn't, however, catch something like casing mistake that I'm sure a more robust or rather a more php-targeted development environment would have. Either way, I solved my problem - What's funny to me is that had I been writing a javascript I would have noticed that right away. Because it was color coded I just didn't see it.

With .Net I have compiled over the years a nice few functions for security and session management, as well as a templating system via master pages and content place holders. Apples to oranges I know a competent php developer could deliver what I just don't want to spend the time to learn, and likely cheaper given the inherent costs of a windows environment.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11884
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA

Re: PHP Headache

Postposted on Wed Mar 23, 2011 1:17 pm

As an IDE, I suggest NetBeans. It'll at least raise a warning about using an assignment in a comparison.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9865
Joined: Fri Dec 27, 2002 7:51 pm
Location: Portugal (that's next to Spain)

Re: PHP Headache

Postposted on Mon Mar 28, 2011 3:54 pm

For what it's worth, Eclipse is one of the best IDEs out there (IMHO, anyway, for Java/script) and they now have an IDE for PHP. So you might want to check that out if you're going to be doing more PHP development.
Gigabyte Z77-DS3H | Core i7 3770 | 2x8 GB Crucial Ballistix DDR3-1600| Samsung 830 128GB SSD | Gigabyte HD7950 3GB | Win 7 Pro x86-64
CampinCarl
Graphmaster Gerbil
 
Posts: 1312
Joined: Mon Jul 04, 2005 8:53 pm

Re: PHP Headache

Postposted on Sun Apr 10, 2011 12:49 pm

I'm back!

Well I've got everything working properly, the paypal integration is done and tested and working. However I've run into absolutely an unsolvable issue on my end. One of my Client's main guys is testing from Hong Kong. Another is testing from Great Britain. Now in all our tests within the USA we've had no issues. However he was able to duplicate a problem that we are not. I should also make mention that there is no locale issues that I'm aware of, there is no code that carers where you are located.

I have a Session variable called cartid that tracks what you put in and take out of your cart. Works like a charm. However, our 2 across-the-pond friends get bizarre results. When they add an item to the cart, then choose shipping, it redirects them back to the cart review page and tells them their cart is empty. If they use the 'back button' and go back a page, when they return to the cart page it has (2) of the item they ordered and their old (original) cartid.

Is there anything you more knowledgeable php folks can tell me I'm missing? I have session_start(); on every page that uses session variables and like I said, I've had lots of people try to duplicate this problem without success. If you're curious, the site is http://wizardspeed.com. The page(s) in question are the shipping cart page and the php page that adds shipping. We thought we were ready to go live last night, and had a friend post a real order. PayPal has no record of it. I just placed an order for a test Item for $1.00 and it worked perfectly, all paypal notifications were sent etc. So back to my original question regarding php session variables. If I wanted to check the existence of one, here is what i wrote:

Code: Select all
//create new session cartid if one doesn't exist
 if (!isset($_SESSION['cartid'])){   
    $_SESSION['Payment_Amount'] = 0.00;
     $sql = "Insert into tblCart(datestamp) values('$strDateStamp')";
     $conn=mysql_connect($host, $user, $pwd) or die('Cannot connect to the database because: ' . mysql_error());
     mysql_select_db($dbname);
     mysql_query($sql) or die('This Query failed: . ' . mysql_error());                  
     $_SESSION['cartid'] = mysql_insert_id();    
 } 


No matter what myself and up to 5 other web-savvy folks (some programmers) tried, we could not get it to generate but one cartid without killing the session/clearing cache. I verified all browsers too, I just can't reproduce it. Help! Is there anything wrong with what I've written above? When the gentleman in HK adds an item to his cart, then chooses shipping (redirects, calculates, redirects back) his cartid has changed, and it's empty. :evil:
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11884
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA

Re: PHP Headache

Postposted on Sun Apr 10, 2011 1:47 pm

Your problem seems to be: wizardspeed.com vs. www.wizardspeed.com . The former works OK, the latter gets me that problem you mentioned.

Haven't done any in-depth investigation, but I'm guessing the problem is with the cookie domain.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9865
Joined: Fri Dec 27, 2002 7:51 pm
Location: Portugal (that's next to Spain)

Re: PHP Headache

Postposted on Sun Apr 10, 2011 1:49 pm

morphine wrote:Your problem seems to be: wizardspeed.com vs. http://www.wizardspeed.com . The former works OK, the latter gets me that problem you mentioned.

Haven't done any in-depth investigation, but I'm guessing the problem is with the cookie domain.


AH! That might explain it, I've seen this before in the form of SSL certificates, the prefix matters. I'll work over my code and check back. THANKS!

edit: I can't say for sure because our friends in GB and HK aren't available at this time, but I feel pretty sure you nailed it. I always forget nuances like that - thanks again.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11884
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 0 guests