[SOLVED] PHP/Javascript/IE Cookie Problem

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
romulus15
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 12:30 pm

[SOLVED] PHP/Javascript/IE Cookie Problem

Post by romulus15 »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color] 


Ok...this might get a bit complicated, but I'll do my best to keep it simple.

We're creating a dynamic news web site.  I am using PHP to pull the header POST info (multiple items), putting it in a variable seperated by commas, and using it to set a cookie.  The cookie is set using JavaScript so I can set a cookie in the middle of the page, rather than in the header.

Here is my code as it sits now (say POST contains 3 items, 1, 2 and 3):

Code: Select all

<?
	foreach ($HTTP_POST_VARS as $posted)
	{

			$cookie_value .= (string)$posted . ",";
	}
	
	$cookie_value = rtrim($cookie_value, ',');
	
	echo "<script type="text/javascript">\r";
	echo "<!--\r";
	echo "setCookie("atshome","" . $cookie_value . "",720);\r";
	echo "alert(getCookie("atshome"));\r";
	echo "//-->\r";
	echo "</script>\r";
?>

Ok...not here comes the strange part. That code works fine in FireFox, setting the cookie to e.g. 1,2,3

In IE 6, I get the following when I go to view source:

Code: Select all

&lt;script type="text/javascript"&gt;
&lt;!--
setCookie("atshome","1,
3,
5",720);
alert(getCookie("atshome"));
//--&gt;
&lt;/script&gt;
Obviously, IE is breaking my setCookie's second parameter on the commas.

If I set a variable, say $test_value = "1,2,3" and replace the variable in the setCookie function, it works fine in both FireFox and IE.

Can anyone spot something that I'm doing wrong, or is there a quirk with IE that I need to work around?

If you want to see the code in action go to: http://home.advantechcny.com

Click on customize, select a couple of checkboxes and click submit. IE will also give an "Undertermined string constant" error, and if you view the source, you should see the same as I am getting.

Thanks for your help guys. I've had my partner look at the code as well and he can't figure anything out either.


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]

it's not the commas that are the problem. Javascript, at least IE's implementation, doesn't support line spanning rows. You should probably use a function like [php_man]trim[/php_man]() to remove all whitespace around the variable being input.
romulus15
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 12:30 pm

Post by romulus15 »

This is not a line spanning row, and there is no whitespace in the variable.

The actual variable is 1,2,3

It works fine in FireFox, with no spaces, etc between the number.

Just as a clairifaction, if I don't append the commas in the foreach loop, it still breaks the line after 1 and 2.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well.. IE wouldn't automatically do that, so there's something your code must be doing or similar...
romulus15
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 12:30 pm

Post by romulus15 »

Well the exact code is listed in my first post. So what in my code would cause it to do what it does?

How can it possibly work flawlessly in FireFox and be fubar in IE?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the solution I posted earlier is the fix. You have asked the browser to return a character return in the attribute, noted by this code on the customize page:

Code: Select all

<input type="checkbox" name="1" value="1">ABC World News</td><td width="210"><input type="checkbox" name="
2" value="
2">ABC US News</td></tr><tr><td width="210"><input type="checkbox" name="
3" value="
3">ABC Politics</td><td width="210"><input type="checkbox" name="
4" value="
4">ABC MONEYScope</td></tr><tr><td width="210"><input type="checkbox" name="
5" value="
5">ABC Health</td><td width="210"><input type="checkbox" name="
6" value="
6">ABC Entertainment</td></tr><tr><td width="210"><input type="checkbox" name="
7" value="
7">ABC Travel</td><td width="210"><input type="checkbox" name="
8" value="
8">ABC World News Tonight</td></tr><tr><td width="210"><input type="checkbox" name="
9" value="
9">ABC 20/20</td><td width="210"><input type="checkbox" name="
10" value="
10">ABC Primetime</td></tr><tr><td width="210"><input type="checkbox" name="
11" value="
11">ABC Nightline</td><td width="210"><input type="checkbox" name="
12" value="
12">ABC This Week</td></tr><tr><td width="210"><input type="checkbox" name="
13" value="
13">ABC Good Morning America</td><td width="210"><input type="checkbox" name="
14" value="
14">Slashdot - "News for Nerds"</td></tr><tr><td width="210"><input type="checkbox" name="
15" value="
15">The Register - IT News</td><td width="210"><input type="checkbox" name="
16" value="
16">ABC Science and Technology</td></tr><tr><td width="210"><input type="checkbox" name="
17" value="
17">ESPN Top Headlines</td><td width="210"><input type="checkbox" name="
18" value="
18">ESPN NFL</td></tr><tr><td width="210"><input type="checkbox" name="
19" value="
19">ESPN NBA</td><td width="210"><input type="checkbox" name="
20" value="
20">ESPN MLB</td></tr><tr><td width="210"><input type="checkbox" name="
21" value="
21">ESPN NHL</td><td width="210"><input type="checkbox" name="
22" value="
22">ESPN College Basketball</td></tr><tr><td width="210"><input type="checkbox" name="
23" value="
23">ESPN College Football</td><td width="210"><input type="checkbox" name="
24" value="
24">ABC Sports</td></tr>
As you can see, you actually have carriage returns in your values. So Firefox was either smart or stupid enough to "trim" that information off.. :)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I'd say stupid. But then again it is smart to clean up after us like that. ;)

