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
spacebiscuit
Forum Contributor
Posts: 390 Joined: Mon Mar 07, 2005 3:20 pm
Post
by spacebiscuit » Thu Dec 16, 2010 3:42 pm
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
Post
by spacebiscuit » Thu Dec 16, 2010 3:52 pm
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?
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Thu Dec 16, 2010 4:08 pm
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
Post
by cpetercarter » Fri Dec 17, 2010 2:16 am
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
Post
by spacebiscuit » Fri Dec 17, 2010 3:19 am
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.
Darhazer
DevNet Resident
Posts: 1011 Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria
Post
by Darhazer » Fri Dec 17, 2010 5:31 am
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
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Fri Dec 17, 2010 11:33 am
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.
Darhazer
DevNet Resident
Posts: 1011 Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria
Post
by Darhazer » Sat Dec 18, 2010 2:07 am
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
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Sat Dec 18, 2010 12:18 pm
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
Post
by spacebiscuit » Sat Dec 18, 2010 8:55 pm
I can't find the verb 'upvote' in my Oxford English dictionary...