1

I have a URL in this format:

https://www.example.com/activate/9ajebaidblahdeblahblah2020

My setup is /activate/index.php

How can I parse /9ajebaidblahdeblahblah2020 with the index.php file in the /activate folder?

I have tried...

$current_url = $_SERVER['REQUEST_URI'];
echo $current_url;

//OR

var_dump($_SERVER['REQUEST_URI']);

What I would like to do is (purpose)...

$current_url = explode("/", $_SERVER['REQUEST_URI']);
//echo $current_url[2];

Thank you ahead of time for any help.

RustyNox
  • 409
  • 6
  • 7
  • Use [parse_url()](https://www.geeksforgeeks.org/php-parse_url-function/). Hope this works for you – Robin Singh Oct 26 '20 at 06:41
  • As in my example, same with parse_url it returns a 404. Assume I don't get to setup the URL (in my database). – RustyNox Oct 26 '20 at 06:45
  • If you are using Apache, you will have to add some .htaccess URL rewriting to it(similar for nginx) – nice_dev Oct 26 '20 at 06:48
  • Yeah, I though so, was hoping there might have been some new magic method since 7.x. Perhaps I will need to go with /activate/index.php/9ajebaidblahdeblahblah2020 at minimum, this I can change in the db. – RustyNox Oct 26 '20 at 06:50
  • I have decided to stay with my original URL, but include index.php because it is never seen except in the email link, the file is just a processor, and then I can explode the result and do like: $authKey = explode("/", $_SERVER['REQUEST_URI']); echo $authKey[3]; /activate/index.php/9ajebaidblahdeblahblah2020 – RustyNox Oct 26 '20 at 07:22

3 Answers3

2

Add a .htaccess file in your activate project folder with the below code:

RewriteEngine On

RewriteRule ^/?activate/(.+)$ /activate/index.php?val=$1 [NC,L,P]

Demo: https://htaccess.madewithlove.be?share=aa64900e-ba78-4f17-9369-326f4384dd47

Later in your index.php file, you can just use as:

<?php

echo $_GET['val'];
nice_dev
  • 17,053
  • 2
  • 21
  • 35
1

You don’t have a page named 9ajebaidblahdeblahblah2020 on your server.

You can redirect all URL’s to index.php with htaccess (https://httpd.apache.org/docs/current/howto/htaccess.html).

See more details here: Redirect all to index.php using htaccess

zvi
  • 3,677
  • 2
  • 30
  • 48
0
<php

$url = $_SERVER['REQUEST_URI'];

$route_path = parse_url($url, PHP_URL_PATH);

$segments = array_filter(explode("/", $route_path));

print_r($segments);

You'll get the following Result,

Array( [1] => activate, [2] => 9ajebaidblahdeblahblah2020 )