Some helpful research links:
[big_search]php linefeed variables[/big_search]
romulus15
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 12:30 pm

Post by romulus15 »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Thanks for the help so far, but now you've got me stumped as to where the linebreak is comming from.

Here's the code that generates the form (note, echo "<input types..." are on one line, the BB width breaks it into 2:

Code: Select all

<?
		/*==================================================
		Read the RSS feeds from the feeds.txt file.  
		Feeds.txt has a format of:
		Feed Title,URL
		This breaks each line at the comma and stors the
		resulting string in the splitcontents array.
		==================================================*/
		$counter = 0;
		$filename = "feeds.txt";
		$fd = fopen ($filename, "r");
		$contents = fread ($fd,filesize ($filename));
		fclose ($fd);
		$delimiter = ",";
		$splitcontents = explode($delimiter, $contents);
		$splitcontents = rtrim($splitcontents, "\n\r");
		$splitcontents = ltrim($splitcontents, "\n\r");
		
		//Start select_feeds form to select news feeds
		echo "<form action="setprefs.php" method="post" name="select_feeds" id="select_feeds">";

		//Populate select_feeds form with feeds.
		for($i = 0; $i < count($splitcontents); $i = $i + 6)
		{
			echo "<tr><td width="210">";
			echo "<input type="checkbox" name="".$splitcontents[$i]."" value="".$splitcontents[$i]."">".$splitcontents[$i+1];
			echo "</td>";
			echo "<td width="210">";
			echo "<input type="checkbox" name="".$splitcontents[$i+3]."" value="".$splitcontents[$i+3]."">".$splitcontents[$i+4];
			echo "</td></tr>";
		}
		echo "<tr><td>&nbsp;</td></tr>";
		echo "<tr><td colspan=2 align=center>";
    	echo "<input type="submit" value="Submit">";
		echo "</td></tr>";
		echo "</p></form>";
		//End select_feeds form to select news feeds
	?>
Here's an example of the comma delimited file it's parsing:

Code: Select all

1,ABC World News,http://my.abcnews.go.com/rsspublic/world_rss20.xml,
2,ABC US News,http://my.abcnews.go.com/rsspublic/us_rss20.xml,
3,ABC Politics,http://my.abcnews.go.com/rsspublic/politics_rss20.xml,

Even after doing an rtrim and ltrim to trim out the carriage returns and line feeds, IE still breaks it up.

Am I missing something stupidly obvious as usual? :roll:


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you didn't read the Posting Code thread.... :evil::roll:

just use [php_man]trim[/php_man](). Do not pass the second argument.
romulus15
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 12:30 pm

Post by romulus15 »

Sorry about not posting correctly, my fault.

As far as just using trim, it doesn't help the situation at all, i.e. it does nothing for me.

Trim would just strip the whitespace from the beginning and end of the string, of which there is none. I figured if I passed in the carriage return and line feed, it would pull them out if they existed in the string, which does nothing in my situation as well.

That's the thing that bothers me. Nowhere in my code do I see the possibility of line feeds being introduced into the variable.

It takes the value of the first POST var, add a comma, value of second post var, comma, etc. The only way that a line feed could be introduced is if one existed on the POST var, which it can't that I know of.

Am I wrong in thinking this way?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Code: Select all

$splitcontents = explode($delimiter, $contents);
$splitcontents = rtrim($splitcontents, "\n\r");
$splitcontents = ltrim($splitcontents, "\n\r");
You're trying to trim an array, try:

Code: Select all

$splitcontents = explode($delimiter, $contents);
foreach ($splitcontents as $key => $value) {
    $splitcontents[$key] = trim($value);
}
Mac
romulus15
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 12:30 pm

Post by romulus15 »

Thank you, thank you. It works perfectly now. Forgive the brain fart, totally forgot that it was an array. Maybe it's time to start using hungarian notation in my PHP :D

Just for my knowledge, do you know why the line feed was introduced?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

customize.php is outputting it.. as I've said now plenty of times.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

romulus15 wrote:Just for my knowledge, do you know why the line feed was introduced?
because your file contained it. In binary form it looks like:

Code: Select all

blah,blah,blah,<CR>blah,blah,blah....
where <CR> stands for linefeed/carriage return sequence. Therefore after exploding resulting array looks like:

Code: Select all

Array(7) &#123;
  &#1111;0] => "blah"
  &#1111;1] => "blah"
  &#1111;2] => "blah"
  &#1111;3] => "<CR>blah"
  &#1111;4] => "blah"
  &#1111;5] => "blah"
  &#1111;6] => "blah...."
&#125;
with linefeed/carriage return preserved in the fourth element
romulus15
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 12:30 pm

Post by romulus15 »

Thank you, that's why I wanted to know.

It's strange how FireFox handled it and removed the carriage returns automatically.
Post Reply