Conor Mac Aoidh
http://macaoidh.name
conor@macaoidh.name
 

Search

Archives

  • July 2010
  • June 2010
  • April 2010
  • March 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008

Spam

4,729 spam comments
blocked by
Akismet

Tag Cloud

    bt broadband CMS conor's management system conormacaoidh conor mac aoidh content management system drums fast Fedora fedora 10 furasta furasta cms furasta org gnome guide hello world HTML icsp iPhone Java javascript joomla kde Linux Mandriva mc kennas Monaghan Music mysql php repository Scratch stealing the ceiling The Dominican Affair the pot smoking pirates the strats tutorial Twitter updates web design forum webme webworks weekly tweets Windows wordpress

Ads

PHP – Saving Database Queries

Posted May 18th, 2009 by Conor in in Languages,PHP

This is something I wrote today while building botb SNS – a project that I will have to start blogging about soon! It should be used on large scale projects that have multiple database queries. It will be very useful if you have so many queries that you can’t keep track of them and are afraid of re-querying incidentally. Enough said – down to the code:

<?php
class user{
var $id;
var $name='';
var $username='';
var $email='';

function __construct($id){
$this->id=$id;
}
function about($t){
if($this->$t!=”) return $this->$t;
$q=dbRow(‘select ‘.$t.’ from users where id=”‘.$this->id.’”‘);
$this->$t=$q[$t];
return $this->$t;
}
}
?>

You will obviously have to adjust this to suit your database setup. It also requires the following function:

function dbRow($q){
$s=mysql_query($q);
$r=mysql_fetch_assoc($s);
return $r;
}

As you can probably see this example simply echos database information. The main benefit of the class is it will never make the same query twice. Once you ask for the username it is stored within the class so if you ask for it again in the same page it doesn’t have to query the database again. You could look at it as a type of internal page caching. Also it is incredibly easy to use. All you need to do is:

$u=new user($id);
echo 'Query Database: '.$u->about('name');
echo '<br/> From Memory: '.$u->about('name');

Easy as that!

3 responses so far

Furasta Reconstruction

Posted May 4th, 2009 by Conor in in CMS,Web Projects

After a long pause in development I have begun writing the first stable version of Furasta – 0.2. I haven’t really done anything with the project since I set up the website a few months ago. But since then I have been building up experience by examining other open source projects, such as Webme, WordPress and Joomla. I learnt a lot from those projects and they have contributed a lot of ideas for Furasta!

I have abandoned all of the old code and opted to write the whole thing again from scratch. Considering how long it took me to write v0.1.5, about a month of flat out coding every night, I expect v0.2 to be completed in the same sort of timescale. One thing is for sure – there will be no micro-optimisation this time! After all the effort I put into that last time the damn program was still slow. This time I will invest my executable time in more important tasks, ones that only directly affect what’s going on as you load the page, and perform tasks such as server side caching through AJAX so the user doesn’t notice it.

The project is also going to be available from svn from now on. Since I am writing it from scratch there are only a few files in the svn at the moment and there’s also no installation script. But that will come in time!

http://code.google.com/p/furasta-cms/source/checkout

I am taking a completely different approach to writing the CMS this time. In fact I would go as far as calling it an engine rather than a CMS. Basically what I plan to do is build a solid framework that on it’s own does nothing, but has the ability to do everything. There will be no pages except the essential homepage. Everything, I mean everything that will give this program the characteristics of a CMS will be achieved by the plugin architecture. So for example to edit and display pages, I will create a plugin. Obviously the CMS will have to be shipped with a few default plugins, which it will be possible to disable through a plugin manager plugin! I might be going a bit overboard with this idea but in theory it seems like a good one!

So enough talking, time to start writing!

No responses yet

The Kember Identity

Posted May 2nd, 2009 by Conor in in Fedora,Languages,PHP

This morning on twitter I came across something called The Kember Identity. Elliott Kember seems to have thought of something that simply hasn’t occured to the rest of the world. That thing has been aptly named “The Kember Identity”. It’s homepage is located here.

Anyway the Kember Identity is an md5 hash. Each md5 hash contains 32 characters so the KID is the theoretical hash that when it is encrypted it returns itself. At the moment this is a theory as it has not been proven. So Elliott has started a competition to see if anyone can figure out if the KID actually exists and if so what it is.

I have chosen to enter this competition with a PHP script that I wrote just a few minutes ago. It’s fairly simple and I have added a html table in there to clean it up a bit. Instead of computing endlessly this script displays all failed equations as it goes. When it finds the KID it will stop and show it in bright green. I ran the script for a few minutes and got to 200,000 hashes with no luck. Also FireFox froze a few times while running it. The best thing to do is just leave it to do it’s job and don’t do anything else while it is working!

You can view it here: http://files.macaoidh.name/php/the-kember-identity/md5.php


<?php

function encript($str){ return md5($str); }

function generateSum(){ return md5(mt_rand()); }

function loKIDe(){
  $s=generateSum();
  $md5=encript($s);

  echo '<tr><td>'.$s.'</td><td>'.$md5.'</td>';

  if($s!=$md5) return false;
  else{
    echo '<tr style="color:green"><td>'.$s.'</td><td>'.$md5.'</td></tr>';
    return true;
  }
}

echo '
<h1>Finding the Kember Identity...</h1>
<table>
  <tr>
    <th>32 Digit Number</th>
    <th>Md5 Of Num</th>
    <th>Count</th>
  </tr>
