Complete Insanity!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Complete Insanity!

Post by spacebiscuit »

Ok this is completely insane, I have never seen anything like this in over 11 years of programming in php.

Consider this code - it copies a URL posted via form into a session variable and then outputs the session to screen.

Code: Select all

for($x=0; $x<=100; $x++){
		 
     $_SESSION[toreport][$x]=$_POST["link"];
     print_r($_SESSION[toreport][$x]);
					
	                     } 
In Firefox it correctly outputs the url - 'http://...."

In IE it only output the first character - 'h'

I've not ever seen two different brwosers handle the same code differently - it is just not possible.

What is going on?

Rob.
Last edited by Benjamin on Thu Dec 16, 2010 4:50 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Complete Insanity!

Post by spacebiscuit »

Ok so it gets worse....

If change any letter in the variable name 'toreport' it outputs correctly:

torepora - OK!
toreporb - OK!
toreporc - OK!
torepord - OK!
torepore - OK!
toreport - No!

Seriously - WTF?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Complete Insanity!

Post by Jonah Bron »

First, fix your code like this:

Code: Select all

for ($x = 0; $x < 100; $x++) {
    $_SESSION['toreport'][$x] = $_POST['link'];
    print_r($_SESSION['toreport'][$x]);
}
And why do you need 100 copies of the same string?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Complete Insanity!

Post by cpetercarter »

Jonah Bron's suggestion is correct. Leaving the offset name "toreport" unquoted can have unexpected results.

However, the unexpected result should happen in the server, not in the browser, so I do not understand why the OP gets different results in FF and IE.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Complete Insanity!

Post by spacebiscuit »

Yes - adding the quotations fixed the problem - but like you say it makes no sense that browsers display the output differently since PHP is server-side.

In answer to your question Jonah - I did not copy my code correctly the post link variable is suffixed with a variable, for example:

$_POST["link{$x}"]

Such that the same URL is not copied 100 times, this enables me to copy upto 100 URLs instead.

Thanks for your help.

Rob.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Complete Insanity!

Post by Darhazer »

cpetercarter wrote:Jonah Bron's suggestion is correct. Leaving the offset name "toreport" unquoted can have unexpected results.

However, the unexpected result should happen in the server, not in the browser, so I do not understand why the OP gets different results in FF and IE.
Maybe there is an error reporting which interfere with site CSS and actually the output is the same in both browsers, but rendering is not ;)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Complete Insanity!

Post by Jonah Bron »

Darhazer wrote:Maybe there is an error reporting which interfere with site CSS and actually the output is the same in both browsers, but rendering is not ;)
Man, I love the way you can upvote posts on Stackoverflow.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Complete Insanity!

Post by Darhazer »

Jonah Bron wrote:
Darhazer wrote:Maybe there is an error reporting which interfere with site CSS and actually the output is the same in both browsers, but rendering is not ;)
Man, I love the way you can upvote posts on Stackoverflow.
sorry? I have no idea what you are talking about :?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Complete Insanity!

Post by spacebiscuit »

I'm glad it was not just me!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Complete Insanity!

Post by Jonah Bron »

Darhazer wrote:sorry? I have no idea what you are talking about :?
I just wish I could upvote your post :)
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Complete Insanity!

Post by spacebiscuit »

I can't find the verb 'upvote' in my Oxford English dictionary...
Post Reply