2013-07-19 80 views
0

用下面的代码面临的问题:PHP头随机ID变量

<?php 
     $con = mysql_connect("localhost","",""); 
     if (!$con){ 
     die('Could not connect: ' . mysql_error()); 
     } 
     mysql_select_db("abc", $con); 

     $ids = intval($_GET['id']); 
     if ($ids==0){ 
      $id = rand (0,10); 
     } 

     header("Location: http://index.php?id=$id"); 
?> 

浏览器显示出“此页面有太多的重定向”。任何人都请帮我解决这个问题。

+0

那是'index.php'中的代码吗? – Paulpro

+1

你的标题应该是:'header(“Location:/index.php?id=$id”);' –

+1

最后一行绝对缺少URI主机。它应该是http://yourhost.com/index.php?id=$id或者只是index.php?id = $ id,就像@Broken Heart说的那样。 –

回答

2

即使已将$_GET['id']设置为非零值,您也始终致电header()。因此你有一个无限的重定向循环。

如果您在if块内移动header()呼叫,无限循环将消失。

+0

非常感谢。完美的作品。 –

0
You can use If condition with the assigning the value in a variable instead of header 

for example: 

If($id="something") 
{ 
$error="Ohhh!!!, You are wrong"; 
$color="bg-color-red"; 
} 

and then call these variable when you are submitting the page.