0

Haii

So I want to make a multiplayer game on php and I adopted the randomly generated link method.

For example like the skribbl.io game which will, upon pressing “create private room”, create a random link for your friends to join you:

skribbl.io/?random_string
like skribbl.io/?h4fwY8Ef

So for me, it looks like they have a php file with no name for which they provide the random string as a parameter. But I couldn’t make a no name php file.

So what I’ve opted instead is that everytime a user clicks “Create private match” for my game, a php script will copy an already existing php scripts containing the server side work but name it as random_string.php like y4aHr329sJ.php so the players would write in the url: mygame.com/y4aHr329sJ (without the .php because of an apache .htaccess rule)

But the drawback is that for every person that wants to “create private room”, i’d have more unnecessary files would be duplicated on the server.

Even thought I have a script that deletes all the expired random_string.php files after the game expired (at each start of a day the script is ran and deletes the 1 day old files)

I still think that’s kind of counter productive.

BTW I’m aware of doing something like mygame.com/join?random_string but I don’t particularly like it.

Also I’m not a fan of apache .htaccess turning mygame.com/random_string into mygame.com/join?random_string automatically because some players like to copy the link from the address bar and not from the “share this link” box.

Plus I would prefer it if there’s a solution without the question mark that skribbl.io uses in skribbl.io/*?*random_string.

Your help means alot. Thanks in advance!!

Jhon
  • 135
  • 8
  • 1
    had you considered using an database? – Elias Soares May 27 '20 at 19:53
  • 1
    You're using htaccess already to remove the .php. Why not use it to send all traffic to the single file as well? – rjdown May 27 '20 at 19:54
  • *"it looks like they have a php file with no name"* - Nope. They might not even be using PHP. They have some server-side code which responds to a root HTTP request on that server, and that code checks the query string for a value. *"more unnecessary files would be duplicated"* - Ya, there's no reason to do that. Generally one would have data in a database and users would be able to modify that data, and the code would use that data to dynamically generate the response. – David May 27 '20 at 19:55
  • I’ll check it out rjdown, I’m a nooby in .htacces that’s why I didn’t know it existed. – Jhon May 27 '20 at 19:56
  • Okay so David you’re talking about a server side code that does it. Is it what rjdown is suggesting with the .htaccess method?? – Jhon May 27 '20 at 19:58
  • So rjdown I didn’t really grasp the concept as to how apache could pass the random_string parameter to the php script with the redirect. Wouldn’t it be in the get parameters and available in the url? – Jhon May 27 '20 at 20:00
  • @Jhon: I'm talking about PHP code, which presumably you're already using? The question itself is pretty vague and generic, but it sounds like you're asking how to allow users to generate a kind of random key value and use it as a kind of private link. You can generate random string values, store them in your data along with the users who generated them, and fetch them from your data when a user requests a page and passes along that value on the query string. It sounds like you've made some incorrect assumptions about another system and need to take a step back and focus on your system. – David May 27 '20 at 20:00
  • @Jhon: You can use `.htaccess` to set a default PHP file to handle requests when no file is specified, and that PHP file can read the value from the query string. – David May 27 '20 at 20:01

1 Answers1

1

In reality, you have an index.php file in the root folder but apache could be configured to hide it from the URL.

You can do it by putting theses configuration (in an .htaccess at the root of the project or in the vhost config)

And you need to have mod_rewrite enabled.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

If you want to keep the index.html you may need to create a path like: /rooms/random-room-name and handle the room like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/rooms/(.*)$ /index.php?room=$1 [L]
jmaitrehenry
  • 2,190
  • 21
  • 31
  • Oh okay nice!! And could I even hide the question mark before the random_string ? – Jhon May 27 '20 at 20:01
  • Yes, this code remove it, if you want to keep it, just remove the slash before `$1` – jmaitrehenry May 27 '20 at 20:02
  • Thanks alot! But now I'm having problems with being able to get the index.html or some leaderboards.php that I have because all traffic is going to that single index.php file. – Jhon May 27 '20 at 20:09
  • So how to make exceptions? – Jhon May 27 '20 at 20:17
  • Normally, the RewriteConf check if the file doesn't exist before applying it. But, if you have an index.php and an index.html it could be tricky as normally you should only have one index file. – jmaitrehenry May 27 '20 at 20:40
  • I edit my answer to add a way to handle your room and keep the index.html handling `/` – jmaitrehenry May 27 '20 at 20:43