homepage Welcome to WebmasterWorld Guest from 54.196.201.253
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

    
Flash game website structure
flurpalgames




msg:3969361
 7:21 pm on Aug 10, 2009 (gmt 0)

Hiya there!

So I already have started this flash game website and I am using a form of management where:
I have a mysql database that contains all info about the games(source location, title, description, etc. as well as an identification number). I have a single page in PHP that takes the number from the url (gamepage.php?num=1), finds the game with that number, finds the information about that game like the title and such, and displays it in that page. That's how my site works basically.
I was wondering how other flash game websites are structured, I mean, how games are added, and such. I see that with most of them, they have a game page with a URL such as http://www.example.com/game/gamename , as if the game has its own directory. How does this all work? How are the games added easily? Thanks.

 

rocknbil




msg:3969833
 3:09 pm on Aug 11, 2009 (gmt 0)

Welcome aboard flurpalgames,

http://www.example.com/game/gamename

Most likely, this is done using an .htaccess directive with mod_rewrite, like what's done for SEO-friendly URL's. You leave your actual game url as gamepage.php?num=1, and allow your script to process this query string as it does now. You would create an .htaccess file for that directory and create a mod_rewrite directive to rewrite from the "friendly" URL to your script. One way would be

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /gamepage.php

What this does is if it's not a real file (-f) and not a real directory (-d) rewrite to gamepage.php. In gamepage.php you would split up the request_uri on the slashes / and look up the last value (gamepage) in a database.

select game_id from table where gametitle='gamepage';

If found, set the get value,

$_GET['num'] = (game id value found)

And now you have num=1, your script continues on as normal.

The above method is a bit of overkill, as it requires searching the entire system to check whether the request is a file or a directory, which is apparently a bit intensive. Another way to do it is to create a specific rewrite rule for each game, like

RewriteEngine On
RewriteRule ^Game-One$ /gamepage.php?num=1 [L]
RewriteRule ^Game-Two$ /gamepage.php?num=2 [L]
RewriteRule ^Game-Three$ /gamepage.php?num=3

(note the last, [L] is only needed for all but the last, it means if found, this is the last rule to process.)

The first method may be a more reasonable approach in terms of maintenance if there are thousands of games, the second is more efficient if the number is only a hundred or so.

There are many other ways to approach it, using other environment variables, and mod_rewrite experts here will have better, more efficient ideas; explore the mod_rewrite documentation for the solution that works best for you.

mod_rewrite [httpd.apache.org]

CyBerAliEn




msg:3969801
 2:39 pm on Aug 11, 2009 (gmt 0)

Your approach is fine!

Though the other sites appear to be using subdirectories to hold and organize all of the games... I bet you (99.9%) those other sites are using what is referred to as 'URL rewriting'. Search for it to learn more.

What it is, is a part of the Apache web server where you can specify different rules and options for URLs requests and what file is returned to the user.

For example... what I do is setup Apache as (create a '.htaccess' file in your web root; note that you may not be able to do this if your web server is not configured to allow it):

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule *. getgame.php [L]

What does this code do? Is basically says, if the requested file cannot be found (it does not exist), the server will automatically go to '/getgame.php' on its backend. So, say a user types in '/games/8/blaster/' and this directory/file does not exist... this URL will remain in the browser, but the server will fetch '/getgame.php' instead.

How does that help you? Setup '/getgame.php' to process the request! You do this by something like the following...


<?php
/*Say this is your '/getgame.php' script. Say you actually access games by '/gamepage.php?num=1', but you setup your URLs as '/games/1/blaster/' and setup Apache to rewrite.*/
$uri = $_SERVER['REQUEST_URI'];
$parts = explode('/',$uri);
$_REQUEST['num'] = $parts[2];
include("{$_SERVER['DOCUMENT_ROOT']}/gamepage.php");
exit();
?>

Now... when someone enters a game URL, such as '/games/453/space-invaders/'. If that file doesn't actually exist, your server fetches '/getgame.php' internally. This processing script then sets up your '/gamepage.php' for display.

