<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Escaping JavaScript for PHP</title>
	<atom:link href="http://sixohthree.com/241/escaping/feed" rel="self" type="application/rss+xml" />
	<link>http://sixohthree.com/241/escaping</link>
	<description>The Weblog of Adam Backstrom</description>
	<lastBuildDate>Sun, 01 Jan 2012 21:48:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Ron Lowe</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-4186</link>
		<dc:creator>Ron Lowe</dc:creator>
		<pubDate>Sat, 27 Aug 2011 09:42:38 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-4186</guid>
		<description>I know it is a long time after the others but I have tried the above without success.

I need to pass a mysql string output via PHP to a javascript function. I have now found (elsewhere on the net) a method that works.

$test=&quot;&amp;#39clin1cal0001&amp;#39&quot;;

echo &quot;&lt;a HREF=&#039;&#039; rel=&quot;nofollow&quot;&gt;Click 

this&lt;/A&gt;&quot;;

The $test is passed to function and appears in an alert there. PHEW!!</description>
		<content:encoded><![CDATA[<p>I know it is a long time after the others but I have tried the above without success.</p>
<p>I need to pass a mysql string output via PHP to a javascript function. I have now found (elsewhere on the net) a method that works.</p>
<p>$test=&#8221;&amp;#39clin1cal0001&amp;#39&#8243;;</p>
<p>echo &#8220;&lt;a HREF=&#8221; rel=&#8221;nofollow&#8221;>Click </p>
<p>this&lt;/A>&#8221;;</p>
<p>The $test is passed to function and appears in an alert there. PHEW!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Backstrom</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-3436</link>
		<dc:creator>Adam Backstrom</dc:creator>
		<pubDate>Mon, 10 May 2010 12:38:35 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-3436</guid>
		<description>Naresh, you should be using htmlentities() rather than javascript_escape() in that context, since you&#039;re not embedding your string into JavaScript.</description>
		<content:encoded><![CDATA[<p>Naresh, you should be using htmlentities() rather than javascript_escape() in that context, since you&#8217;re not embedding your string into JavaScript.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naresh Kumar</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-3433</link>
		<dc:creator>Naresh Kumar</dc:creator>
		<pubDate>Thu, 06 May 2010 09:21:44 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-3433</guid>
		<description>Hello There,

It is not working for me. Could anyone please in this..

Here is my code:

&lt;code&gt;
&lt;?php
&#160;&#160;&#160;&#160;function javascript_escape1($str) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$new_str = &#039;Test&#039;;

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$str_len = strlen($str);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for($i = 0; $i &lt; $str_len; $i++) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$new_str .= &#039;\x&#039; . dechex(ord(substr($str, $i, 1)));
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return $new_str;
&#160;&#160;&#160;&#160;}
?&gt;
&lt;form method=&quot;post&quot;&gt;&lt;select onchange=&quot;alert(this.value)&quot; name=&quot;mysel&quot;&gt;
&lt;option value=&#039;&lt;? &#160;echo javascript_escape1(&quot;T&#039;st&quot;); ?&gt;&#039; &gt;T&#039;st&lt;/option&gt;
&lt;option value=&#039;two&#039;&gt;Two&lt;/option&gt;
&lt;/select&gt;
&lt;input type=&quot;submit&quot; value=&quot;test&quot;&gt;
&lt;/form&gt;
&lt;?php
&#160;&#160;&#160;&#160;if ($_POST) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;echo &quot;&lt;pre&gt;&quot;; print_r($_POST);echo &quot;&lt;/pre&gt;&quot;;
&#160;&#160;&#160;&#160;}
?&gt;
&lt;/code&gt;


Thanks in advance.</description>
		<content:encoded><![CDATA[<p>Hello There,</p>
<p>It is not working for me. Could anyone please in this..</p>
<p>Here is my code:</p>
<p><code><br />
&lt;?php<br />
&nbsp;&nbsp;&nbsp;&nbsp;function javascript_escape1($str) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$new_str = 'Test';</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str_len = strlen($str);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for($i = 0; $i &lt; $str_len; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= '\x' . dechex(ord(substr($str, $i, 1)));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $new_str;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
?&gt;<br />
&lt;form method="post"&gt;&lt;select onchange="alert(this.value)" name="mysel"&gt;<br />
&lt;option value='&lt;? &nbsp;echo javascript_escape1("T'st"); ?&gt;' &gt;T'st&lt;/option&gt;<br />
&lt;option value='two'&gt;Two&lt;/option&gt;<br />
&lt;/select&gt;<br />
&lt;input type="submit" value="test"&gt;<br />
&lt;/form&gt;<br />
&lt;?php<br />
&nbsp;&nbsp;&nbsp;&nbsp;if ($_POST) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "&lt;pre&gt;"; print_r($_POST);echo "&lt;/pre&gt;";<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
?&gt;<br />
</code></p>
<p>Thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JN</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-3343</link>
		<dc:creator>JN</dc:creator>
		<pubDate>Fri, 13 Nov 2009 23:35:22 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-3343</guid>
		<description>&lt;code&gt;
function js_escape($str) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for($i = 0, $l = strlen($str), $new_str=&#039;&#039;; $i &lt; $l; $i++)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$new_str .= (ord(substr($str, $i, 1)) &lt; 16 ? &#039;\\x0&#039; : &#039;\\x&#039;)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;. dechex(ord(substr($str, $i, 1)));
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return $new_str;
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><code><br />
function js_escape($str) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for($i = 0, $l = strlen($str), $new_str=''; $i &lt; $l; $i++)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= (ord(substr($str, $i, 1)) &lt; 16 ? &#039;\\x0&#039; : &#039;\\x&#039;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;. dechex(ord(substr($str, $i, 1)));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $new_str;<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nightfly</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-292</link>
		<dc:creator>Nightfly</dc:creator>
		<pubDate>Fri, 22 May 2009 22:06:54 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-292</guid>
		<description>For super tidy string iteration, I always use this:

for($i = 0, $l = strlen($str); $i &lt; $l; $i++) {
&#160;&#160;&#160;&#160;$new_str .= ‘\\x’ . dechex(ord(substr($str, $i, 1)));
}</description>
		<content:encoded><![CDATA[<p>For super tidy string iteration, I always use this:</p>
<p>for($i = 0, $l = strlen($str); $i &lt; $l; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= ‘\\x’ . dechex(ord(substr($str, $i, 1)));<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Backstrom</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-291</link>
		<dc:creator>Adam Backstrom</dc:creator>
		<pubDate>Thu, 31 Jul 2008 20:56:45 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-291</guid>
		<description>tedivm: good point. I&#039;ve updated the post to reflect your suggestion.</description>
		<content:encoded><![CDATA[<p>tedivm: good point. I&#8217;ve updated the post to reflect your suggestion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tedivm</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-289</link>
		<dc:creator>tedivm</dc:creator>
		<pubDate>Thu, 31 Jul 2008 18:17:39 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-289</guid>
		<description>You should take the &quot;strlen($str)&quot; check and put it outside of the for loop. The reason is that function calls use up a lot more resources than simple variable checks, and where the strlen function currently is it will be called for each interation of the loop (so if the string has a length of 50, that function will be called fifty times).

$strLen = strlen($str);
for($i = 0; $i &lt; $strLen; $i++) {
&#160;&#160;&#160;&#160;$new_str .= &#039;\\x&#039; . dechex(ord(substr($str, $i, 1)));
}</description>
		<content:encoded><![CDATA[<p>You should take the &#8220;strlen($str)&#8221; check and put it outside of the for loop. The reason is that function calls use up a lot more resources than simple variable checks, and where the strlen function currently is it will be called for each interation of the loop (so if the string has a length of 50, that function will be called fifty times).</p>
<p>$strLen = strlen($str);<br />
for($i = 0; $i &lt; $strLen; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= &#8216;\\x&#8217; . dechex(ord(substr($str, $i, 1)));<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pascal</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-290</link>
		<dc:creator>Pascal</dc:creator>
		<pubDate>Thu, 31 Jul 2008 13:23:12 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-290</guid>
		<description>I see. And I guess using htmlentities(strip_tags($myVar)) would break the javascript. Good script then.</description>
		<content:encoded><![CDATA[<p>I see. And I guess using htmlentities(strip_tags($myVar)) would break the javascript. Good script then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Backstrom</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-288</link>
		<dc:creator>Adam Backstrom</dc:creator>
		<pubDate>Thu, 31 Jul 2008 12:41:44 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-288</guid>
		<description>Pascal,

Here&#039;s an example of &lt;a href=&quot;http://static.bwerp.net/~adam/2008/07/31/js.php&quot; rel=&quot;nofollow&quot;&gt;addslashes() allowing XSS&lt;/a&gt;. It&#039;s not sufficient to block all hack attempts. That page will load off-domain JavaScript in Firefox 3.0.1, Opera 9.51, Safari 3.1.2, and Internet Explorer 7.0.6001.18000.</description>
		<content:encoded><![CDATA[<p>Pascal,</p>
<p>Here&#8217;s an example of <a href="http://static.bwerp.net/~adam/2008/07/31/js.php" rel="nofollow">addslashes() allowing XSS</a>. It&#8217;s not sufficient to block all hack attempts. That page will load off-domain JavaScript in Firefox 3.0.1, Opera 9.51, Safari 3.1.2, and Internet Explorer 7.0.6001.18000.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pascal</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-287</link>
		<dc:creator>Pascal</dc:creator>
		<pubDate>Thu, 31 Jul 2008 01:38:45 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-287</guid>
		<description>Why not just use addslashes($myvar)?</description>
		<content:encoded><![CDATA[<p>Why not just use addslashes($myvar)?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

