首页 > 解决方案 > 重定向到电子邮件用户站点

问题描述

我尝试执行此代码,但我做错了,因为它总是将我重定向到谷歌:

    <?php

$email = $_POST['email'];
$after_email = explode('@', $email);
$part = $part[1];
if($part == 'aol.com') {
        header("Location: http://aol.com");
} else if($part == 'yahoo.com') {
        header("Location: http://yahoo.com");
} else {
        header("Location: http://google.com");
}
?>

这是我的 html 表单

<form action="snd.php">
email: <input type="text" id="email" name="email"><br>
<input type="submit" value="Submit">
</form> 

标签: phpemailredirect

解决方案


您在 上调用了该explode()函数$email。这意味着$after_email现在是一个数组。您的错误是您访问了错误的数组变量。

更改此行:

$part = $part[1];

至:

$part = $after_email[1];

编辑

也更改表单中的这一行:

<form action="snd.php">

至:

<form action="snd.php" method="post">

您也没有指定表单方法。默认情况下,此设置为$_GET。但是在您的 php 脚本中,您调用的是$_POST超全局变量。将方法更改为post,您就可以开始了。


推荐阅读