0

I am developing a simple profile website that allows different users to create a profile and share their profile link so that other can view their profile. I need to use one page to output the profile data instead of creating several pages for each user.

I am using Xampp version 7.3.5-0 runing Apache Server and PHP7. My current profile link is pro/profile/?user=mike, where pro/ is the site URL, profile is a folder containing index.php file to display the user profile and ?user=mike is the query string. Mike in this query string is the username of the current profile to view, if you want to view the profile for another user, say John, you just replace 'mike' with 'john'. Below is a sample query code and error code in index.php file available in the /profile folder:

 <?php
  require('db_connect.php');
  $username=($_GET['user']);
  $result=mysqli_query($db_connect,"SELECT * FROM  users WHERE 
  username='$username' LIMIT 1");
  $count=mysqli_num_rows($result);
  while($row=mysqli_fetch_array($result)){
?>

<?php 
  if ($count==0) {
    echo "Oops! No information was found for $username";
   }else{
   echo "";
}
?>

Currently if I want to view profiles for three people, say mike, john and hilda, I will use: 1. pro/profile/?user=mike 2. pro/profile/?user=john 3. pro/profile/?user=hilda If I input a user that does not exist, it will display the error message above.

Now I would like to use a different code that will output the user profile without using the GET method, but still using one index.php file instead of creating a folder and a page for each user because there are hundreds of users. The links would change to these: 1. pro/profile/mike 2. pro/profile/john 3. pro/profile/hilda

I don't know if it is possible, please help.

Chris
  • 4,672
  • 13
  • 52
  • 93
  • Check [this](https://stackoverflow.com/questions/5921075/get-last-word-from-url-after-a-slash-in-php/5921179#5921179) out . – Swati May 28 '19 at 17:50

1 Answers1

0

You can easily get the username from the url path like this:

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$parts = explode('/', $path);
$username = end($parts);

or you can use:

$username = basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
Sunchezz
  • 740
  • 6
  • 21
  • Tried this it was displaying page not found, then I followed some suggested links up there, I found out that I need .htaccess file placed at the same folder as that of index.php, now its working using this code in .htaccess: `Options +FollowSymLinks # Turn on the RewriteEngine RewriteEngine On # Rules RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /pro/profiles/` – Mikeotizels May 28 '19 at 19:11