Remember to have your '/gamepage.php' setup to validate the requested game (ie, if the game does not exist, display a simple game does not exist error; you don't want the user to see a bunch of garbled PHP error messages or a blank page). Likewise, I would validate the user input! Since you know the user should ONLY be using numeric characters to identify the game (which I assume is its database ID?)... you could change the above code line to:
$_REQUEST['num'] = preg_replace("/[^0-9]/",'',$parts[2]);
This will strip out ANY non "zero" through "nine" characters from the user input.

Now, when you create URLs for your games, use the format:
'/games/[DATABASE ID NUMBER]/[GAME TITLE]/'

(but remember to strip the game title of unfriendly characters and turn spaces into dashes! this would be SEO friendly)

As you move forward with your site... you may eventually want to change the rewrite rule to point to something like '/processurl.php'. And have this page use similar code as provided above. But notice the URL as '/games/32/whatever/'? You could also have URLs like '/blog/232/cool-post/' or '/news/2111/game-released/'. By using the '$parts[1]' variable, you could grab "games" or "blog" and process the request to different files, ie:

/*similar code as above block with following modifications/additions*/
if ($parts[1]==="blog") { $page = "blogpage.php"; }
if ($parts[1]==="games") { $page = "gamepage.php"; }
//...etc...
if ($page=="") { $page = "notfound.php"; }
include("{$_SERVER['DOCUMENT_ROOT']}/{$page}");
exit();
/*etc*/

Best of luck! Hope it all made sense!

flurpalgames




msg:3970019
 6:54 pm on Aug 11, 2009 (gmt 0)

Wow, this is great! Thanks!

I do have a few questions:
1) I already made my site so that a bunch of different dynamic pages link to a game. For example, on puzzle.php, it lists all games that have "puzzle" in the category field in the database. It then gather title, play count, and the ID number....so that it can put that number into the link to go to that page, e.g., href="gamepage.php?num=$gameid" Do I need to change these links to games all around, or can I keep it like this and then when someone goes to gamepage.php?num=1 it will mask it as...say....example.com/bloons. or example.com/1/bloons . I was just wondering how much I would have to change in the code of my web pages.

2) Where is the .htaccess file to be edited? Or where must I create it?

Thanks a lot!

[edited by: encyclo at 11:32 am (utc) on Aug. 12, 2009]
[edit reason] please only use example.com [/edit]

phranque




msg:3970435
 11:35 am on Aug 12, 2009 (gmt 0)

1) if you want http://www.example.com/game/gamename to be your canonical url you should use that url in all your on-page/internal linking on your site.
the http://www.example.com/gamepage.php?num=n is only used internally.
the usage of mod_rewrite described above is an "internal rewrite" and that internal url is not exposed publicly.
if you get an external request for http://www.example.com/gamepage.php?num=n you should use an "external redirect" to your canonical url which is http://www.example.com/game/gamename and the subsequent request will be internally rewritten to http://www.example.com/gamepage.php?num=n.
in this way, only your canonical urls will be indexed or shown in the address bar of your browser.
2) for this purpose, the .htaccess file should reside in your document root directory.

flurpalgames




msg:3970735
 6:22 pm on Aug 12, 2009 (gmt 0)

Ok. I made the .htaccess file, inputted the code that Cyberalien gave, and saved it.
Then I made the file called getgame.php, and c&p'd the next snippet of code that Cyberalien gave me.
I then went to example.com/gamepage.php?num=1 and it gave me a 500 internal server error.

So, saying that the variable for the title of the game on gamepage.php is $n_output, how do I make it so that when you go to example.com/gamepage.php?num=1, it changes the url in the address bar to example.com/1/tigerattack (that is, the $n_output on gamepage.php is Tiger Attack but the code deletes spaces and makes all letters lowercase)?

phranque




msg:3972795
 11:33 am on Aug 16, 2009 (gmt 0)

it gave me a 500 internal server error

first you should check your server error log to find out what caused the 500 response.

StoutFiles




msg:3972870
 3:33 pm on Aug 16, 2009 (gmt 0)

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^games/([^/]+) /games.php?game=$1 [NC]
RewriteRule ^([^.]*)/$ $1.php [l]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / New To Web Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved