如何使用 PHP 将数据库中的 URL 代码附加到域名并重定向?

发布于 2024-08-04 10:04:24 字数 240 浏览 5 评论 0原文

我使用了一半的 URL 缩短系统。我从用户那里获取 URL,然后在 MySQL 中为此创建代码。然后我必须将即将到来的代码附加到我的域名(现在我正在本地主机上工作),例如 http://localhost/a5c3 然后将其重定向到真实域。

我被困在这里了代码片段至少对我有好处,让我理解我要做什么,或者你可以解释我要做什么。

I am on half of URL shortening system. I get the URL from the user and then create code in MySQL for that. Then I have to append coming code to my domain name (now I am working on localhost) like http://localhost/a5c3 then redirect it to real domain.

I stuck in here. A code snippet would be good for me at least to understand what I am going to do or you can explain what I am going to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ花落谁相伴 2024-08-11 10:04:24

如果您没有将短代码与 URL 相关联,那么您需要这样做,并且重定向会很容易。

算法:

将表单中的 URL 和生成的代码保存到数据库中以供以后使用。

$url = $_POST['url']
Generate code
Concatenate your URL with the code
$full_url = $url.$code

向用户显示缩短的 URL。

如果您想重定向用户,在他/她将 URL 放入浏览器地址后,请执行以下操作:

创建一个 .htaccess 文件,向其中添加以下行并将其拖放到您的根文件夹中:

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?code=$1 [L]

.htaccess 文件会将所有内容重定向到你的index.php。例如,如果用户输入 http://example.com/ujijui 那么 .htaccess 将调用 http://example.com/index.php?code=ujijui。因此,您可以使用 $_GET 捕获 URL 中的查询字符串。

在你的index.php文件中:

$code = $_GET['code']
mysql_connect('localhost', 'user', 'password')
mysql_select_db('your_db')
$sql = "SELECT url from Table where code=$code"
$result = mysql_query($sql)
Loop through result and get the URL

header("Location: $url")

明白了,这只是一个算法。

If you are not associating short code with a URL then you need to do that, and redirection will be easy.

Algorithm:

Save the URL from the form and generated code to the database for later use.

$url = $_POST['url']
Generate code
Concatenate your URL with the code
$full_url = $url.$code

Show the shortened URL to the user.

If you want redirect the user, after he/she puts the URL in the browser address then do this:

Create a .htaccess file, add the following lines to it and drop it to your root folder:

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?code=$1 [L]

The .htaccess file will redirect everything to your index.php. For example, if the user types http://example.com/ujijui then .htaccess will call http://example.com/index.php?code=ujijui. So you can capture the query string in the URL by using $_GET.

In your index.php file:

$code = $_GET['code']
mysql_connect('localhost', 'user', 'password')
mysql_select_db('your_db')
$sql = "SELECT url from Table where code=$code"
$result = mysql_query($sql)
Loop through result and get the URL

header("Location: $url")

Get it, this is just an algorithm.

我们只是彼此的过ke 2024-08-11 10:04:24

您需要让服务器将不存在的 URL 重定向到现有页面(例如,使用 mod_rewrite 在 Apache 上)。这个“包罗万象”的页面将读取 URL,检查数据库中是否存在给定的代码,如果存在,则重定向到正确的 URL。 Ainab 的伪代码解释了最后一部分。

如果您没有关联短代码
有了 URL 那么你需要这样做,
并且重定向会很容易。

从表中选择 url,其中 code=code
header("位置:$url")

You need to have your server redirect non-existent URLs to an existing page (for example, using mod_rewrite on Apache). This 'catch-all' page will read the URL, check if the code given exists in the database, and if so, redirect to the proper URL. Ainab's pseudocode explains the last part.

if you are not associating short code
with a URL then you need to do that,
and redirection will be easy.

SELECT url from Table where code=code
header("Location: $url")

浊酒尽余欢 2024-08-11 10:04:24

对于重定向问题,您应该尝试这样的操作:

.htaccess 文件:

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?url=$1 [L]

在index.php 文件中:

<?php
    $url = $_GET['url'];

    // These will be taken from database
    $urls_associations = array(
      '1234' => "http://www.example.com",
      '5678' => "http://www.google.com",
      '90AB' => "http://stackoverflow.com",
    );

    // Redirect
    if (isset($urls_associations[$url])) {
      $redirect = $urls_associations[$url];
      header("Location: $redirect");
      echo "<a href='$redirect'>Go To : $redirect</a>";
    }
    else {
      echo "Unknown URL code.";
    }

然后,当用户转到 http://localhost/1234,他/她被重定向到 http://example。当然,您应该在数据库上运行查询而不是从数组中读取,但它看起来很简单,只需使用类似以下内容:

$code = mysql_escape_string($url);
$res = mysql_query("SELECT url FROM mytable WHERE code='$code'");
if ($redirect = mysql_result($res)) {
  header("Location: $redirect");
  echo "<a href='$redirect'>Go To : $redirect</a>";
}
else {
  echo "Unknown URL code.";
}

For the redirection problem, you should try something like this:

The .htaccess file:

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?url=$1 [L]

And in the index.php file:

<?php
    $url = $_GET['url'];

    // These will be taken from database
    $urls_associations = array(
      '1234' => "http://www.example.com",
      '5678' => "http://www.google.com",
      '90AB' => "http://stackoverflow.com",
    );

    // Redirect
    if (isset($urls_associations[$url])) {
      $redirect = $urls_associations[$url];
      header("Location: $redirect");
      echo "<a href='$redirect'>Go To : $redirect</a>";
    }
    else {
      echo "Unknown URL code.";
    }

Then, when the user goes to, for example, http://localhost/1234, he/she gets redirected to http://example.com, etc. Of course, you should run a query on the database instead of reading from an array, but it looks quite easy, just use something like:

$code = mysql_escape_string($url);
$res = mysql_query("SELECT url FROM mytable WHERE code='$code'");
if ($redirect = mysql_result($res)) {
  header("Location: $redirect");
  echo "<a href='$redirect'>Go To : $redirect</a>";
}
else {
  echo "Unknown URL code.";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文