Jump to content

writing a function based on if it is the index file or not?


kade119

Recommended Posts

i'm a complete newb to PHP , i'm trying to write a small script that if the statement is true than i want to show the flash on the index page and if not show an image

 

could someone get me in the right direction?

 

thanks

 

 

$flash = "http://www.testsite.com/banner.swf"

$page = basename($REQUEST_URI);
if ($page == 'index.php')
{ echo "$flash";}
else
{echo "<img src="banner.jpg";} 

Link to comment
Share on other sites

You're not far off. Try this...

 

<?php

$flash = 'http://www.testsite.com/banner.swf';

$page = basename($_SERVER['REQUEST_URI']);

if ($page == 'index.php' || $page == '') {
?>
<object width="550" height="400">
<param name="movie" value="<?php echo $flash ?>">
<embed src="<?php echo $flash ?>" width="550" height="400">
</embed>
</object>
<?php
}
else {
echo '<img src="banner.jpg" alt="Image Banner"/>';
}
?>

 

You have to remember that people can just go to http://www.testsite.com to access your homepage. The REQUEST_URI will be empty but they will still be on the homepage so you need to check for that to.

 

Also, the flash file URL needs to be wrapped in embed tags.

 

 

Link to comment
Share on other sites

You're not far off. Try this...

 

<?php

$flash = 'http://www.testsite.com/banner.swf';

$page = basename($_SERVER['REQUEST_URI']);

if ($page == 'index.php' || $page == '') {
?>
<object width="550" height="400">
<param name="movie" value="<?php echo $flash ?>">
<embed src="<?php echo $flash ?>" width="550" height="400">
</embed>
</object>
<?php
}
else {
echo '<img src="banner.jpg" alt="Image Banner"/>';
}
?>

 

You have to remember that people can just go to http://www.testsite.com to access your homepage. The REQUEST_URI will be empty but they will still be on the homepage so you need to check for that to.

 

Also, the flash file URL needs to be wrapped in embed tags.

You forgot to end the PHP before echoing the flash ;)

Link to comment
Share on other sites

Yeah sure...

 

Web servers can be set up to issue a default page if a user tries to access the domain with no URI. If this didn't happen, any user that went to http://www.testsite.com/ would see a blank page. The default page is usually either index.html, index.php or default.asp (for IIS machines).

 

If a user accesses http://www.testsite.com or http://www.testsite.com/index.php, they are accessing the same page as the former will user will simply be shown the default page. Because of this, there are two possible request URI's for the homepage and you will need to check for both of them.

 

Also, just thinking about it, you may sometimes find a backslash ( / ) as your request uri. You could use the following to check if your user is on the homepage:

 

<?php 
function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}
?>

Link to comment
Share on other sites

Yeah sure...

 

Web servers can be set up to issue a default page if a user tries to access the domain with no URI. If this didn't happen, any user that went to http://www.testsite.com/ would see a blank page. The default page is usually either index.html, index.php or default.asp (for IIS machines).

 

If a user accesses http://www.testsite.com or http://www.testsite.com/index.php, they are accessing the same page as the former will user will simply be shown the default page. Because of this, there are two possible request URI's for the homepage and you will need to check for both of them.

 

Also, just thinking about it, you may sometimes find a backslash ( / ) as your request uri. You could use the following to check if your user is on the homepage:

 

<?php 
function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}
?>

 

whoa... so how do i write that w/ what i have now ?

Link to comment
Share on other sites

<?php
// You should probably move this into your functions file
function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}

$flash = 'http://www.testsite.com/banner.swf';

if (is_homepage()) {
?>
<object width="550" height="400">
<param name="movie" value="<?php echo $flash ?>">
<embed src="<?php echo $flash ?>" width="550" height="400">
</embed>
</object>
<?php
}
else {
echo '<img src="banner.jpg" alt="Image Banner"/>';
}
?>

Link to comment
Share on other sites

so below is what i have so far, i didn't need the else statement since it is using swfobject, the image shows through whenever i strip the flash it uses the div bckgrnd image... 

 

problem: whenever i just type http://www.site.com/folder/  it's not playing the flash .. 

 

also what do the 2 single quotes that have nothing in them in the array for?

 

  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));

 

 

<?php

function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}
$banner = 'graphics/banner_02.png';
$flash = '<script type="text/javascript"> 
    swfobject.embedSWF("cool-blue-banner.swf", "banner", "578", "290", "9.0.0", false, false, {play:"true",loop:"false", wmode:"transparent"},
false);
</script>';
?>
<?php
if (is_homepage()) {
echo $flash;
}	
?>	

Link to comment
Share on other sites

the '' is basically a test for an empty string. If there is nothing behind the URL, ie. if you accessed www.phpfreaks.com, $_SERVER['REQUEST_URI'] will return an empty string.

 

If you are running the script within a folder, you will need to add the folder name to the test parameters, ie. for your example above, array('/folder ', '/folder/index.php', '/folder/index.html', '/folder/').

Link to comment
Share on other sites

Actually you won't have to add the folder into the array as the $_SERVER['REQUEST_URI'] has the basename function wrapped around it. This function takes a filename and path as a string and returns the filename portion. This means that any folder on it will be removed and therefore you won't have to check it.

 

To test your script, try printing out the value of basename($_SERVER['REQUEST_URI']) and see what you get on different pages. You can add which ever values are displayed on the homepage to the array.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.