GoLinks – PHP/MySQL Links Directory Script

GoLinksHere at XEWeb we are cracking open a bottle of champagne as we announce the new version of GoLinks.

GoLinks is a simple yet powerful and efficient links directory script with many features including unlimited categories, link submission, search engine friendly URL’s, multiple themes and language support with completely customizable templates.

Take a look at http://www.golinks-script.com to download, order and view a demo online.

A simple and lightweight Javascript calendar

This Javascript code will display a calendar in a table, useful for date picking…

// these are labels for the days of the week
cal_days_labels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

// these are human-readable month name labels, in order
cal_months_labels = ['January', 'February', 'March', 'April',
                     'May', 'June', 'July', 'August', 'September',
                     'October', 'November', 'December'];

// these are the days of the week for each month, in order
cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// this is the current date
var cal_current_date = new Date();

function Calendar(month, year) {
  this.month = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
  this.year  = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
}

Calendar.prototype.generateHTML = function(){

  // get first day of month
  var firstDay = new Date(this.year, this.month, 1);
  var startingDay = firstDay.getDay();
 
  // find number of days in month
  var monthLength = cal_days_in_month[this.month];
 
  // compensate for leap year
  if (this.month == 1) { // February only!
    if((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0){
      monthLength = 29;
    }
  }

  // get todays date
  var cal_today_date = new Date();
  checkDay = (cal_today_date.getMonth() == this.month && cal_today_date.getFullYear() == this.year) ? true : false;
 
  // do the header
  var monthName = cal_months_labels[this.month]
  var html = '<table class="calendar-table" cellpadding="4" cellspacing="0">';
  html += '<tr><th><a href="javascript:cal.prevMonth()">&#171;</a></th><th colspan="5">';
  html +=  monthName + "&nbsp;" + this.year;
  html += '</th><th><a href="javascript:cal.nextMonth()">&#187;</a></th></tr>';
  html += '<tr class="calendar-header">';
  for(var i = 0; i <= 6; i++ ){
    html += '<td class="calendar-header-day">';
    html += cal_days_labels[i];
    html += '</td>';
  }
  html += '</tr><tr>';

  // fill in the days
  var day = 1;
  // this loop is for is weeks (rows)
  for (var i = 0; i < 9; i++) {
    // this loop is for weekdays (cells)
    for (var j = 0; j <= 6; j++) {
      html += '<td class="calendar-day">';
      if (day <= monthLength && (i > 0 || j >= startingDay)) {
        ts = this.generateTimestamp(day);
        if (checkDay == true && day == cal_today_date.getDate())
          html += '<a class="today" href="javascript:void(0)">' + day + '</a>';
        else
          html += '<a href="javascript:void(0)">' + day + '</a>';
        day++;
      }
      html += '</td>';
    }
    // stop making rows if we've run out of days
    if (day > monthLength) {
      break;
    } else {
      html += '</tr><tr>';
    }
  }
  html += '</tr></table>';
  document.getElementById('calendar-holder').innerHTML = html;
}

Calendar.prototype.generateTimestamp = function(day) {
  month = this.month + 1;
  return "'" + day + "','" + month + "','" + this.year + "'";
}

Calendar.prototype.prevMonth = function() {
  if (this.month == 0) {
    this.month = 12;
    this.year = (this.year - 1);
  }
  this.month = (this.month - 1);
  this.generateHTML();
}

Calendar.prototype.nextMonth = function() {
  if (this.month == 11) {
    this.month = -1;
    this.year = (this.year + 1);
  }
  this.month = (this.month + 1);
  this.generateHTML();
}

To display the calendar for example…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
    <title>Select date</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="css/calendar.css" />
    <script type="text/javascript" src="scripts/calendar.js"></script>
</head>
<body>
<div id="calendar-holder">
<script type="text/javascript">
// <![CDATA[
 var cal = new Calendar();
 cal.generateHTML();
// ]]>
</script>
</div>
</body>
</html>

Convert plain text to clickable links in PHP

A useful function if you want to convert a link in plain text into a clickable link…

function clickable_links($text) {
  $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)', '<a target="_blank" href="\1">\1</a>', $text);
  $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)', '\1<a target="_blank" href="http://\2">\2</a>', $text);
  return $text;
}

