Detecting the URL and choosing a statement.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

Detecting the URL and choosing a statement.

Post by Master_Phantom »

Hi comm,

First I´m a bit new in PHP but I understand it well.

I´m going to tell what is my objetive:

I have a site that I want to show in the index "a.jpg" and in the other pages "b.jpg"

For example I want something like this:

Code: Select all

 <?php
 
if (myURL == "http://mydomain.com/index.php"){
 
echo ("<div align='center'><img src='a.jpg'>");
 
} else {
 
echo ("<div align='center'><img src='b.jpg'>");
 
?> 
The problem is that I need how to get the URL into a variable. :cry:

Thanks for advance

Greetings

Master Phantom
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: Detecting the URL and choosing a statement.

Post by hansford »

$myUrl = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Johnlbuk
Forum Newbie
Posts: 19
Joined: Mon Sep 10, 2007 3:01 pm

Re: Detecting the URL and choosing a statement.

Post by Johnlbuk »

Hi,

You can use the following CurlPageName function and then use an if statement. This just fetches the page name so you can use it without knowing the domain to use it on.

Code: Select all

<?php
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
 
$pagename = curPageName();
 
if ($pagename == "index.php"){
 
echo ("<div align='center'><img src='a.jpg'>");
 
} else {
 
echo ("<div align='center'><img src='b.jpg'>");
 
}
?>
This will work for you!
John
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

Re: Detecting the URL and choosing a statement.

Post by Master_Phantom »

Woooooow,

Thanks a lot, you´re very quick, I´m going to test it right now.

Thanks again :D

A++

Master Phantom
Johnlbuk
Forum Newbie
Posts: 19
Joined: Mon Sep 10, 2007 3:01 pm

Re: Detecting the URL and choosing a statement.

Post by Johnlbuk »

No problem.
If you need any further assistance let me know.

John
Post Reply