1

This is probably a very easy question. Anyway how do you use variables from a url without requests. For example:

www.mysite.com/get.php/id/123

Then the page retrieves id 123 from a database.

How is this done? Thanks in advance!

UPDATE

If i have the following structure:

support/
        sys/
            issue/
                  issue.php
            .htaccess
            home.php
            etc.....

With .htaccess file containing:

RewriteEngine On 
RewriteRule ^/issue/(.*)$ /issue/issue.php?id=$1 [L]

Why do I have to type:

http://www.mysite.com/support/sys/issue/issue/1234

In order to load a file? When I want to type

http://www.mysite.com/support/sys/issue/1234

also, how do I then retrieve the id once the file loads?

0

7 Answers 7

2

Problem

This is a very basic/common problem which stems from the fact that your .htaccess rule is rewriting a url which contains a directory which actually exists...

File structure

>support
    >sys
        >issue
            issue.php
        .htaccess

(I.e. the directory issue and the .htaccess file are in the same directory: sys)

Rewrite Issues

Then:

RewriteEngine ON
RewriteRule ^issue/(.*)/*$ issue/issue.php?id=$1 [L]
# Note the added /* before $. In case people try to access your url with a trailing slash

Will not work. This is because (Note: -> = redirects to):
http://www.mysite.com/support/sys/issue/1234
-> http://www.mysite.com/support/sys/issue/issue.php?id=1234
-> http://www.mysite.com/support/sys/issue/issue.php?id=issue.php

Example/Test

Try it with var_dump($_GET) and the following URLs:

Output will always be:

array(1) { ["id"]=> string(9) "issue.php" } 

Solution

You have three main options:

  1. Add a condition that real files aren't redirected
  2. Only rewrite numbers e.g. rewrite issue/123 but not issue/abc
  3. Do both

Method 1

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^issue/(.*)/*$ issue/issue.php?id=$1 [L]

Method 2

RewriteEngine ON
RewriteRule ^issue/(\d*)/*$ issue/issue.php?id=$1 [L]

Method 3

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^issue/(\d*)/*$ issue/issue.php?id=$1 [L]

Retrieving the ID

This is the simple part...

$issueid = $_GET['id'];
Sign up to request clarification or add additional context in comments.

Comments

1

In your .htaccess you should add:

RewriteEngine On
RewriteRule ^id/([^/]*)$ /get.php/?id=$1 [L]

Also like previous posters mentioned, make sure you have your mod_rewrite activated.

1 Comment

What have you tried? This are pretty much basics, I warmly suggest you head over to php.net/manual/en/index.php and start reading on things like php.net/manual/en/reserved.variables.server.php
0

You have to use a file called .htaccess, do a search on Google and you'll find a lot of examples how to accomplish that.

Comments

0

You will need mod_rewrite (or the equivalent on your platform) to rewrite /get.php/id/123 to /get.php?id=123.

Comments

0

I tried and tried the .htaccess method but to no avail. So I attempted a PHP solution and came up with this.

issue.php

<?php
if (strpos($_SERVER['REQUEST_URI'], 'issue.php') !== FALSE){
    $url = split('issue.php/', $_SERVER['REQUEST_URI']);
}elseif (strpos($_SERVER['REQUEST_URI'], 'issue') !== FALSE){
    $url = split('issue/', $_SERVER['REQUEST_URI']);
}else{
    exit("URI REQUESET ERROR"); 
}

$id = $url[1];

if(preg_match('/[^0-9]/i', $id)) {
    exit("Invalid ID");
}
?>

Comments

0

What you're looking for is the PATH_INFO $_SERVER variable.

From http://php.net/manual/en/reserved.variables.server.php:

'PATH_INFO'

Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.

explode() it and work on its parts.

EDIT: Use rewrite rules to map the users' request URLs to your internal structure and/or hide the script name. But not to convert the PATH_INFO to a GET query, that's totally unnecessary! Just do a explode('/',$_SERVER['PATH_INFO']) and you're there!

Also, seeing your own answer, you don't need any preg_mathes. If your database only contains numeric ids, giving it a non-numeric one will simply be rejected. If for some reason you still need to check if a string var has a numeric value, consider is_numeric().

Keep it simple. Don't reinvent the wheel!

Comments

0

Just wondering why no answer has mentioned you about use of RewriteBase

As per Apache manual:

The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.

Using RewriteBase in your /support/sys/issue/.htaccess, code will be simply:

RewriteEngine On
RewriteBase /support/sys/issue/

RewriteRule ^([0-9+)/?$ issue.php?id=$1 [L,QSA]

Then insde your issue.php you can do:

$id = $_GET['id'];

to retrieve your id from URL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.