Email verification using tokens – CakePHP

Web & Code 16 Comments

I recently wrote a code for an email verification system using cakephp. Well, before you use it there are few things. This is written by a cake noob so may not be the best there is. I have not written it as a general stand alone piece of code. So its need modifications for use. And I am a sucker when it comes to commenting and formatting my code so please dont mind. My thanks to Edward and the CakePhP Google Groups. And finally cake rocks!!!

cheers

Database Table

A basic user table with the following columns

id - primary key and auto increment
username
email
password
[...]
tokenhash*
activate *

*columns with which we are concerned with

Controller code

<?php
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Form');
var $components = array('Email');
[...]
function register() {
if (!empty($this->data)) {
$this->User->data = $this->data;
$hash=sha1($this->data['User']['username'].rand(0,100));
//Create Token using form data and random number to ensure its unique and cannot be replicated
$this->User->data['User']['tokenhash']=$hash;
if ( $this->User->validates()) {
$this->User->save($this->data);
//Save all form data including the tokenhash
$ms='Click on the link below to complete registration ';
$ms.='www.sitename.com/users/verify/t:'.$hash.'/n:'.$this->data['User']['username'].'';
$ms=wordwrap($ms,70);
//create mail body
$this->Email->from = 'yourName <email>';
$this->Email->to=$this->data['User']['email'];
$this->Email->subject = 'Confirm Registration for Niwiki - reg.';
$this->Email->send($ms);
//send mail
$this->Session->setFlash('Please Check your email for validation Link');
$this->redirect('/users/login');
exit;
}
}
}

Read the rest of this entry »

Simple RSS reader based on PHP

Web & Code No Comments

There are many RSS parsers, readers and 3rd party scripts which do the work of reading a rss feed and displaying it on an external web page. When I ran into this problem recently, I decided to write for myself a plain simple code which could do the job without much of a hassle.

<?php
$startitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
	global $startitem, $tag, $title, $description, $link;
	if ($startitem) {
		$tag = $name;
	} elseif ($name == "ITEM") {
		$startitem = true;
	}
}
function endElement($parser, $name) {
	global $startitem, $tag, $title, $description, $link;
	if ($name == "ITEM") {
	printf("<dt><a href='%s'>%s</a></dt>",
		trim($link),htmlspecialchars(trim($title)));
	printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
		$title = "";
		$description = "";
		$link = "";
		$startitem = false;
	}
}
function characterData($parser, $data) {
	global $startitem, $tag, $title, $description, $link;
	if ($startitem) {
	switch ($tag) {
		case "TITLE":
		$title .= $data;
		break;
		case "DESCRIPTION":
		$description .= $data;
		break;
		case "LINK":
		$link .= $data;
		break;
	}
	}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("YOUR RSS LINK HERE","r")
	or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
	xml_parse($xml_parser, $data, feof($fp))
		or die(sprintf("XML error: %s at line %d",
		xml_error_string(xml_get_error_code($xml_parser)),
		xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>

P.S. The code is not entirely my own. I have referred Kevin Yank’s Tutorial . If you want to understand how the whole thing works, I suggest that you read through it.

© 2009, Nikhil Hullur | Entries RSS Comments RSS