How to setup one time download of your files securely

How to setup one time download of your files securelyIf you are selling a digital product and want to offer it for download but are concerned that people will share the URL to the file, you could use a solution like ClickBank. However the advantage of setting up one time downloads from your server instead is that you have more control over what you are selling and you don’t have to pay any fee’s.

Let us assume that you have setup and integrated a payment solution (such as PayPal using the IPN, post coming soon on this) and you have a database setup that holds the payment information.

CREATE TABLE `transactions` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`transaction_id` VARCHAR( 50 ) NOT NULL ,
`file_downloaded` TINYINT( 1 ) NOT NULL
) ;

Now we will create download.php which will be the link sent to the user after they have paid to download your files.

To keep it secure you would include the transaction ID (which would be unique to each payment) as part of the query string, so the link would be:

http://www.site.com/download.php?transaction_id=XX

Now you do need the files to be uploaded somewhere on your web server, however the location will never be revealed. The best way is to put them in a folder outside of the web root so that they cant be accessed via HTTP. However if this is not possible then put them in a folder name that cannot be guessed (use a random password generator if you don’t have any ideas).

<?php
// Path to the files to be downloaded
$file = '/home/usr/files/blah.zip';

// Transaction ID
$transaction_id = $_GET['transaction_id'];

// Connect to database
mysql_connect('localhost', 'user', 'pass');
mysql_select_db('dbname');

// Lookup the transaction ID in the database
$query = sprintf("SELECT * FROM transactions WHERE transaction_id='%s'",
mysql_real_escape_string($transaction_id));
$query = mysql_query($query);
$row = mysql_fetch_array($query);

// Valid transaction ID?
if (!$row) {
  die('Invalid transaction ID!');
}

// File already been downloaded?
elseif ($row['file_downloaded'] == 1) {
  die('File has already been downloaded, please contact us if you have any problems');
}

else {

  // It's a valid transaction, update the database so that we know the file has been downloaded for next time
  $query = sprintf("UPDATE transactions SET file_downloaded = 1 WHERE transaction_id='%s'",
  mysql_real_escape_string($transaction_id));
  $query = mysql_query($query);

  // Now force the file to be downloaded
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename='.basename($file));
  header('Content-Transfer-Encoding: binary');
  header('Expires: 0');
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  ob_clean();
  flush();
  readfile($file);
  exit();

}
?>

Using reCAPTCHA to stop spam in PHP

Using reCAPTCHA to stop spam in PHPreCAPTCHA helps prevent automated abuse of your site (such as comment spam or bogus registrations) by using a CAPTCHA to ensure that only humans perform certain actions.

You may have seen reCAPTCHA in use on many websites (including our forum) as it’s completely free and comes with an excellent web service which we are going to implement in this article.

The first thing to do is sign up to reCAPTCHA to receive your public and private key. These keys are used to authorize your account.

Next you need to download the reCAPTCHA library in PHP which can be found on this page. Once you have this you are ready to go.

First we are going to store the public and private key in variables, this would usually go in something like config.php.

<?php
include('recaptchalib.php');
$publickey = "…";
$privatekey = "…";

// Rest of your config goes here…
?>

Now on the page you want to display the CAPTCHA (e.g. register.php) you would include this at the top:

<?php
// Include the config file
include('config.php');

// If the form has been submitted
if (!empty($_POST)) {

  // Send the CAPTCHA results to reCAPTCHA
  $resp = recaptcha_check_answer ($privatekey, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);

  // Incorrect CAPTCHA?
  if (!$resp->is_valid) {
    $error = 'The reCAPTCHA was not entered correctly!';
  }

  else {
    // Otherwise continue with further validation etc
  }
}
?>

Now on the same page we display the actual CAPTCHA which is generated by the library so we don’t have to worry about any HTML or Javascript.

<form action="" method="post">
<!– Form fields would go here –>
<p><?php echo recaptcha_get_html($publickey); ?></p>
<p><input type="submit" name="submit" value="Complete Sign Up" /></p>
</form>