0

If you have a url such as the one as follows:

http://www.example.com/400x200

is it possible to create a page which echos out 400x200 when the user visits that url using php?

I know how to echo the path - that is easy enough (ltrim($_SERVER['PATH_INFO'], '/')), but do not know how to create the page dynamically.

Any help would be much appreciated, thanks in advance

5 Answers 5

1

The request URI (/400x200) is stored in the server superglobal: $_SERVER["REQUEST_URI"].

You need to take that and route the URI accordingly. The simplest possible scenario: in your index.php, place this code:

$uri = trim($_SERVER["REQUEST_URI"],"/");

if (preg_match("/\d+x\d/")) {
    list($width,$height) = explode("x",$uri);
    // some logic with the above vars, e.g. include a view script
}

What this does is check whether the URI has the format {number}x{number}, extracts both numbers and stores them in the variables $width and $height. You can then do whatever you like with the variables.

In order to make the request always point to the file containing this code, edit your .htaccess and put in something like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

(the .htaccess code is copied from the default Zend Framework project, in case anyone asks).

Sign up to request clarification or add additional context in comments.

4 Comments

"they do not seem to give the right result" is a little vague. What happens?
If you go to url/index.php/600x400 , an image is correctly built to the dimensions, but not if you simply go to url/600x400
OK, but what happens if you go to url/600x400?
I'd wager it's because the URI is fetched incorrectly. var_dump your variables and see if they all contain values you want to work on.
1

You may want to look at Apache Rewrites for rewriting your URL:

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Comments

1

Do not know what do You mean by creating the page dynamically but I guess that using of mod_rewrite is what You need.

In Your .htaccess file You have to create some rules that will rewrite the URL to something distinguishable by Your PHP script - like from URL

http://www.example.com/400x200

to get

http://www.example.com/index.php?param=400x200

And then You can in Your index.php script do echo $_GET['param'];...

Google something about PHP and mod_rewrite: http://www.google.com/#q=PHP+mod_rewrite

Comments

1

Assuming you're using Apache, this can be done using something called URL rewriting. Create a file called .htaccess in your document root, and add this:

# Turn URL rewriting on
RewriteEngine on
Options +FollowSymlinks

# Rewrite rule
RewriteRule ^(\d+x\d+)/?$ index.php?dimensions=$1 [L]

The first two lines turn the rewrite engine on, and the third line defines a RewriteRule. The first part ^(\d+x\d+)/?$ is a regular expression that the part of the URL after the domain will be matched against.

The second part index.php?dimensions=$1 is the URI that will be rewritten to. The client doesn't see this, but PHP will.

If I do a print_r($_GET) in index.php with the URL http://localhost/400x300, I get this:

Array ( [dimensions] => 400x300 )

This is from the standard $_GET superglobal array in PHP and can be used as normal. URL rewriting leaves the URL as it is in the browser, yet allows you to turn it into one usable by PHP with a query string.


To make your script a bit easier to use, you could split the expression up to get separate X and Y values:

RewriteRule ^(\d+)x(\d+)/?$ index.php?x=$1&y=$2 [L]

Which will give an array like this:

Array ( [x] => 400, [y] => 300 )

Comments

0

Make it a GET variable like

http://www.example.com?size=400x200 

Then you can retrieve the String with

$size = $_GET['size'];

What I'd recommend you to do is to get the values based on split or explode()

$lw = $size.explode('x',$size);
$length = $lw[0];
$width = $lw[1];
//Manipulate the values accordingly like
echo $length.'x'.$width;

2 Comments

I tried that - the question is how to rewrite the GET variable to a shorter form of the url
The OP gives an example of their URL in their question. Your example assumes they want/can change the URL to use a query string.

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.