Page 1 of 1

Javascript/PHP: Insta Create Javascript Vars via PHP

Posted: Fri Mar 19, 2004 2:20 pm
by randomblink
Ok...
I really only need to know whether my idea is possible. I will try to figure out HOW to make it happen once I know what the general consensus is.

I build an HTML page.
I create a *.js file to use in it.
In HTML I add the <script> tags to reference the *.js file.
The page is visited by someone.
The page gets loaded.
The javascript is referenced.
All vars in the *.js file are loaded and now available.
Everything is great.

However, ANYONE can visit my *.js file if they are savvy enough.
I know that PHP can create and delete files.

Is it POSSIBLE to do the following:

I build a PHP file that will create a webpage.
I create a BLANK *.js file (or maybe don't create one at all).
I write PHP to put together the variables I want to reference.
The page is visited by someone.
The page gets loaded.
The PHP creates a (or opens the blank one) *.js file.
It drops all the vars in that I want it to.
The webpage just created references the NEWLY create or edited*.js file.
All vars in the *.js file are loaded and now available.
The PHP then deletes or empties the *.js file.

???

I am trying to generate javascript vars without giving the user a way to peek. I want to put together a game but don't want to generate the js vars in a place that the user can just by hitting VIEW SOURCE... ugh!

Any help would be appreciated...

Posted: Fri Mar 19, 2004 2:43 pm
by andre_c
Using a system like that would make it more difficult but not impossible

Re: Javascript/PHP: Insta Create Javascript Vars via PHP

Posted: Fri Mar 19, 2004 2:50 pm
by TheBentinel.com
I get what you're saying. I see a few hitches you'll hit.

You won't easily know when the browser has actually fetched your .js file. It might be doing other things, like illegally downloading movies or something, and be pretty slow about grabbing your file.

If you work past that, the user STILL has a copy of your .js file on his drive, in the cache. So even a half-savvy user (like a gamer) would be able to get at it.

Are you using sessions? Maybe you could have a PHP file that outputs the .js text, but only for an active session. So the html you dump looks like this:

Code: Select all

<script src="myScript.php?sid=123445"></script>
Then your myScript.php file outputs the real data only if the sid is valid. If it isn't, then it outputs nothing, or garbage.

Re: Javascript/PHP: Insta Create Javascript Vars via PHP

Posted: Fri Mar 19, 2004 3:15 pm
by randomblink
TheBentinel.com wrote: If you work past that, the user STILL has a copy of your .js file on his drive, in the cache. So even a half-savvy user (like a gamer) would be able to get at it.
I can deal with that... See I am wanting to put together a card game, so that users can play a particular CCG I enjoy (Collectible Card Game for the non-CCG'ers). And I think I could garble the js stuff enough so it MIGHT be unreadable. But when they ONLY have to click VIEW SOURCE to get the list of cards the opponent has in his hands, well... that is just too tempting. But that is something to consider for sure...
TheBentinel.com wrote: Are you using sessions? Maybe you could have a PHP file that outputs the .js text, but only for an active session. So the html you dump looks like this:

Code: Select all

<script src="myScript.php?sid=123445"></script>
Then your myScript.php file outputs the real data only if the sid is valid. If it isn't, then it outputs nothing, or garbage.
Please expand... This sounds promising, but I am not sure I understand...
The sid? That is a session id? I guess I need to read up more on sessions. I know how to set session vars, but not deal with session ids...

See, the idea is, I want to put this game together with JUST:
1) PHP - outputting strict xHTML
2) CSS for layout design
3) Javascript for nice look and feel

In a CCG the users put together their own decks. So I had planned on having PHP gather the cards from each deck, their hands, and those in play via MySQL. But I want the game to be a little smoother than refreshing the dang screen every 10 seconds... AND I don't want the users to be able to quick look at the js file to see what cards their opponent has in their hands nor what the next card is in their own draw deck.

I wish js could grab data INTO a currently open window FROM a database. Then I could just load the main screen via php and run the game via js. But it can't unless you use iframes or frames. And I am vehemently against that since there are no frame setups that are ADA compliant. I want anyone/everyone to be able to play. Ugh!

With a game being turn-bound, usually you play your round then wait for the opponent. But sometimes, some cards interact with the other player... Maybe your card lets you grab a random card from the opponents hand... or maybe you get to access a card in play on the oppponents side... But if the opponent has certain cards in play, they sometimes get to cancel your interaction, or adjust it, maybe forcing you to access a card from a different area than you wanted... ugh! So much... I am still in the drawing board phase, but I BELIEVE all that I want to do is possible, so I will keep looking...

But if you could expand on your sid=XXXX idea that would be great!
Thanks...

Oh, and thanks to everyone giving their two-cents worth. I can use all the help I can get...

Posted: Fri Mar 19, 2004 3:29 pm
by TheBentinel.com
Anybody that liked Galaxy Quest is all right in my book. ;-)

There's a component for pulling down data from within a page. It's the microsoft.xmlhttp object. Here's some vbscript that you can translate to javscript:

Code: Select all

<script language="vbscript">
  set http = createobject("microsoft.xmlhttp")
  http.open "GET", "http://cnn.com" ,false // false means 'not asyncronous'
  http.send
  document.write(http.responsetext)
</script>
The url in http.open could be your PHP program, along with whatever vars you need to pass in. The response doesn't have to be XML, it can be any text you want to send. You could send a comma-separated list of values, then parse that into the variables to be set.

I don't think those wind up in the cache either, but I'm not sure about that.

If you need to keep talking to the server, you probably don't want to go with the .js route, since that will require a page refresh for each new call.

On the session thing, yeah, the sid refers to the PHP-assigned session id. Just tack it onto the .js on the way out the door and make sure it's still there when it comes back.