Get URL and change the according image in PHP


change image according to url

Trick to change image according to URL in PHP

Hi people! This trick is pretty simple and very useful at sometimes.
Sometimes you might want to change image according to URL in any PHP page here is the tutorial.

STEP1

In first step you need to get the current url. See the code below
This code is a php function which allows you to find out the current page URL.
You can get the Current page URL like this
This will print the current page URL. But for this tutorial we need to store it in a variable.
We’ve stored the current page URL in the variable ‘$cur’.

Step 2

Now we need to split the URL according to ‘/’ and have to select the last part.
Note: if your URL is something like this ‘http://www.example.com/pages/about’ you’ve to use the above code as it is, but if your URL is Like this ‘http://www.example.com/pages/about/’ you’ve to change the ‘-1′ to ‘-2′.
The above code will store last part of the URL in the variable $url for example ‘about’.

Step 3

Now all you need to do is save an image in this name. eg. ‘about.jpg’
Then go to your page and put the following code where you want your image to be placed.
Hurrah!! if you go to that page you can see your image. Now include all the above codes into each page where you want to show the images and place the image with the same name of URL’s last part.

0 comments:

Post a Comment

www.comhttp.blogspot.in. Powered by Blogger.