PHP - 从当前页面获取节点值后重定向到另一个页面
PHP - Redirect to another page after getting node values from current page
我想在从当前页面获取节点值并将它们传递到 URL 的参数后将用户重定向到另一个页面,这是我的代码:
<?php
$dom = new DOMDocument;
$html = file_get_contents(home_url(). $_SERVER['REQUEST_URI']);
$dom->loadHTML($html);
$addresses = $dom->getElementsByTagName('address');
foreach ($addresses as $key => $address) {
$add[$key] = $address->nodeValue;
}
$fields['address'] = urlencode($add[0]);
$qry = http_build_query($fields);
if(!empty($qry)){
header( 'Location: http://example.com/subscription-form/?' . $qry) ;
}
?>
确实重定向成功,但是 http_build_query
值为空。这是重定向的示例: http://example.com/subscription-form/?address=
, URL 没有 $_GET['address']
的值。
PHP 重定向没有用,所以我用 JavaScript 试了一下,结果成功了:
$redir = '<script type="text/javascript">';
$redir.= 'window.location.href="http://example.com/subscription-form/?';
$redir.= $qry .'"';
$redir.= '</script>';
echo $redir;
是的,没有必要 urlencode
因为它已经被 http_build_query
完成了。
我想在从当前页面获取节点值并将它们传递到 URL 的参数后将用户重定向到另一个页面,这是我的代码:
<?php
$dom = new DOMDocument;
$html = file_get_contents(home_url(). $_SERVER['REQUEST_URI']);
$dom->loadHTML($html);
$addresses = $dom->getElementsByTagName('address');
foreach ($addresses as $key => $address) {
$add[$key] = $address->nodeValue;
}
$fields['address'] = urlencode($add[0]);
$qry = http_build_query($fields);
if(!empty($qry)){
header( 'Location: http://example.com/subscription-form/?' . $qry) ;
}
?>
确实重定向成功,但是 http_build_query
值为空。这是重定向的示例: http://example.com/subscription-form/?address=
, URL 没有 $_GET['address']
的值。
PHP 重定向没有用,所以我用 JavaScript 试了一下,结果成功了:
$redir = '<script type="text/javascript">';
$redir.= 'window.location.href="http://example.com/subscription-form/?';
$redir.= $qry .'"';
$redir.= '</script>';
echo $redir;
是的,没有必要 urlencode
因为它已经被 http_build_query
完成了。