javascript - Is json_encode extremely picky? -


It seems that json_encode inside my PHP file can have other stuff, which is fine, because I just Something I normally do in file A (with json_encode) in my file

I thought I would ask because I would rather update $ my $ d database with variables as $ _SESSION because json_encode does not seem to work when all the files I have in its file are in. For example, this code does not work:

  & lt ;? Php session_start (); Include 'dbcon.php'; $ SessionID = uniqid (); Echo json_encode ($ sessionID); If (hasset ($ _ POST ['clear session']) == '1') {$ query = "update currentID SET id = ('0')"; $ Execute = $ mysqli-> Query ($ query) or die ($ mysqli-> error .__ LINE__); } And {$ query = "update current ID SET id = ('$ sessionID')"; $ Result = $ mysqli- & gt; Query ($ query) or die ($ mysqli-> error .__ LINE__); }? & Gt;  

When I go to the file in my browser, I actually get json_encode results, though my javascript says that it does not import it correctly.

So, now I have just two PHP files:

   

That's the same thing in the first file, but at this time, my javascript imports correctly.

and

  & lt ;; Php session_start (); Include 'dbcon.php'; If (hasset ($ _ POST ['clear session']) == '1') {$ query = "update currentID SET id = ('0')"; $ Execute = $ mysqli-> Query ($ query) or die ($ mysqli-> error .__ LINE__); } And {$ sessionID = $ _SESSION ["session id"]; $ Query = "Update current ID SET id = ('$ sessionID')"; $ Result = $ mysqli- & gt; Query ($ query) or die ($ mysqli-> error .__ LINE__); }? & Gt;  

I think my question is, why does this happen? It seems silly that I have to store Unicode in one session so that my second PHP file is added to the database. Whereas if I was just a file, then I can update the database when I generate a new unix and avoid using $ _SESSION in the first place.

You have it:

  $ sessionID = uniqid (); Echo json_encode ($ sessionID); // "53d4c17abfe87"  

Since uniqid () produces a plain string, your output is not valid JSON, instead you will need something like this:

  $ sessionID = uniqid (); Echo json_encode (array ($ sessionID)); // ["53d4c17abfe87"]  

Why does json_encode () generate invalid JSON in the first place? Since sometime it is useful to generate partial JSON. For example, this is an easy move to inject the values ​​in the generated JavaScript code:

  var foo = & lt ;? = Json_encode ($ sessionID)? & Gt ;;  

Also applies:

Applies a superset of PHP JSON - This scalar type and zero will also encode and decode JSON standard only Supports values ​​when they are nested inside an array or an object.

To answer the question title:

Is json_encode extremely selective?

On the contrary, it is quite comfortable!


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -