Jump to content

Get Value after slash in URL


farnoise

Recommended Posts

Hi There,

I am trying to this trick with PHP that get the last Value of the URL that the user enters in the Address bar in the browser (Not that that this is not a form) and use that value.

 

for example I have a index page that starts with the code to do trick, then your comes to my website www.abcded.com/username

So what I need to do is take the value which in this case is "Username" so I can use it, and as you know in normal case it will look for the folder called username under my root folder.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/259277-get-value-after-slash-in-url/
Share on other sites

I don't think this can be achieved by PHP alone.

 

As you mentioned, in the normal case, it will look for a folder (a file would probably do too). If that fails, your server will probably throw a 404 error without calling any of your PHP scripts. Therefore, I think a PHP-only solution doesn't exist.

 

However, this can definitely be accomplished using a .htaccess file and a RewriteRule.

Although, if it is possible that the user may type in the index.php and you always want the parent folder, then this would work as well

$root_folder = basename($_SERVER['REQUEST_URI']);
if(substr($root_folder, -4) == '.php')
{
    $root_folder = basename(dirname($_SERVER['REQUEST_URI']));
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.