Tutorial :javascript php array


i have a js array like this:

var myArray = [];  myArray[1] = 'test';  myArray[2] = 'test';  

-i want to hide it from users to see it. how can i store just the array in a php script and call it?

right now i have the .js separate from my form. and i just call it. but anyone can easily view source and visit the url using the .js name

-another question i have is to hide a url values from the user. i have something like this: www.test.ca/people.php?id=12

i want to hide the values. thanks


For the JS code, if the browser has to execute it, then the user can see it. Not much you can do.

If you want to carry values between pages and you don't want them to be seen, don't use a query string -- use PHP sessions instead.


All Javascript code is viewable from the client. There really is no way around this.

Even an AJAX call can be viewed via a good browser plugin.


Javascript is a client-side executed script, so you won't ever be able to hide it. You can encrypt it, you can make it difficult to view it, but that's pretty useless.

Just put it in your sources, or if you want to hide it a little further, get the array with an AJAX call, and make the call show nothing when it's not called with AJAX (the array can still be revealed with developper browser plugins, or with being hacked adding extra headers.
Here's the PHP condition code : if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])

Don't try to make it harder then that, it will be a waste of time.


Think that the browser is a transparent box. Everything you want to hide, needs to sit on the server.


If you want to send data across multiple pages, you have two options -

  1. Use PHP Sessions
  2. Use hidden fields

I would recommend the second option, because PHP sessions have the same problem as using a global variable, i.e., you can't use the same key in the whole applications, it is harder to maintain a session etc.

You can't hide a JS code from the user, because the browser will certainly execute it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »