Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 18, 2024, 02:34:59 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)JavaScript into PHP
Pages: [1]
Print
Author Topic: JavaScript into PHP  (Read 953 times)
everlive
Level 0
*


View Profile
« on: February 01, 2016, 10:17:24 AM »

So, I am aware that there is no way to protect your code, especially in the case of JavaScript that is required to be visible.

But I am curious if there is a way to use PHP to generate it onto a page and are their any benefits or disadvantages like loading time and what not?
Logged
oahda
Level 10
*****



View Profile
« Reply #1 on: February 01, 2016, 10:26:26 AM »

I think it would show up if you clicked to view the source even if generated, just like you can inspect generated HTML, so I don't really see the point of doing that instead of just writing the JS directly. Seems harder to work with for no particular reason.
Logged

everlive
Level 0
*


View Profile
« Reply #2 on: February 01, 2016, 10:57:38 AM »

Mostly to explore the idea of manipulating JavaScript through PHP and used a PHP database to do it.  Apart of JSON of course.
Logged
oahda
Level 10
*****



View Profile
« Reply #3 on: February 01, 2016, 12:20:57 PM »

Oh. Right. Yeah, was gonna suggest JSON before I read the end of your message... Tongue

Well, it's possible, of course. But I can't think of any use case for it. Huh?
Logged

Cheezmeister
Level 3
***



View Profile
« Reply #4 on: February 03, 2016, 09:43:42 PM »

This again? "Protect" your code from what?

If it's code generation you want, sure, you can do that through PHP, but like Prinsessa I don't really see what it actually buys you. (Databases are for data, not code. It's in the name Wink )
Logged

෴Me෴ @chzmstr | www.luchenlabs.com ቒMadeቓ RA | Nextris | Chromathud   ᙍMakingᙌCheezus II (Devlog)
wccrawford
Level 3
***



View Profile
« Reply #5 on: February 04, 2016, 07:13:41 AM »

No, there would be no benefit to loading time, and it would probably only be worse than normal since you've added PHP into the mix.  (Any other language would have the same problem.)

The only way to protect your code is to not send it to the user.  As such, you could have most of your code server-side (PHP or NodeJS or others) and then the user only has access to the code that shows the game state and lets them interact with it.  The real logic would all be on the server.

Of course, you pay a performance penalty there, and you have to run all the code on the server, so your server costs go up, too.
Logged
everlive
Level 0
*


View Profile
« Reply #6 on: February 04, 2016, 03:16:52 PM »

No, there would be no benefit to loading time, and it would probably only be worse than normal since you've added PHP into the mix.  (Any other language would have the same problem.)

The only way to protect your code is to not send it to the user.  As such, you could have most of your code server-side (PHP or NodeJS or others) and then the user only has access to the code that shows the game state and lets them interact with it.  The real logic would all be on the server.

Of course, you pay a performance penalty there, and you have to run all the code on the server, so your server costs go up, too.

Yes, as I said in my first post I know you can't hide anything once it is in the browser.

The main point of this thread to get some answers about JavaScript/PHP interaction.  Most importantly, how to attach a JavaScript game to a PHP database, then assign each player an area of the game so that no player can control another.  Then feed the results of the match back to the PHP database to update the player's statistics.

I would assume object instances in the game state via JavaScript would do the trick based on what the MySQL database gives it.

It's partly my lack of knowledge in the area at fault.
Logged
oahda
Level 10
*****



View Profile
« Reply #7 on: February 04, 2016, 05:06:20 PM »

To talk with a database I'd use AJAX to talk to a small PHP page that outputs JSON, sort of like you said, but you never mentioned AJAX.
Logged

bun
TIGBaby
*



View Profile
« Reply #8 on: March 18, 2017, 09:15:40 PM »

The main point of this thread to get some answers about JavaScript/PHP interaction.  Most importantly, how to attach a JavaScript game to a PHP database, then assign each player an area of the game so that no player can control another.  Then feed the results of the match back to the PHP database to update the player's statistics.

I would assume object instances in the game state via JavaScript would do the trick based on what the MySQL database gives it.

There are really two separate questions here:

  • state initialization
  • ongoing communication

For the second point, Javascript & PHP themselves are irrelevant: like in any other networked game, you need to communicate with the server over some protocol. JSON messages over HTTPS connections or websockets is the popular and obvious option.

The first point is more interesting. As you suggest, you can use PHP to generate some or all of the Javascript that is being served on the page.

However, this has some consequences:

  • security: if you're directly generating Javascript code in PHP, you could end up opening yourself up to script injection and other nasties caused by malicious user input, and
  • poor separation of code: your client-side code becomes tightly coupled to the backend, which makes it harder to test, move, refactor, etc.

The more modern option - which has become increasingly popular with the rise of bundling tools like Webpack - is to serve your Javascript code as a library on your own page that has an explicitly constructor / initialization pattern / entry point. You still might need to get some initial user state onto the page to pass into your JS code - such as a user ID or an auth token or whatever else - but that can then just be a plain JSON object, which you can sfely serialize onto the page from a native object in your backend code.

It's a little bit more work initially to get it set up, but it's well worth it for the strong logical separation you get out of it, and makes it easier to add nice stuff like offline storage on the browser side, to swap out the backend, etc.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic