troubleshooting Question

How do I insert this php code into my php file?

Avatar of badwolfff
badwolfffFlag for United Kingdom of Great Britain and Northern Ireland asked on
PHPJoomlaMySQL Server
8 Comments1 Solution430 ViewsLast Modified:
I have a working version (modified) of the Sige plugin for Joomla 1.6
which can be downloaded from:

Within this I have custom added a script which shortens long URLs manually.
The file sige.php is attached here:  sige.php
I shorten the long URLs that this plugin generates (for the facebook/twitter buttons) with the following code at the top of this file (lines 70-99):
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1') {
	$sql = "SELECT * FROM `url_cache` WHERE `before`='".mysql_real_escape_string($url)."' LIMIT 1";
	$check = mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($check)==1) {//we found it in cache
		$url = mysql_fetch_assoc($check);
		return $url['after'];

	//create the URL
	$bitly = ''.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

	//get the url
	//could also use cURL here
	$response = file_get_contents($bitly);

	//parse depending on desired format
	if(strtolower($format) == 'json') {
		$json = @json_decode($response,true);
		$sql = "INSERT INTO `url_cache` SET `before`='".mysql_real_escape_string($url)."', `after`='".mysql_real_escape_string($json['results'][$url]['shortUrl'])."'";
		mysql_query($sql) or die(mysql_error());
		return $json['results'][$url]['shortUrl'];
	else //xml
		$xml = simplexml_load_string($response);
		$sql = "INSERT INTO `url_cache` SET `before`='".mysql_real_escape_string($url)."', `after`='".mysql_real_escape_string(''.$xml->results->nodeKeyVal->hash)."'";
		mysql_query($sql) or die(mysql_error());
		return ''.$xml->results->nodeKeyVal->hash;

Open in new window

Later I call this code to shorten my URLs in real time with this code (lines 804-810):
$url = $mosConfig_live_site."/plugins/content/sige/plugin_sige/socialshare.php?img=".$mosConfig_live_site.$rootfolder.$_images_dir_."/Midsize/".$images[$a]['filename']."&name=".$imagename;

$u = make_bitly_url($url,'badwolfff','R_f650e553b78bb0569fc67dbc16a5de7c','json');

$html .= '<a href="'.$u.'&t=Zuenelli - '.$imagename.'"  title="aggiungi a facebook" target="_blank"><img src="'.$mosConfig_live_site.'/plugins/content/sige/plugin_sige/facebook.png" /></a><a href=" propone - '.$imagename.' (cliccare sul seguente link per scoprire): '.$u.'"  title="pubblica su twitter" target="_blank"><img src="'.mosConfig_live_site.'/plugins/content/sige/plugin_sige/twitter.png" /></a>

Open in new window

The working versions can be tested on the following link (try clicking on the facebook/twitter links):
My test website

The problem is this:
at present I use the service with my api login/key but I would like to use a custom url shortening service which runs from my own server.

I succeeded in doing this by following another web tutorial.
The code is as follows:
- short.php
$baseurl = 'http://localhost/'; //address to use as the first part of the short URLs (note trailing slash)
$sql_host = 'localhost'; //host of your mysql database (usually localhost)
$sql_user = 'root'; //mysql user with access to your database
$sql_pass = 'root'; //password for this user
$sql_db = 'turl'; //the name of your database
$db = mysql_connect($sql_host, $sql_user, $sql_pass);
mysql_select_db($sql_db, $db);
if(isset($_GET['k'])) {
   $k = mysql_real_escape_string($_GET['k'], $db);
   if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
      if(mysql_num_rows($result) > 0) {
         $row = mysql_fetch_row($result);
         header('HTTP/1.1 301 Moved Permanently');
         header('Location: ' . $row[0]);
if(isset($_POST['submit'], $_POST['url'])) {
   if(filter_var($_POST['url'], FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) !== false) {
      $url = mysql_real_escape_string($_POST['url'], $db);
      if($result = mysql_query("SELECT `key` FROM `turl` WHERE `url` = '" . $url . "'", $db)) {
         if(mysql_num_rows($result) > 0) {
            $row = mysql_fetch_row($result);
            $key = $row[0];
      if(!isset($key) && mysql_query("INSERT INTO `turl` (`url`) VALUES ('" . $url . "')", $db)) {
         $id = mysql_insert_id($db);
         $key = base_convert($id, 10, 36);
         mysql_query("UPDATE `turl` SET `key` = '" . $key . "' WHERE `id` = '" . $id . "'");
      echo '<a href="' . $baseurl . $key . '" target="_blank">' . $baseurl . $key . '</a>';
else {
<form method="post" action="short.php">
<label>URL: <input type="text" name="url" id="url" /></label>
<br />
<input type="submit" name="submit" id="submit" value="Shorten" />

Open in new window

RewriteEngine On
RewriteCond %{REQUEST_URI} \/([0-9a-z]+)$ [NC]
RewriteRule ^(.*) short.php?k=%1 [L]

Open in new window

sql code
  `url` text NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `key` (`key`)

Open in new window

A working example can be seen here:Working URL shortening example


What I would like to do is to substitute the short URL generation code in my sige.php with the working code from short.php. Sige.php generates URLs on the fly and I need my new code with the short.php script to generate all shortened URLs on the fly as my icons get created from my images folder and loaded on my page (same as how it works now with the code).

How could I manage that? I am not a php expert so I would really appreciate the exact code that I need to modify/substitute in my sige.php document.

thanks in advance

p.s. I also assume that after I manage to do that I will have to leave the .htaccess file inside the
folder - right?
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros