Community & Support

Code for simple Facebook, Digg, del.icio.us, Reddit links

The Service Links module does this to the nth degree, but here is some code if you are looking simple way to drop in social networking links.

<A target="_new" HREF="http://digg.com/submit?url=<?php print $GLOBALS['base_url'].'/'; print $path; ?>&title=<?php print $title; ?>"> Digg</A>
<A target="_new" HREF="http://del.icio.us/post?url=<?php print $GLOBALS['base_url'].'/'; print $path; ?>title=<?php print $title; ?>">del.icio.us</A>
<A target="_new" HREF="http://reddit.com/submit?url=<?php print $GLOBALS['base_url'].'/'; print $path; ?>title=<?php print $title; ?>">Reddit</A>
<A target="_new" HREF="http://www.myspace.com/Modules/PostTo/Pages/?t=<?php print $title; ?>&c=&u=<?php print $GLOBALS['base_url'].'/'; print $path; ?>&l=5">MySpace</A>
<A target="_new" HREF="http://www.facebook.com/sharer.php?u=<?php print $GLOBALS['base_url'].'/'; print $path; ?>&t=<?php print $title; ?>">Facebook</A>

Comments

LIttle bit better

Discovered that the above doesn't work in all situations. This code should work better.....

<A  HREF="http://digg.com/submit?url=<?php print $GLOBALS['base_url'].'/'; if ($path) { print $path; } if (!$path) { print 'node/'.$nid; } ?>&title=<?php print $title; ?>&bodytext=&media=&topic=">Digg</A>

<A HREF="http://del.icio.us/post?url=<?php print $GLOBALS['base_url'].'/'; if ($path) { print $path; } if (!$path) { print 'node/'.$nid; } ?>title=<?php print $title; ?>">del.icio.us</A>

<A  HREF="http://reddit.com/submit?url=<?php print $GLOBALS['base_url'].'/'; if ($path) { print $path; } if (!$path) { print 'node/'.$nid; } ?>title=<?php print $title; ?>">Reddit</A>

<A HREF="http://www.myspace.com/Modules/PostTo/Pages/?t=<?php print $title; ?>&c=&u=<?php print $GLOBALS['base_url'].'/'; if ($path) { print $path; } if (!$path) { print 'node/'.$nid; }  ?>&l=5">MySpace</A>

<A target="_new" HREF="http://www.facebook.com/sharer.php?u=<?php print $GLOBALS['base_url'].'/'; if ($path) { print $path; } if (!$path) { print 'node/'.$nid; }  ?>&t=<?php print $title; ?>">Facebook</A>

Easiest way for me

I dont know if this is the Drupal way, but I found that using $_SERVER['REQUEST_URI'] was the easiest way. With this method, you do not have to worry about path aliases or node/nid.

<?php
$title
= drupal_get_title();
$path_to_images = 'sites/all/media/share_icons/'; //This will need to be changed
?>

<table width="369" border="0" cellpadding="10">
  <tr>
    <td><div align="center"><a href="http://www.facebook.com/sharer.php?u=<?php global $base_root;
print
$base_root; print $_SERVER['REQUEST_URI']; ?>
&t=<?php print $title; ?>" target="_blank"><img src="<?php
global $base_path; print $base_path; ?>
<?php print $path_to_images; ?>facebook-logo.gif" alt="Facebook" width="50" height="50"><br>
      Facebook<br>
    </a></div></td>
    <td><div align="center"><a href="http://del.icio.us/post?url=<?php global $base_root;
print
$base_root; print $_SERVER['REQUEST_URI']; ?>
&title=<?php print $title; ?>" target="_blank"><img src="<?php
global $base_path; print $base_path; ?>
<?php print $path_to_images; ?>icon-delicious.gif" alt="Del.icio.us" width="50" height="50"><br>
    Del.icio.us</a></div></td>
    <td><div align="center"><a href="http://digg.com/submit?url=<?php global $base_root;
print
$base_root; print $_SERVER['REQUEST_URI']; ?>
&title=<?php print $title; ?>&bodytext=&media=&topic=" target="_blank"><img src="<?php
global $base_path; print $base_path; ?>
<?php print $path_to_images; ?>icon-digg.gif" alt="DIGG" width="50" height="50"><br>
    Digg</a></div></td>
    <td><div align="center"><a href="http://reddit.com/submit?url=<?php global $base_root;
print
$base_root; print $_SERVER['REQUEST_URI']; ?>
&title=<?php print $title; ?>" target="_blank"><img src="<?php
global $base_path; print $base_path; ?>
<?php print $path_to_images; ?>icon-reddit.gif" alt="Reddit" width="50" height="50"><br>
    Reddit</a></div></td>
    <td><div align="center"><a href="http://www.stumbleupon.com/submit?url=<?php global $base_root;
print
$base_root; print $_SERVER['REQUEST_URI']; ?>
%26title%3D<?php print $title; ?>" target="_blank"><img src="<?php
global $base_path; print $base_path; ?>
<?php print $path_to_images; ?>icon-stumble.gif" alt="Stumble" width="50" height="50"><br>
    Stumble</a></div></td>
    <td><div align="center"><a href="http://www.myspace.com/Modules/PostTo/Pages/?t=<?php print $title; ?>&c=&u=<?php global $base_root;
print
$base_root; print $_SERVER['REQUEST_URI']; ?>
&l=5" target="_blank"><img src="<?php
global $base_path; print $base_path; ?>
<?php print $path_to_images; ?>myspace_logo.gif" width="50" height="50"><br>
    Myspace</a></div></td>
  </tr>
</table>

This code is insecure

This code is not secure as it is posted above.

The reason is that it outputs user input unfiltered, and therefore is a conduit for Cross Site Scripting (XSS) attacks.

The proper way is to do this:

<?php
$uri
= urlencode($_SERVER['REQUEST_URI']);
?>

Then use $uri everywhere in the above code that uses REQUEST_URI.

--
Drupal performance tuning, development, customization and consulting: 2bits.com, Inc.
Personal blog: Baheyeldin.com.

That too. Furthermore &c=&u

That too.

Furthermore &c=&u etc is not valid in HTML. This should be &amp;c=&amp;u.

Simply check_plain the entire URL.

nobody click here