Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 捕获url内容并重定向页面_Php_Html_Url_Redirect - Fatal编程技术网

Php 捕获url内容并重定向页面

Php 捕获url内容并重定向页面,php,html,url,redirect,Php,Html,Url,Redirect,我有一个像这样的url设置 mysite.com/QR?id=12345 其中id将是一些num。我希望能够浏览到此url,获取id值并将页面重定向到其他url 我可以用PHP做这个吗?该ID还将与我的数据库中的ID相对应 编辑:修改我原来的问题。 我拥有的是一个指向一个不存在的页面的url,其中ID可以是任何数字。我需要找到一种浏览的方法,以获取该url并提取id值,然后重定向到一个新页面,在该页面中,我将根据该id显示内容,该id在我的数据库中具有相应的值 if(isset($_GET['

我有一个像这样的url设置

mysite.com/QR?id=12345
其中id将是一些num。我希望能够浏览到此url,获取id值并将页面重定向到其他url

我可以用PHP做这个吗?该ID还将与我的数据库中的ID相对应

编辑:修改我原来的问题。 我拥有的是一个指向一个不存在的页面的url,其中ID可以是任何数字。我需要找到一种浏览的方法,以获取该url并提取id值,然后重定向到一个新页面,在该页面中,我将根据该id显示内容,该id在我的数据库中具有相应的值

if(isset($_GET['id']) == '12345'){
    header('Location: example.com');
}
我猜是这样的

// First check if it exists
if (isset($_GET['id']))
{
    $id = someSecurityFunction($_GET['id']);        

    // Check what the value is of ID
    switch ($id)
    {
        case '12345':
            header("Location: website.com?...");
            exit();
        case '67890':
            header("Location: website.com?...");
            exit();
        default:
            echo "No corresponding ID value found...";
            break;
    }

    // Or just redirect to another page and handle there your ID existence thing
    // by omitting the switch-case and redirect at once
    // header("Location: website.com?id=$id");
}
else
{
    echo "No ID found in querystring...";
}
我猜是这样的

// First check if it exists
if (isset($_GET['id']))
{
    $id = someSecurityFunction($_GET['id']);        

    // Check what the value is of ID
    switch ($id)
    {
        case '12345':
            header("Location: website.com?...");
            exit();
        case '67890':
            header("Location: website.com?...");
            exit();
        default:
            echo "No corresponding ID value found...";
            break;
    }

    // Or just redirect to another page and handle there your ID existence thing
    // by omitting the switch-case and redirect at once
    // header("Location: website.com?id=$id");
}
else
{
    echo "No ID found in querystring...";
}
这能回答你的问题吗


这是否可以回答您的问题?

基本上,您需要一个PHP文件来接收
mysite.com/QR
上的数据-这可以通过创建名为
index.PHP
的PHP文件并将其放入
/QR
目录或使用Apache ModRewrite(假设您正在运行Apache服务器)来完成

使用
/QR
目录方法的优点是很容易-缺点是它区分大小写,
mysite.com/QR?id=12345
将导致404错误

要使用Apache ModRewrite,您需要在web树的根目录中创建或编辑一个
.htaccess
文件(或者在httpd-vhosts.conf中,是的,这需要更多的权限,维护它需要重新启动服务器)在该
/.htaccess
文件中,您需要指向将处理二维码重定向的PHP文件-
QR.PHP
,例如:

RewriteEngine On
RewriteRule ^QR/?$ /qr.php [NC,QSA,L]
  • NC=无大小写(使其不区分大小写,因此
    /QR
    /QR
    将起作用
  • QSA=查询字符串追加,以便将
    id=12345
    传递给
    /qr.php
  • L=Last,如果有效,则在此之后不会处理重定向
您的
/qr.php
文件需要执行以下操作:

if(empty($_GET['id'])) {
  // deal with exceptions where the 'id' isn't set
}
else {
  $iId = (int) $_GET['id']; // may as well cast it to an INT if it's matching an auto-incremented database id

  //possibly connect to the database, validate the id, update some records and retrieve the redirection URL
  // ... then redirect
  header('HTTP/1.1 303 See Other');
  header('Status: 303 See Other'); # for Chrome/FastCGI implementation
  header('Location: ' . $sSomeRedirectionURLFromTheDatabase);
  die(); # I generally always die after redirecting
  }
}
编辑

实际上,
/qr.php
作为显示内容的页面可能更好,只要您不更新数据库中的任何内容(否则,如果您将页面重新加载到数据库中,则页面重新加载将计入命中率,诸如此类)-使用ApacheModrewrite重定向到它(如前所述),然后在
/qr.php

if(empty($_GET['id'])) {
  // deal with exceptions where the 'id' isn't set
}
else {
  $iId = (int) $_GET['id'];
  // connect to the database, validate the id and retrieve the relevant
  // content that you want to display (based on the id) and then output it.
  }
}

基本上,您需要一个PHP文件来接收
mysite.com/QR
上的数据-这可以通过创建一个名为
index.PHP
的PHP文件并将其放入
/QR
目录或使用ApacheModrewrite(假设您正在运行Apache服务器)来完成

使用
/QR
目录方法的优点是很容易-缺点是它区分大小写,
mysite.com/QR?id=12345
将导致404错误

要使用Apache ModRewrite,您需要在web树的根目录中创建或编辑一个
.htaccess
文件(或者在httpd-vhosts.conf中,是的,这需要更多的权限,维护它需要重新启动服务器)在该
/.htaccess
文件中,您需要指向将处理二维码重定向的PHP文件-
QR.PHP
,例如:

RewriteEngine On
RewriteRule ^QR/?$ /qr.php [NC,QSA,L]
  • NC=无大小写(使其不区分大小写,因此
    /QR
    /QR
    将起作用
  • QSA=查询字符串追加,以便将
    id=12345
    传递给
    /qr.php
  • L=Last,如果有效,则在此之后不会处理重定向
您的
/qr.php
文件需要执行以下操作:

if(empty($_GET['id'])) {
  // deal with exceptions where the 'id' isn't set
}
else {
  $iId = (int) $_GET['id']; // may as well cast it to an INT if it's matching an auto-incremented database id

  //possibly connect to the database, validate the id, update some records and retrieve the redirection URL
  // ... then redirect
  header('HTTP/1.1 303 See Other');
  header('Status: 303 See Other'); # for Chrome/FastCGI implementation
  header('Location: ' . $sSomeRedirectionURLFromTheDatabase);
  die(); # I generally always die after redirecting
  }
}
编辑

实际上,
/qr.php
作为显示内容的页面可能更好,只要您不更新数据库中的任何内容(否则,如果您将页面重新加载到数据库中,则页面重新加载将计入命中率,诸如此类)-使用ApacheModrewrite重定向到它(如前所述),然后在
/qr.php

if(empty($_GET['id'])) {
  // deal with exceptions where the 'id' isn't set
}
else {
  $iId = (int) $_GET['id'];
  // connect to the database, validate the id and retrieve the relevant
  // content that you want to display (based on the id) and then output it.
  }
}

简短的回答是“是”…id值将存在于PHP的$\u GET superglobal:
$\u GET['id']
-一旦获得了它,您可以对它执行任何您喜欢的操作,在数据库中匹配,使用
标题('Location:…')重定向到另一个页面
type call,随便什么。我能为所有url id=XXXXX使用一个php文件吗?就像url不存在是我的问题。我只想提取id并重定向。你需要一个php文件“成为”那个“QR”,两种简单的方法是调用文件
index.php
,并将其放入名为
QR
的文件夹中,但这使得它在*nix服务器上区分大小写,并且
/QR?id=12345
不起作用,或者您可以使用Apache Mod Rewrite重定向到该php文件……简短的回答是“是”…id值将存在于PHP的$\u GET superglobal:
$\u GET['id']
-一旦获得了id值,您可以对其执行任何操作,在数据库中进行匹配,使用
标题('Location:…')重定向到另一个页面
type call,随便什么。我能为所有url id=XXXXX使用一个php文件吗?就像url不存在是我的问题。我只想提取id并重定向。你需要一个php文件“成为”那个“QR”,两种简单的方法是调用文件
index.php
,并将其放入名为
QR
的文件夹中,但这使得它在*nix服务器上区分大小写,并且
/QR?id=12345
不起作用,或者您可以使用Apache Mod Rewrite重定向到该php文件…我认为这很接近,我不一定需要知道id的值。一旦我有了ID,我将重定向到一个页面,该页面根据我在DB中引用的ID显示内容。@MickB那么你想重定向到一个有ID的页面吗?检查我的答案,我已经编辑了它。我认为这很接近,我不必知道ID的值。一旦我有了ID,我将重定向T