Tag Archives: Tutorial

!JoomlaComments dofollow links

!JoomlaComments is a well known Joomla! component that allows you to have comments on your Joomla! site articles. The links are by default set to nofollow and it’s not possible to set this in component’s configuration.

If you wish to make comments links in your blog dofollow, you’ll have to do a little hack within the component core itself :

Open this file : /components/com_comments/joscomment/comment.class.php and look for « nofollow », which should bring you to line 2293 :

$website = “ “;

Remove « nofollow » attribute :

$website = “ “;

That’s all!

Joomla, custom redirection after sign in

When a user signs in in Joomla, he is automatically redirected at homepage.

This article explains you how to add a hack in Joomla 1.5, to redirect the user to the page of your choice after he signed in. It may be useful, especially if you want the user to do a specific action (e.g. : to post an article, a picture, subscribe to your services, etc.). From back-end, you may specify a Joomla! page for redirection, but no custom URL.

I used Joomla 1.5.8 for this example, but it should be the same pattern for every other J1.5.x version.

Open this file :


[votre site]/components/com_user/controller.php

Look for this code line, that should be located at line 163, inside the login() function :

login() :

$mainframe->redirect( $return );

All you have to do is to replace $return by the URL where you want users to be redirected. For instance :

$mainframe->redirect( “http://www.guylabbe.ca” );

Dynamic RSS feeds

RSS feeds became essential for blogs and any other website that publishes content on a regulary basis. It’s a good way to broadcast your content and allow your visitors to follow your blog by suscribing to your feeds.

It allows also to submit your RSS feeds in RSS directory, which will give you deep links (other links than your homepage), in addition to bring new visitors from the directory because you show them interesting content before they have access to your website.

Most of CMS (as Joomla, WordPress, Dotclear, etc.) already have an embeded script that’ll generate RSS feeds for you. But if you own a dynamic website, scripted « by hand », you’ll have to do it by yourself…

Let’s say that you own a dynamic PHP website that provides articles from time to time. We’ll create a PHP function to extract articles from the database, then to write them in a XML file, which will be your RSS feed. This way, your RSS reed will update itself automatically.

GENERATE RSS FEED AUTOMATICALLY WITH URL REWRITING

To do it, we’ll use URL Rewriting, because it allows to do all that in a much cleaner way, by simulate the RSS file on demand, instead of constantly editing it in a real RSS file.

Create a .htaccess that you will place to root of your website, or edit the .htaccess file you may already own :


Options +FollowSymlinks
RewriteEngine on
# Link for RSS feed
RewriteRule ^rss\.xml$ /rss.php [L,NC]

With that rewriting, rss.php file will generate automatically the XML content of the RSS feed; it’ll simulate the file.

Create a file named rss.php. We extract, in the following example, last articles from an imaginary blog.

Structure of the table used in the example :


CREATE TABLE IF NOT EXISTS `blog` (
`id` mediumint(9) NOT NULL COMMENT ‘ID of the article’,
`texte` text NOT NULL COMMENT ‘Text of the article’,
`date` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Articles of the blog’;

To insert into rss.php :

// MySQL connexion
$host = “localhost”; // normally, localhost
$user = “USERNAME”;
$pass = “PASSWORD”;
$bdd = “DB_NAME”;

@mysql_connect($host,$user,$pass) or die(“Connexion error: “.mysql_error());
@mysql_select_db(“$bdd”) or die(“Database selection error: “.mysql_error());

// Specify content type : XML
header(“Content-Type: text/xml”);

// MySQL query that extracts content from DB
$requete = “SELECT id, date FROM blog ORDER BY date DESC LIMIT 10″; // last 10 articles
$resultat = mysql_query($requete);

// XML writing
// header :
$sm = ”;
$sm .= ‘xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd”>’;

// Articles of the blog
while ($blog=mysql_fetch_array($resultat,MYSQL_ASSOC)) {
$xml = ‘‘;
$xml .= ‘‘;
$xml .= ‘RSS FEED TITLE‘; // Title of your RSS feed. For instance : Last articles of blog xyz
$xml .= ‘HTTP://WWW.YOURSITE.COM/‘; // Your website’s URL
$xml .= ‘‘.$title.’‘; // Your website’s description

$xml .= ‘‘;
$xml .= ‘YOUR_SITE/IMAGE.GIF‘; // An image, logo
$xml .= ‘IMAGE TITLE‘; // Normally the same thing as the title before
$xml .= ‘HTTP://WWW.YOURSITE.COM/‘; // your website’s URL
$xml .= ‘DESCRIPTION‘; // Description of you website (for image)
$xml .= ‘‘;
}

$i = 0;

foreach($news as $a) {

if($i < 12) { $id = $a['date']; $datexml = explode('-', $a['date']); $date2=date("D, d M Y H:i:s", mktime(0,0,0, $datexml[1],$datexml[2],$datexml[0])); $lien = "http://www.yoursite.com/articles.php?id=" . $id; // Link to your article (modify as your wish) $xml .= '‘;
$xml .= ‘‘.$a[‘titre’].’‘; // Article title
$xml .= ‘‘.$lien.’‘; // Link to article
$xml .= ‘‘.$lien.’‘; // Permalink
$xml .= ‘‘.$date2.’ GMT‘;
$xml .= ‘‘.strip_tags($a[‘txt’]).’‘; // Text of the article, removing HTML tags that it may contain
$xml .= ‘
‘;

$i++;
}
}

// end of XML
$xml .= ‘‘;
$xml .= ‘‘;
echo $xml;

Then, when you will try to reach http://www.votresite.com/rss.xml, rss.php will be called an a XML file will be generated and shown, with 10 last articles of your blog.

If you wish, it’s also possible to make a function with this code, and instead of simulating the RSS file with URL Rewriting, you can directly write into a rss.xml file. All you have to do is write something like this, to write in a real file :

// write file
$fp = fopen(“flux.xml”, ‘w+’);
fputs($fp, $xml);
fclose($fp);

How to get age from date of birth

If you are running an online community, you may have to show the age of a user, depending on the date of birth he provided on its profile. There are several solutions for it, but sometimes, it may be useful to get this information directly from the MySQL query.


SET @dateofbirth = ‘1987-05-30’;
SELECT (SELECT EXTRACT(YEAR FROM CURRENT_DATE) – EXTRACT(YEAR FROM @dateofbirth) – (CASE WHEN EXTRACT(MONTH FROM CURRENT_DATE) < EXTRACT(MONTH FROM @dateofbirth) THEN 1 ELSE (CASE WHEN (EXTRACT(DAY FROM CURRENT_DATE) < EXTRACT(DAY FROM @dateofbirth) AND EXTRACT(MONTH FROM CURRENT_DATE) = EXTRACT(MONTH FROM @dateofbirth)) THEN 1 ELSE 0 END) END)) AS Age

You must give the date the way MySQL likes it : YY-MM-DD.

The result of the example above will be the age that has today a person born the 30th of May 1987 (« today » = date this article was written). We used SET in this example to simplify and to avoid querying data in a user table.

If you have, for instance, a table called Users, which owns a column that contains dates of birth of all users of the community (called Date_of_birth), you could make your MySQL query this way :


SELECT Username, Date_of_birth, (SELECT EXTRACT(YEAR FROM CURRENT_DATE) – EXTRACT(YEAR FROM Date_of_birth) – (CASE WHEN EXTRACT(MONTH FROM CURRENT_DATE) < EXTRACT(MONTH FROM Date_of_birth) THEN 1 ELSE (CASE WHEN (EXTRACT(DAY FROM CURRENT_DATE) < EXTRACT(DAY FROM Date_of_birth) AND EXTRACT(MONTH FROM CURRENT_DATE) = EXTRACT(MONTH FROM Date_of_birth)) THEN 1 ELSE 0 END) END)) AS Age FROM Users

Selon la « date d’aujourd’hui », le résultat devrait ressembler à ceci :

Username Date_of_birth Age
Phil14 1991-12-02 14:31:19 17
MaryJuicy03 1966-02-12 14:32:34 42
Richard23 1997-12-18 14:33:29 10

 

Remove user control on SWF files

If you work on Flash projects, there is a trick that you should know (if you don’t already know it). The user may always, as it is default set, zoom in and zoom out in a flash SWF file. He can also do what he wants with the timeline reading (rewind, pause, stop, play), etc… but there is a way to get rid of this problem.

It is very easy to do, even if your Actionscript knowledge isn’t too deep, or isn’t at all. All your have to do is :

  1. Click on the main keyframe of your flash document
  2. Open the script window (F9 on PC, CMD+F9 on Mac)
  3. Type this :


Stage.showMenu = false;

Truncate string with PHP

It’s very simple, but useful; for instance, when you make a listing of articles on a page, but you only want to display a preview of the content of each article. Here is a small PHP function for text truncation :

function truncate_text($text, $nbrChar, $append=’…’) {
if(strlen($text) > $nbrChar) {
$text = substr($text, 0, $nbrChar);
$text .= $append;
}
return $texte;
}

// Now, we apply the function to a text :
echo truncate_text(‘this is a text I\’d like to truncate at fifty characters’, 50);

// which will have as result :
// this is a text I’d like to truncate at fifty cha…