Store database links

0

I work with friendly url.

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

There, I wanted to get the full url that someone is requesting and writing to a bank table.

As I have several different types of combinations (rules) in my htaccess , you can see 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 with how to use PHP to get these urls, and to be writing to a table?

    
asked by anonymous 06.09.2016 / 20:43

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 do not want to save the protocol, just do the following:

echo "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    
06.09.2016 / 20:48
1

Taking advantage of the @AllanAndrade code:

<?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();
?>
    
06.09.2016 / 20:54