2013-02-26 179 views
1

我将表单数据从一个页面传递给另一个页面。

下面是表单动作的代码,verify.php:

<?php 

header("Location: http://domain.com/rd/r.php?&email=<?php echo $_GET['email']; ?>"); 

?> 

不过,我不断收到以下错误:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/thanksfo/public_html/LP/x/verify.php on line 3 

不知道如何解决它?它说意想不到的空白或t_encapsed。真的不知道什么是错的...

+0

确保'$ _GET ['email']'实际上是定义的;和'?&email = ...'可以简单地'?email = ...'。 – 2013-02-26 02:46:10

回答

6

双引号中的字符串正确的语法是:

header("Location: http://domain.com/rd/r.php?&email=$_GET[email]"); 

你不能有一个字符串中<?php代码,特别是收盘?>打破了语法为你。

  • 数组语法sans引号仅在此上下文中有效,而不是在普通的PHP代码中。

  • 而作为@Aarolama Bluenk指出,urlencode()建议:

    $formatted = urlencode($_GET['email']);
    header("Location: http://domain.com/rd/r.php?&email=$formatted");

这也是明智的sanitize电子邮件地址,如果没有这样做。

+3

我可能'urlencode'该电子邮件。 – Kermit 2013-02-26 02:42:48

+1

'$ _GET [email]'会发出通知,不像在双引号字符串中使用。 – 2013-02-26 02:50:52

1

或尝试,我觉得也行

<?php 

header("Location: http://domain.com/rd/r.php?&email=".$_GET['email']); 

?> 
2

正如指出的其他人,利用内幕字符串<?php ?>语法将无法正常工作。最重要的是,你也应该确保产生的URL是有效的:

header("Location: http://domain.com/rd/r.php?" . http_build_query(array(
    'email' => $_GET['email'], 
))); 

我也换成了URL的一部分"?&"用简单"?"

参见:http_build_query()

或者,您可以filter电子邮件地址:

$email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL); 
使用这种

$email可以是:

  • null - 值不通过
  • false - 它不是一个有效的电子邮件
  • 的电子邮件地址作为字符串