0

使用 PHP,我有一个脚本,它从 mysql_db 搜索电子邮件,然后从与 db 中的电子邮件相同的行获取用户名。我想要它做的是使用用户名作为 url 中的子域重定向

我试过了:

 $username = //username taken from the db
 $url = 'http://".$username."domain.com';
 header('location: $url');

这只是将变量“$url”附加到我的测试 url 的末尾。

我试过了:

header('http://".$username."domain.com');

这在地址栏中输出:

http://%22.%24account_name.%22.domain.com

如何使用此搜索结果变量并重定向到我正在寻找的子域?

4

3 回答 3

2

你有大量的语法错误。

版本 1 更正(单引号中的变量被视为文字字符串):

$username = //username taken from the db
$url = 'http://".$username."domain.com';
header('location: ' . $url);

版本 2 更正(您的语法和引号用法都错误):

header("Location: http://".$username."domain.com");
于 2012-07-04T11:27:24.943 回答
1

只需使用双引号

$url = "http://{$username}.domain.com";
header("Location: {$url}");
于 2012-07-04T11:27:29.983 回答
0

这将起作用

 $username = //username taken from the db
 $url = "http://$username.domain.com";
 header("Location: $url");
于 2012-07-04T11:30:49.977 回答