How do I get the user's name in the URL?

2

I'm creating a site and would like to make a system of profiles of type: meusite.com/NICKUSUARIO .

And I would like to know how to get this nick in the URL and move to the profile.php file for example.

I know how to use ?nick=nickuser , but I would like to make it more user-friendly to type having to only put meusite.com/nickusuario .

    
asked by anonymous 19.08.2015 / 17:34

4 answers

3

Just use parse_url to interpret a URL and return its components , and later the trim function to remove /

$parse = parse_url( 'http://www.meusite.com/NICKUSUARIO' );
echo trim( $parse['path'] , '/' );

The code above returns only NICKUSUARIO .

Update

The full URL you get using 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']

    
19.08.2015 / 17:51
3

I think you're trying to create friendly URLs, right? For this, you can create a .htaccess file and set it up so that the text after the bar is interpreted as a GET parameter, then to receive it in PHP you usually use $_GET (in the case $_GET['nick'] ), as if the url were actually ?nick=nickuser .

It would look like this:

.htaccess

RewriteEngine On

#Reescrita de URL
#Na linha abaixo será definido que o parâmetro nick poderá receber letras e números
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?nick=$1

PHP file , you would normally receive:

PHP

<?php
echo $_GET['nick'];
?>

In the case of the url accessed be 'profile.php / testName' or 'profile.php? nick = testName' the result will be the display of:

testeNome
    
19.08.2015 / 22:50
1
$parse = parse_url( 'http://www.meusite.com/NICKUSUARIO' );
$value = trim( $parse['path'] , '/' );

Your url would be fixed and the user would just type what comes after /, ex: http://www.meusite.com/stack

your $ value variable would have the value you need there is only you use it as you want, ex: <?php header("location:user.php?nick=".$value);?>

    
19.08.2015 / 19:52
0

To simplify , I believe that doing so will resolve in the case:

 $value = trim($_SERVER['PATH_INFO'],'/');

There are other ways ...

a) Using parse_url() :

    $parse = parse_url($_SERVER['REQUEST_URI']);
    $value = trim( $parse['path'] , '/' );

b) Using preg_replace() :

$value = preg_replace('/(.+).com\/(.*)/','$2', $_SERVER['REQUEST_URI']);
    
19.08.2015 / 20:44