【问题标题】:Posting parameters to url from another page从另一个页面将参数发布到 url
【发布时间】:2022-01-23 16:03:54
【问题描述】:

请帮助我弄清楚如何处理来自链接的重定向,如下所示: http://link.site.com/636337http://new.site.com/index.php?param=202020

注意域是根据ID从数据库中选择的(http://link.site.com/636337 636337是这里的ID),它指向域http://new.site.com/index.php

我要做的就是将参数添加到域中以成为http://new.site.com/index.php?param=202020

感谢您的帮助将受到高度重视和赞赏。

这是我的代码:

include "inc/config.php";

if(!isset($_GET["id"])) {
    addLog(1);
    http_response_code(404);
    exit("<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>");
}

$getid = $pdo->prepare("SELECT id FROM links WHERE uniq_id = :id");
$getid->bindParam("id", $_GET["id"]);
$getid->execute();
if($getid->rowCount() == 0) {
    addLog(1);
    http_response_code(404);
    exit("<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>");
}
$id = $getid->fetch()["id"];

// Only select 10 domains for performance
$getdomains = $pdo->prepare("SELECT * FROM domains WHERE link_id = :id AND status = 0 LIMIT 10");
$getdomains->bindParam("id", $id);
$getdomains->execute();
$domains = array();

if($getdomains->rowCount() == 0) {
    http_response_code(404);
    exit("<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>");
}

foreach ($getdomains->fetchAll() as $domain) {
    $domains[] = $domain["link"];
}


//Redirect to first url to the link with parameter value $paramid
if(empty($domains)) {
    http_response_code(404);
    exit("<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>");
}
$paramid = "20202020";
$urls = $domains[0];
//echo($urls);

header("Location: ".$url."?email=".$paramid); ```

The Problem is, it redirects only to the domain http://new.site.com/index.php but without the parameters 

【问题讨论】:

  • 它现在如何工作/不工作?您能否举例说明您收到的错误或当前的行为方式?
  • 您是在问如何构建链接,或使重写工作?
  • 它只重定向到域new.site.com/index.php,但没有参数
  • 我看到的唯一重定向是header("Location: ".$url."?email=".$paramid);,你期望?email=如何神奇地变成?param=,但目前最多只有你知道的秘密。

标签: php


【解决方案1】:

这很可能是一个错字。检查 PHP 代码的结尾:

$paramid = "20202020";
$urls = $domains[0];
//echo($urls);

header("Location: ".$url."?email=".$paramid);

您创建了变量 $urls,但您使用 $url 作为 Location 标头。 $url 没有在任何地方定义,所以我假设它是一个空变量。
请改写$url = $domains[0];

更多调试

请记住,Location 标头会立即起作用。所以echo这里应该跟exit来调试。

我建议您将位置值设为变量,以便您也可以轻松地回显它:

$url = $domains[0];
$location = $url . '?email=' . $paramid;
// echo $location; exit;

header("Location: " . $location);

帮助自己更轻松地调试总是值得的。

【讨论】:

  • 谢谢,这解决了我的问题。并给出了更清晰的认识。
  • 好的,很好?? 将此​​标记为解决方案,以将此问题标记为已关闭。
猜你喜欢
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2016-01-24
  • 2023-03-24
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多