Store database links

Asked

Viewed 229 times

0

I work with url friendly.

All requests from my site, always goes through index.php

There, I wanted to take the full url that someone is ordering and write to a bank table.

As I have several different types of combinations (rules) in my htaccess, can come urls like this:

www.site.com.br/news/111
www.site.com.br/produto/111
www.site.com.br/como-funciona/
www.site.com.br/news/carros/ford/11

That is, there is an infinite and extensive list of rules.

Can anyone help me in how via PHP take these url, and go recording in a table?

  • Try this: $domain= $_SERVER['HTTP_HOST']; $url = "http://" . $domain. $_SERVER['REQUEST_URI']; echo $url;

  • Test and see if it fits. Put inside any page.

2 answers

1

You can use the example below:

function getUrlAtual() {
    $url = '';
    if (isset($_SERVER["HTTPS"])) {
        if ($_SERVER ["HTTPS"] == "on") {
            $url = 'https://';
        } else {
            $url = 'http://';
        }
    } else {
        $url = 'http://';
    }

    $url .= "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    return $url;
}

echo getUrlAtual();

If you don’t want to save protocol, just do so:

echo "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

1

Taking advantage of the code of @Allanandrade:

<?php
function getUrlAtual() {
    $url = '';
    if (isset($_SERVER["HTTPS"])) {
        if ($_SERVER ["HTTPS"] == "on") {
            $url = 'https://';
        } else {
            $url = 'http://';
        }
    } else {
        $url = 'http://';
    }

    $url .= $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
    return $url;
}

$requisitado = getUrlAtual();

$username = 'root';
$password = 'root';

$conn = new PDO('mysql:host=localhost;dbname=meuBancoDeDados', $username, $password);

$stmt = $conn->prepare('INSERT INTO tabela (url) VALUE ("'.$requisitado.'")');
$stmt->execute();
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.