';

$num='';

while(loKIDe()==false){
  $num++;
  echo '<td>'.$num.'</td></tr>';
}

echo '</table><br/><br/>  If you are seeing this then the line above is equal';

?>

This script should, in theory, find the KID. But it is really a matter of time. I am going to keep it running all night tonight and see if I have any luck. Kae is thinking of writing a JavaScript version that will spread the load between multiple computers thus reducing calculation time. So the race is on! I hope that I am the first to find this magical, mysterious number!

7 responses so far

Collecting Data From Forms – PHP / MySQL

Posted Mar 20th, 2009 by Conor in in HTML,Languages,PHP

Today i had quite a few requests for the same thing on the Web Design Forum. So instead of writing a different answer to all of the threads I decided to summarise it all in a blog post on Collecting Data From Forms Using PHP and MySQL. Hopefully I will just be able to link back to this post when I get this question again!

What is PHP? PHP is a programing language with an easy to use syntax. It resembles C and Java. PHP is excecuted server side which means that if you want to do something with the language then you will have to send a request to the server. This is usually done by pointing to a PHP file to proccess information. It can also be done using AJAX without requiring a reload – but that’s a whole other ball game!

What is MySQL? MySQL is one of many Structured Query Languages. It is basically a language used to store and manipulate data through the use of databases. I have often compared it to Microsoft’s Excel – many people tell me that I’m wrong with that comparison but I think that it is valid!

PHP + MySQL = Dynamic Webpage. In my opinion MySQL and PHP have one common characteristic; they both have a user friendly syntax which is easy to learn and yet they both have the ability to unleash massive amounts of power once you delve into them! The combination of the two languages produces a constantly changing, dynamic website. For example you could have page content being called from the database and then to update that content all you need to do is update the database – rather than trawling through code.

If you are finding this all hard to follow don’t worry, I’m sure it will be a bit more self-explanatory when we look at some code. I am not going to go into the detail of downloading and installing PHP and MySQL. The following assumes that you have done so yourself – or that you are working on an online server with these packages pre-installed.

Setting Up MySQL

First you will need to create a new database and a new table for all of the information to be stored in. Enter this command to create the database:

create database form;

Quickly select that database – tell MySQL that you want to use it.

use form;

Then you will need to create a table which will store our information.

create table users (
user_id int not null auto_increment primary key,
username text,
email text,
password char(40) default null
);

Now that you have the MySQL side of things running you will need to connect to MySQL with PHP. The file below should be named ‘connect.php’.  /*These things are comments */

<?php

/* The name of the database that we have just created */

$dbname = 'form';

/* The hostname which will usually be localhost but change if appropriate */

$hostname = 'localhost';

/* Database Username and Password - you must change these to your own values*/

$dbuser = 'root';

$dbpass = '';

$connect = mysql_connect($hostname,$dbuser,$dbpass) or die('Could not connect to MySQL please insure that the connection information is correct');

mysql_select_db($dbname,$connect) or die('Could not select the database');

?>

Writing The Form

Now that we have written our connection information with PHP we must include that file in our main PHP file and write a form. This is a normal HTML form which collects information from users wishing to register to whatever.

<form method="post">
Username: <input type="text" name="username"/><br/>
Email: <input type="text" name="email"/><br/>
Password: <input type="password" name="password"/><br/>
Confirm: <input type="password" name="confirmpass"/><br/>
<input type="submit" name="save" value="Register"/>
</form>

That is alright as it is but it doesn’t really do anything. If we want to collect that information then we must work our PHP magic! This file contains the same HTML form but it is written in PHP and is named ‘form.php’.

<?php

/* Include the connection file that we created earlier */

require_once 'connect.php';

/* If the user presses the submit save the information to the database and display a thank you message */

if(isset($_POST['save'])){

/* Get the values submitted from the form */

$username = addslashes($_POST['username']);

$email = addslashes($_POST['email']);

$pass = $_POST['password'];

$password = sha1(strip_tags($pass));

/* Check to see if the two password values are correct */

if($pass!=$_POST['confirmpass'] || $pass=='') die('Passwords do not match. <a href="form.php">Return to form.</a>');

/* Update the database with the new information or die with the mysql error */

mysql_query("insert into users values ('','$username','$email','$password')") or die(mysql_error());

/* Display thank you message and exit */

echo '

<h1>',$username,' Thank You For Registering!</h1>

';

exit;

}

/* Else display the form so the user can register */

echo '

<form method="post">

Username: <input type="text" name="username"/><br/>

Email: <input type="text" name="email"/><br/>

Password: <input type="password" name="password"/><br/>

Confirm: <input type="password" name="confirmpass"/><br/>

<input type="submit" name="save" value="Register"/>

</form>

';

/* Close MySQL, exit and close PHP */

mysql_close();

exit;

?>

Woah! Thank god that’s over. If all goes well you will not have any errors running that script. Anyway that’s all the advice I can give you for one day. Good luck!

Please feel free to use or adapt this code as you will. I understand that this isn’t a prime example but it sets the foundation of what you can customise in order to create a brilliant user freindly form! You could also try adding more verification to the form or even using some unobstructive JavaScript to make it look nice. These are all things that I might write a tutorial about in the future but that’s it for now!

No responses yet
Next Entries »



Conor's Blog is powered by Wordpress | Template design by Conor Mac Aoidh