Jump to content

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/52819-solved-display-html-code-if-url/
Share on other sites

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;
}
?>

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.

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.

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.

 

 

$_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':

 

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.

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();?>

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

 

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

 

 

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.