开发者

Storing a large variable in HTML5 persistent storage from an action run in a PHP script

I will begin by briefing as to what this actually should do...

Fetch the entire contents of a web page, turn into a string, and save into persistant storage. However for some reason, it just... wont?

Ive used php's html entities, and then JSON Stringify, however it just fails to work.

My code is as follows...

//arrays set above

$url =  "http://www.google.co.uk";

$handle = fopen($url, "r");

$contents = stream_get_contents($handle);

$contents = htmlentities($contents);

echo "<script lang='te开发者_StackOverflow社区xt/javascript'>var dataString = JSON.stringify('".$contents."'); tokens[".$t." = ".$rowtokens[5]."]; toStore[".$t." = dataString]; alert('CONTENT'); </script>";

EDIT :

That source code renders the following

<script lang='text/javascript'>tokens[0 = tokenvalue here]; toStore[0 = "&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD X... 
//All the rest of the html of the page.
"];localStorage.setItem(token[0], toStore[0]);</script>


You mean:

tokens['".$t."'] = '".$rowtokens[5]."';

Currently it is evaluating to:

tokens[something = test];

which is invalid and does not do what you want:

  • Everything is happening inside the property name; nothing is being set
  • You don't have quotes which will probably mess up things

If your code returns this:

<script lang='text/javascript'>tokens[0 = tokenvalue here]; toStore[0 = "&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD X... 
//All the rest of the html of the page.
"];localStorage.setItem(token[0], toStore[0]);</script>

then it's not valid:

  • It's <script type='text/javascript'>
  • I don't know what you mean with 0 = tokenvalue here (you're storing something in the number 0, which is not possible). Don't you mean tokens[0] = tokenvalue?
  • There are newlines, so you should remove them as you currently have an unterminated string


Found the solution after a quick search

Common sources of unterminated string literal

It was the line breaks from php code that was killing it.

This fixed it nicely

$str = str_replace(array("\r", "\n"), '', $str);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