2012-07-29 68 views
0

我正在寻找一些在我的代码中导致这个PHP页面不重定向的错误。我正在查看是否有人可能知道这个问题的原因(这可能与Cookie有关)。PHP脚本不通过标头重定向

inc_vars.php:

<?php 
//some of the variables have been omitted. 
$pid = 'gbb'; 
$dbtable =''; 
$dbname = ''; 
$dbuser = ''; 
$dbpass = ''; 

$connect = mysql_connect('localhost', $dbuser, $dbpass); 

if(!$connect){ 
    header('Location: omitted'); 
    die(); 
} 

mysql_select_db ($dbname, $connect); 

$webroot = 'omitted'; 

$share_page = $webroot . '/share-the-training'; 

$gift = $webroot . '/free-video?setuser=1199'; 

$bonus_content = $webroot . '/awesome-bonus'; 

$share_php = $webroot . '/share.php'; 

?> 

refresh_id.php:

<?php 

include_once('inc_vars.php'); 

$results = mysql_query("SELECT id FROM " . $dbtable . " WHERE email='" . $_GET['email'] . "'"); 


if(!$results || mysql_num_rows($results)==0){ 
    header('Location: ' . $share_page . '?errorcode=1'); 
    die(); 
} 

$res_arr = mysql_fetch_assoc ($results); 

setcookie($pid . "_viral", (string)$res_arr['id'], time() + 3600 * 365); 

move_on(); 

function move_on(){ 
    header ('Location: ' . $share_php); 
    die(); 
} 

?> 

当人参观refresh_id.php电子邮件= their_email他们应该重定向到$ share_php页面。这不起作用。

但是,如果发生这种情况:refresh_id.php?email = an-email-that-is-in-in-database那么脚本重定向到$ share_page绝对没问题。

我已经尝试了这种方法,并且没有使用gbb_viral cookie。我不知道为什么这不起作用。所有网页现在都可以在网上直播,以防万一你想要寻找自己。

省略

存在于数据库如下电子邮件:[email protected](对于那些要测试的这款)

更新范围

愚蠢的错误。我只是在move_on()函数中添加了全局$ share_php,现在一切正常。感谢大家对SQL注入的关注,我现在正在切换到准备好的语句。

+1

注意SQL注入攻击 - 您正在将GET VAR直接转储到您的查询中。使用预准备的语句 – Utkanos 2012-07-29 18:41:19

回答

3

在你move_on函数,变量$share_php不会因为variable scope存在。因此,您的重定向如下所示:Location:。位置标题中没有URL。

您可以将该变量传递给该函数,或使用global关键字使其可用。试试这个:

move_on('/redirect_url'); 

function move_on($url){ 
    header ('Location: ' . $url); 
    die(); 
} 

事实上,在refresh_id.php我没有看到一个变量,名为$share_php任何地方,所以要重定向到一个空的URL。

+0

$ inc_vars中定义的$ share_php。PHP的,但使用$ share_php在功能上不等于 – voodoo417 2012-07-29 18:47:25

+0

这样做的窍门(男孩,我愚蠢)谢谢! – 2012-07-29 18:48:03

1

您还需要为浏览器设置状态标头以遵守位置标题。尝试添加

header('HTTP/1.1 303 See Other'); 

使用curl将帮助您调试。此外,您正在为您的SQL查询设置自己的SQL注入。

编辑:在阅读第二个答案后,您没有将位置传递给重定向功能是正确的。这应该也是固定的。

$results = mysql_query("SELECT id FROM " . $dbtable . " WHERE email='" . $_GET['email'] . "'"); 

永远不要相信用户这样的输入。相反,使用SQL绑定。这里是你将如何使用的mysqli图书馆做:http://php.net/manual/en/mysqli-stmt.bind-param.php