kade119 Posted April 7, 2010 Share Posted April 7, 2010 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";} Quote Link to comment Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 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. Quote Link to comment Share on other sites More sharing options...
mattal999 Posted April 7, 2010 Share Posted April 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 That's my bad... I've modified it in case anyone copies it in the future! Thanks! Quote Link to comment Share on other sites More sharing options...
kade119 Posted April 7, 2010 Author Share Posted April 7, 2010 thanks much man, but can you explain this.. 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. Quote Link to comment Share on other sites More sharing options...
kade119 Posted April 7, 2010 Author Share Posted April 7, 2010 so does adding SERVER bring them back to the root? Quote Link to comment Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 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', '/')); } ?> Quote Link to comment Share on other sites More sharing options...
kade119 Posted April 7, 2010 Author Share Posted April 7, 2010 if ($page == 'index.php' || $page == '') { does this mean if the page is equal to index.php or if page is equal to nothing? Quote Link to comment Share on other sites More sharing options...
kade119 Posted April 7, 2010 Author Share Posted April 7, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 <?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"/>'; } ?> Quote Link to comment Share on other sites More sharing options...
kade119 Posted April 8, 2010 Author Share Posted April 8, 2010 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; } ?> Quote Link to comment Share on other sites More sharing options...
jeffshen Posted April 8, 2010 Share Posted April 8, 2010 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/'). Quote Link to comment Share on other sites More sharing options...
kade119 Posted April 8, 2010 Author Share Posted April 8, 2010 thanks man - that clarifies it Quote Link to comment Share on other sites More sharing options...
br0ken Posted April 8, 2010 Share Posted April 8, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.