Simple PHP script to add dynamic Stumbleupon button
Written by Amatocianmonk
2008-12-17
This is a simple piece of code to add a dynamic stumbleupon button to your website.
We first need to get the current page url in order to create the link.
Here, $_SERVER["SERVER_NAME"] is the domain of your server (www.amatoc.com) and $_SERVER["REQUEST_URI"] is the current path (/folder/file.php). We append http:// to the front of those two server variables to return the full URL. So it all becomes http://amatoc.com/folder/file.php.
<?
$pageURL = 'http://';
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
?>
We then add the following code to create the link and display this stumbleupon button
,
<?
$pageURL = 'http://';
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
<a href="http://www.stumbleupon.com/submit?url=' . $pageURL . '" target="_blank"><img border="0" src="http://www.stumbleupon.com/images/su_micro.gif" alt="Stumbleupon button" /></a>
?>
That is all of the code that you need to generate your button for users to submit your page to stumbleupon without having to hard code it into every page. This is helpful if you want to do something like we have on this site.
If you would like to be more robust and verify the server port and append the proper URL if it isnt 80, you can use the code below.
$pageURL = 'http://';
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
echo('
<a href="http://www.stumbleupon.com/submit?url=' . $pageURL . '" target="_blank"><img border="0" src="http://www.stumbleupon.com/images/su_micro.gif" alt="Stumbleupon button" /></a>');
?>
2009-07-03
Now can you give us one to block it?