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
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.

Link to comment
Share on other sites

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']));
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.