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!!
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]