luke777 Posted May 24, 2007 Share Posted May 24, 2007 Hi there, Im Stuck! I have a website monstergamez.net Which has 4,000 games on but unfortunatly im unable to edit individual game pages because its based on game.php. Basically i need some php code to put into game.php that will recognise the url and act on it. For example I need to display an advert on http://www.monstergamez.net/index.php?params=game/991/Nightmare-House/ So the php code would have to be somthing like this : If remote url = "http://www.monstergamez.net/index.php?params=game/991/Nightmare-House/" then DISPLAY AD CODE end if I hope this is clear enough for someone to give me an answer! Thanks Luke! Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/ Share on other sites More sharing options...
Dragen Posted May 24, 2007 Share Posted May 24, 2007 maybe you should have a table in your database for adverts with an id corresponding to a game, then on your game.php have a statement which checks if an advert exists for that game. if it does it outputs it, otherwise it doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-260760 Share on other sites More sharing options...
luke777 Posted May 24, 2007 Author Share Posted May 24, 2007 Thats a good idea man, thanks ill do that tonight when im back from work. Cheers Luke. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-260824 Share on other sites More sharing options...
Dragen Posted May 24, 2007 Share Posted May 24, 2007 cool. Let me know if it works for you Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-260826 Share on other sites More sharing options...
luke777 Posted May 24, 2007 Author Share Posted May 24, 2007 Ah im afriad it didnt work, Is there any other way i can do this ??? Luke. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-261156 Share on other sites More sharing options...
john010117 Posted May 24, 2007 Share Posted May 24, 2007 Can you show us the code that you've used to do what Dragen told you to do? Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-261167 Share on other sites More sharing options...
luke777 Posted May 25, 2007 Author Share Posted May 25, 2007 Well it did work, but it doesnt display HTML code up on the page.... i just moved the authorname code and changed it to <b>test</b> but it showed up exactly like that on the game page..... not like this : test Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-261434 Share on other sites More sharing options...
Dragen Posted May 25, 2007 Share Posted May 25, 2007 how are you displaying it? Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-261443 Share on other sites More sharing options...
luke777 Posted May 25, 2007 Author Share Posted May 25, 2007 echo $game['advert']; Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-261760 Share on other sites More sharing options...
luke777 Posted May 26, 2007 Author Share Posted May 26, 2007 I just need some code in php that i can put in the template file = game.php Its needs to be code doing this = IF url in browser = "http://www.monstergamez.net/game/9999398/Bat-In-Nightmare.html" then Display html code end if Luke Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-262174 Share on other sites More sharing options...
Dragen Posted May 26, 2007 Share Posted May 26, 2007 what about a switch? <?php switch($_SERVER['PHP_SELF']){ case 'Bat-In-Nightmare.html': //HTML CODE HERE break; case 'othergame.html': //HTML CODE HERE break; case 'anothergame.html': //HTML CODE HERE break; default: //HTML CODE HERE (probably leave blank to not show anything) break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-262200 Share on other sites More sharing options...
luke777 Posted May 26, 2007 Author Share Posted May 26, 2007 I placed this code in the game.php file <?php switch($_SERVER['PHP_SELF']){ case 'Bat-In-Nightmare.html': //HTML CODE HERE break; case 'game/9999398/Bat-In-Nightmare.html': //yo yo yo break; case 'anothergame.html': //HTML CODE HERE break; default: hello break; } ?> Nothing happens could someone provide an alternative code or explain this one better ? Thanks Luke. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-262202 Share on other sites More sharing options...
Dragen Posted May 26, 2007 Share Posted May 26, 2007 change the comments to some code.. such as: <?php switch($_SERVER['PHP_SELF']){ case 'Bat-In-Nightmare.html': echo 'this is bat in nightmare'; break; case 'game/9999398/Bat-In-Nightmare.html': echo 'so is this'; break; case 'anothergame.html': echo 'this is another game'; break; default: hello break; } ?> just place whatever code you want to happen in those parts. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-262219 Share on other sites More sharing options...
luke777 Posted May 28, 2007 Author Share Posted May 28, 2007 I have done this .... This is the current code in game.php (the template file for all game pages) <?php switch($_SERVER['PHP_SELF']){ case 'http://www.monstergamez.net/game/9999398/Bat-In-Nightmare.html': echo 'so is this'; break; case 'anothergame.html': echo 'this is another game'; break; default: break; } ?> case = "http://www.monstergamez.net/game/9999398/Bat-In-Nightmare.html" because i already tryed just case 'game/9999398/Bat-In-Nightmare.html': Nothing is showing up on http://www.monstergamez.net/game/9999398/Bat-In-Nightmare.html please could someone help me .... i know barely any php and if you read the posts above you will understand my problem :-\ Thanks Luke. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263267 Share on other sites More sharing options...
svivian Posted May 28, 2007 Share Posted May 28, 2007 $_SERVER['PHP_SELF'] should return the current URL, minus the domain name, and including a leading forward slash. For example: http://www.example.com/folder/something.html PHP_SELF will be: /folder/something.html So in your case I think you want to put case '/game/9999398/Bat-In-Nightmare.html': Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263272 Share on other sites More sharing options...
luke777 Posted May 28, 2007 Author Share Posted May 28, 2007 Yes i have tryed this but nothing shows on that page! Current code on game.php = <?php switch($_SERVER['PHP_SELF']){ case '/game/9999398/Bat-In-Nightmare.html': echo 'so is this'; break; case 'anothergame.html': echo 'this is another game'; break; default: break; } ?> but still nothing on http://www.monstergamez.net/game/9999398/Bat-In-Nightmare.html If i place code in between default: break; then it works.... but thats for every page. help! Luke. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263274 Share on other sites More sharing options...
redarrow Posted May 28, 2007 Share Posted May 28, 2007 try this way ok. <?php ob_start();?> <html> <title>My Games Page</title> <head></head> <body><center>My Games<hi></center> <br> <br> <center> <table border="4" bordercolor="black"> <tr> <td> <a href="<?php $_SERVER['PHP_SELF']?>?cmd=game1">Game 1 </a> </td> <td> <a href="<?php $_SERVER['PHP_SELF']?>?cmd=game2">Game 2 </a> </td> <td> <a href="<?php $_SERVER['PHP_SELF']?>?cmd=game3">Game 3 </a> </td> <td> <a href="<?php $_SERVER['PHP_SELF']?>?cmd=game4">Game 4 </a> </td> <td> <a href="<?php $_SERVER['PHP_SELF']?>?cmd=game5">Game 5 </a> </td> </tr> </table> </center> </body> </html> <?php if($_GET['cmd']=="game1"){ header("location: http://www.google.com"); } if($_GET['cmd']=="game2"){ header("location: http://www.yahoo.com"); } if($_GET['cmd']=="game3"){ header("location: http://www.msn.com"); } if($_GET['cmd']=="game4"){ header("location: http://www.ask.com"); } if($_GET['cmd']=="game5"){ header("location: http://www.yell.com"); } ?> <?php ob_flush();?> Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263281 Share on other sites More sharing options...
Dragen Posted May 28, 2007 Share Posted May 28, 2007 try just echoing $_SERVER['PHP_SELF'] to see what it is. echo $_SERVER['PHP_SELF']; Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263288 Share on other sites More sharing options...
luke777 Posted May 28, 2007 Author Share Posted May 28, 2007 echo $_SERVER['PHP_SELF']; Shows /index.php This may be relevant : .htacess contents RewriteEngine on RewriteCond %{HTTP_HOST} ^monstergamez.net [NC] RewriteRule ^(.*)$ http://www.monstergamez.net/$1 [L,R=301] RewriteEngine on RewriteRule ^(.*)\.html$ index.php?params=$1 [L] Redirect 301 http://monstergamez.net http://www.monstergamez.net Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263373 Share on other sites More sharing options...
Dragen Posted May 28, 2007 Share Posted May 28, 2007 /index.php ? Is that from the page which displays the games? If so what code do you have to display the right game? I don't think any of those htaccess variables would effect it.. apart from maybe the .php to .html one. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263393 Share on other sites More sharing options...
luke777 Posted May 28, 2007 Author Share Posted May 28, 2007 I dont know its based on an arcade script Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263508 Share on other sites More sharing options...
Dragen Posted May 29, 2007 Share Posted May 29, 2007 What is the page name that has all the code to display each game? wasn't it game.php? try the echo $_SERVER['PHP_SELF']; in game.php You'd most likely have some $_GET data in the url as well.. such as game.php?id=314, but only when a game is selected Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263800 Share on other sites More sharing options...
luke777 Posted May 29, 2007 Author Share Posted May 29, 2007 surely someone knows some code in php to do this simply... If url = http://www.somthing.com/wateva.html then display code end if I dont know php but i im sure someone can use if statments for this.... Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263834 Share on other sites More sharing options...
Dragen Posted May 29, 2007 Share Posted May 29, 2007 the switch I've given you is a neater way of using lots of if's, giving more flexibility to add more text/html to other pages if needed. Think of each case: as an if(){ and the break; as the closing bracket } The switch() part checks a certain variable and the cases are the value of that variable.. so instead of having: <?php if($_SERVER['PHP_SELF'] == '/game.php'){ echo 'some text'; }elseif($_SERVER['PHP_SELF'] == '/index.php'){ echo 'some other text'; } ?> you can use: switch($_SERVER['PHP_SELF']){ case '/game.php': echo 'some text'; break; case '/index.php': echo 'some other text'; break; default: break; } the default: part is what it should echo/do if switch($_SERVER['PHP_SELF']) doesn't equal anything in any of the cases. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263843 Share on other sites More sharing options...
chrisprse Posted May 29, 2007 Share Posted May 29, 2007 Hi Briefly reading this topic it seems you are putting the code given by other members into "game.php" when on the first post it suggests that you should be putting the code on "index.php". This is the real URL: http://www.monstergamez.net/index.php?params=game/991/Nightmare-House/ With use of mod-rewrite you are making the URL appear like: http://www.monstergamez.net/game/991/Nightmare-House.html The fact that game appears after the domain name when using mod-rewrite is where I think you are going wrong. You are editing "game.php" when infact you should be editing "index.php". A simple piece of code on "index.php" should work: <?php $URL = "http://www.monstergamez.net/".$params.""; $URL = rtrim($URL, "/") . ".html"; switch($URL) { case 'http://www.monstergamez.net/game/991/Nightmare-House.html': //If this URL is in the browser... echo '<b>Put your ad for the game "Nightmare House" here...</b>'; // ... Display this HTML. break; case 'http://www.monstergamez.net/game/9999398/Bat-In-Nightmare.html': //If this URL is in the browser... echo '<b>Put your ad for the game "Bat In Nightmare" here...</b>'; // ... Display this HTML. break; default: echo 'Error!'; } ?> Let me know how you get on.. Chris. Quote Link to comment https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/#findComment-263937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.