0

I'm using cpanel and php to create my website.

how i can get id from url http://example.com/id

problem is that when i'm trying to get url , im redirecting to file with id name 404 error !

and i can't get path in index.php page ?

for example when i try to get this url

http://example.com/01OWUY

i want to get 01OWUY with

$link .= $_SERVER['REQUEST_URI']; 

code but i'm going to a 404 error

update :

every user have a id

with this id he can see her info

for example user with id 01OWUY can see her info from database like this ->

<?php

require 'config.php';

//Database connection
$conn = mysqli_connect(SERVERNAME, USERNAME, PASSWORD, DBNAME);

$user = handle_user($conn, $_SERVER['REQUEST_URI'])
?>

and the function is this ->

// Return true if link is active
function handle_user($conn, $linkId)
{
    $sql = "SELECT *FROM users WHERE link_id = '" . $linkId . "'";
    $result = $conn->query($sql);

    // get user data from db
    if ($result->num_rows > 0) {
        return $result;
    }
    return false;
}

but i get a 404 error !!!

AliBehrozi
  • 53
  • 9
  • Try to form your question like this: Given this world... (explain the situation), When I do this...(explain what do you do), I expect this to happen...(explain what do you expect to happen). – lewis4u Jul 11 '20 at 17:28
  • What do you mean by "get" the id? Because what would a 404 error have to do with `$_SERVER`? – GetSet Jul 11 '20 at 17:32
  • @lewis4u ok, i update my question – AliBehrozi Jul 11 '20 at 17:32
  • That means that the link `http://example.com/01OWUY` doesn't exist! Is it supposed to be there??? – lewis4u Jul 11 '20 at 17:32
  • Kindly show your source code. You want help so show it. Trust me, it will be more clear on what you mean then your current use of programming terminology as a beginner. – GetSet Jul 11 '20 at 17:34
  • @lewis4u i update my question again i hope help so this time – AliBehrozi Jul 11 '20 at 17:48
  • Did you try to debug it with var_dump($conn, $user); die(); what do you get? – lewis4u Jul 11 '20 at 17:54
  • when i try a path like ```http//example.com/154``` even if index page be a simple html file , i get a 404 error egain. my $conn is right becuse i test it befor . – AliBehrozi Jul 11 '20 at 17:58
  • @lewis4u but when i try URL with out id its working but ```$_SERVER['REQUEST_URI']``` give me a ```/``` only – AliBehrozi Jul 11 '20 at 17:59
  • Thanks for posting your code. It is now clear that you want to create a "router". You will need to edit your server config file. Here may be helpful starting point on that and the rest: https://stackoverflow.com/questions/20960877/the-basics-of-php-routing ... Read the answers section. – GetSet Jul 11 '20 at 18:05
  • And what happens when you var_dump($sql); die; inside handle_user() function – lewis4u Jul 11 '20 at 18:05
  • @lewis4u thank you for your question its work , and i will add a try catch in the function – AliBehrozi Jul 11 '20 at 18:19

1 Answers1

1

You will need to load index.php if the file does not exist.

this is a very common practice in php frameworks like Laravel.

.htaccess file

# Not for real file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

for this to work you must have mod_rewrite enabled

Then you will have the data available in your index.php file. you will still need to sanitize the input $_SERVER["REQUEST_URI"]


Recommendation for lightweight framework includes routing features:

Lumen

https://lumen.laravel.com/

Artistan
  • 1,982
  • 22
  • 33