-1

After successfully complete the registration form I am redirecting the user to ccroipr-id.php?id=63 page. Here a new value is saved to the database which is

ccroipr-201801101515569979

this value is changed for every new registration.

Now, for every profile page, I am showing the data using ccroipr-id.php?id=63 page.

But now I want the url will be

ccroipr-201801101515569979.php

So, desired possible outcome is: domain.com/ccroipr/201801101515569979

Can you tell me how can I achieve this?

Note: I am learning PHP.

Shibbir
  • 409
  • 3
  • 14
  • you are using ajax request to create user or in PHP, means form submitted by ajax or php? – Sanjay Kumar Singh Jan 10 '18 at 08:22
  • @SanjayKumarSingh Yes, the registration page is validating using jQuery/Ajax call. After successfully submitting the data I just redirecting to the ccroipr-id.php page. – Shibbir Jan 10 '18 at 08:25
  • have you tried using header('location: ccroipr-201801101515569979.php') or window.location.href? – Suyog Jan 10 '18 at 08:27
  • @Suyog I know I can redirect them to `ccroipr-201801101515569979.php` page but the number is different for every registration. – Shibbir Jan 10 '18 at 08:28
  • so you can dynamically generate the file name every time. e.g. $num = date('YmdHis'); $page = 'ccroipr-'.$num.'.php'; header('Location:'.$page); – Suyog Jan 10 '18 at 08:31
  • @Suyog I want to use only one page e.g test.php for every `ccroipr-201801101515569979.php` page. This number will be different – Shibbir Jan 10 '18 at 08:33
  • @Suyog I don't want to create thousands of file. – Shibbir Jan 10 '18 at 08:35
  • @ShibbirAhmed Would it suit you then to use an URL like `your-domain.net/profile.php/201801101515569979`, does it have to be `ccroipr-regtimestamp+rnd.php`? – Philipp Maurer Jan 10 '18 at 09:32
  • @PhilippMaurer it will be better if it's `domain.com/ccroipr/201801101515569979`. Is it possible using .htaccess ? – Shibbir Jan 10 '18 at 09:52
  • @PhilippMaurer I am showing the profile data using `ccroipr-id.php?id=63` now If got this url `domain.com/ccroipr/201801101515569979` then I can show the exact data because id `63` is for this column value: `201801101515569979` – Shibbir Jan 10 '18 at 09:55
  • @ShibbirAhmed So another user might get redirected to the page `ccroipr-id.php?id=65` instead, leading to another value, like `201801101515569999`? – Philipp Maurer Jan 10 '18 at 11:20
  • @PhilippMaurer yes you right, only the number will be changed. – Shibbir Jan 10 '18 at 11:55

1 Answers1

0

To use the URL domain.com/ccroipr/201801101515569979 for a user profile, you need to implement the following two steps:

Create a php file called ccroipr.php on your site, that contains code to analyse the request's URL.

This can be accomplished, by using the PHP global $_SERVER variable in PHP. It contains a field named 'REQUEST_URI' that stores the path that was used to access the given file. So for the URL domain.com/ccroipr/201801101515569979 the variable $_SERVER['REQUEST_URI'] holds the value ccroipr/201801101515569979. This can be used to extract the suer id:

$requestParts = explode('/', strstr($_SERVER['REQUEST_URI'] . '?', '?', TRUE));
$userId = '';
if (count($requestParts) > 1) {
    $userId = $requestParts[1];
}

if ($userId === '') {
    // Redirect to login page, generate a new ID or show an error.
    die();
}
// Call database and filter for the $userId value to retrieve all userspecific data
// Generate the specific HTML

The function strstr($path, '?', TRUE) cuts off all the GET parameters from the requested path. You can still access them through the $_GET variable though. A question mark is added to the original path to make sure the function still acts like expected in case no such parameters were submitted.

The function explode('/', $url) then separates all parts of the path that are separated by slashes, so each part can be analysed for itself.

The condition if (count($requestParts) > 1) checks, if the path part for the user id was submitted in the given URL. If it was, it is saved in the $userId variable. Keep in mind though, that there still might have been submitted an empty string for it. This will happen if someone calls domain.com/ccroipr/ for example.

The condition if ($userId === '') then verifies, if a user id was really submitted.

Modify your .htaccess file to allow requests that omit the .php ending.

The way to accomplish that is best described in this question: How to execute a PHP web page without the .php extension in the URL?

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
  • You can use `$dbValue = str_replace($'-', '/', $dbValue, 1);` to transform `ccroipr-201801101515569979` to `ccroipr/201801101515569979` or vary from the solution to explode the path with '-' chars instead of slashes. Keep in mind that you have to consider in the .htaccess file as well, if you want to do it. – Philipp Maurer Jan 10 '18 at 12:06
  • Thanks for that dude but what happened if I access the site with `domain.com/ccroipr/` without the number? – Shibbir Jan 10 '18 at 12:18
  • @ShibbirAhmed The `$userId` variable would contain an empty string and therefore the condition `if ($userId === '')` would be true, resulting in an error, a redirect or anything else you program for that case. – Philipp Maurer Jan 10 '18 at 12:20
  • Thank You, dude. I will apply that code and let you know If I am getting any issue but thanks for your help. – Shibbir Jan 10 '18 at 12:29