PHP RSS作家类

/* E X A M P L E -----------------------------------------------
		$feed = new RSS();
		$feed->title       = "RSS Feed Title";
		$feed->link        = "http://website.com";
		$feed->description = "Recent articles on your website.";

		$db->query($query);
		$result = $db->result;
		while($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			$item = new RSSItem();
			$item->title = $title;
			$item->link  = $link;
			$item->setPubDate($create_date); 
			$item->description = "<![CDATA[ $html ]]>";
			$feed->addItem($item);
		}
		echo $feed->serve();
	---------------------------------------------------------------- */

	class RSS
	{
		var $title;
		var $link;
		var $description;
		var $language = "en-us";
		var $pubDate;
		var $items;
		var $tags;

		function RSS()
		{
			$this->items = array();
			$this->tags  = array();
		}

		function addItem($item)
		{
			$this->items[] = $item;
		}

		function setPubDate($when)
		{
			if(strtotime($when) == false)
				$this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
			else
				$this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
		}

		function getPubDate()
		{
  			if(empty($this->pubDate))
				return date("D, d M Y H:i:s ") . "GMT";
			else
				return $this->pubDate;
		}

		function addTag($tag, $value)
		{
			$this->tags[$tag] = $value;
		}

		function out()
		{
			$out  = $this->header();
			$out .= "<channel>\n";
			$out .= "<title>" . $this->title . "</title>\n";
			$out .= "<link>" . $this->link . "</link>\n";
			$out .= "<description>" . $this->description . "</description>\n";
			$out .= "<language>" . $this->language . "</language>\n";
			$out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";

			foreach($this->tags as $key => $val) $out .= "<$key>$val</$key>\n";
			foreach($this->items as $item) $out .= $item->out();

			$out .= "</channel>\n";
			
			$out .= $this->footer();

			$out = str_replace("&", "&", $out);

			return $out;
		}
		
		function serve($contentType = "application/xml")
		{
			$xml = $this->out();
			header("Content-type: $contentType");
			echo $xml;
		}

		function header()
		{
			$out  = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
			$out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
			return $out;
		}

		function footer()
		{
			return '</rss>';
		}
	}

	class RSSItem
	{
		var $title;
		var $link;
		var $description;
		var $pubDate;
		var $guid;
		var $tags;
		var $attachment;
		var $length;
		var $mimetype;

		function RSSItem()
		{ 
			$this->tags = array();
		}

		function setPubDate($when)
		{
			if(strtotime($when) == false)
				$this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
			else
				$this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
		}

		function getPubDate()
		{
			if(empty($this->pubDate))
				return date("D, d M Y H:i:s ") . "GMT";
			else
				return $this->pubDate;
		}

		function addTag($tag, $value)
		{
			$this->tags[$tag] = $value;
		}

		function out()
		{
			$out .= "<item>\n";
			$out .= "<title>" . $this->title . "</title>\n";
			$out .= "<link>" . $this->link . "</link>\n";
			$out .= "<description>" . $this->description . "</description>\n";
			$out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";

			if($this->attachment != "")
				$out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";

			if(empty($this->guid)) $this->guid = $this->link;
			$out .= "<guid>" . $this->guid . "</guid>\n";

			foreach($this->tags as $key => $val) $out .= "<$key>$val</$key\n>";
			$out .= "</item>\n";
			return $out;
		}

		function enclosure($url, $mimetype, $length)
		{
			$this->attachment = $url;
			$this->mimetype   = $mimetype;
			$this->length     = $length;
		}
	}

PHP MySQL转储

if (!function_exists('mysql_dump')) {

   function mysql_dump($database) {

      $query = '';

      $tables = @mysql_list_tables($database);
      while ($row = @mysql_fetch_row($tables)) { $table_list[] = $row[0]; }

      for ($i = 0; $i < @count($table_list); $i++) {

         $results = mysql_query('DESCRIBE ' . $database . '.' . $table_list[$i]);

         $query .= 'DROP TABLE IF EXISTS `' . $database . '.' . $table_list[$i] . '`;' . lnbr;
         $query .= lnbr . 'CREATE TABLE `' . $database . '.' . $table_list[$i] . '` (' . lnbr;

         $tmp = '';

         while ($row = @mysql_fetch_assoc($results)) {

            $query .= '`' . $row['Field'] . '` ' . $row['Type'];

            if ($row['Null'] != 'YES') { $query .= ' NOT NULL'; }
            if ($row['Default'] != '') { $query .= ' DEFAULT \'' . $row['Default'] . '\''; }
            if ($row['Extra']) { $query .= ' ' . strtoupper($row['Extra']); }
            if ($row['Key'] == 'PRI') { $tmp = 'primary key(' . $row['Field'] . ')'; }

            $query .= ','. lnbr;

         }

         $query .= $tmp . lnbr . ');' . str_repeat(lnbr, 2);

         $results = mysql_query('SELECT * FROM ' . $database . '.' . $table_list[$i]);

         while ($row = @mysql_fetch_assoc($results)) {

            $query .= 'INSERT INTO `' . $database . '.' . $table_list[$i] .'` (';

            $data = Array();

            while (list($key, $value) = @each($row)) { $data['keys'][] = $key; $data['values'][] = addslashes($value); }

            $query .= join($data['keys'], ', ') . ')' . lnbr . 'VALUES (\'' . join($data['values'], '\', \'') . '\');' . lnbr;

         }

         $query .= str_repeat(lnbr, 2);

      }

      return $query;

   }

}

PHP 反SQL注入功能

function cleanuserinput($dirty){
	if (get_magic_quotes_gpc()) {
		$clean = mysql_real_escape_string(stripslashes($dirty));	 
	}else{
		$clean = mysql_real_escape_string($dirty);	
	} 
	return $clean;
}

PHP 下载文件

<?php

$filename = $_GET['filename'];

// Modify this line to indicate the location of the files you want people to be able to download
// This path must not contain a trailing slash.  ie.  /temp/files/download
$download_path = "ficheros/";
	
// Make sure we can't download files above the current directory location.
if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
$file = str_replace("..", "", $filename);
	
// Make sure we can't download .ht control files.
if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
	
// Combine the download path and the filename to create the full path to the file.
$file = "$download_path$file";
	
// Test to ensure that the file exists.
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
	
// Extract the type of file which will be sent to the browser as a header
$type = filetype($file);

// Get a date and timestamp
$today = date("F j, Y, g:i a");
$time = time();

// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);

?>

PHP 登录类

class Auth
{
	var $user_id;
	var $username;
	var $password;
	var $ok;
	var $salt = "34asdf34";
	var $domain = ".domain.com";

	function Auth()
	{
		global $db;
		
		$this->user_id = 0;
		$this->username = "Guest";
		$this->ok = false;
		
		if(!$this->check_session()) $this->check_cookie();
		
		return $this->ok;
	}
	
	function check_session()
	{
		if(!empty($_SESSION['auth_username']) && !empty($_SESSION['auth_password']))
			return $this->check($_SESSION['auth_username'], $_SESSION['auth_password']);
		else
			return false;
	}

	function check_cookie()
	{
		if(!empty($_COOKIE['auth_username']) && !empty($_COOKIE['auth_password']))
			return $this->check($_COOKIE['auth_username'], $_COOKIE['auth_password']);
		else
			return false;
	}
	
	function login($username, $password)
	{
		global $db;
		$db->query("SELECT user_id FROM users WHERE username = '$username' AND password = '$password'");
		if(mysql_num_rows($db->result) == 1)
		{
			$this->user_id = mysql_result($db->result, 0, 0);
			$this->username = $username;
			$this->ok = true;
			
			$_SESSION['auth_username'] = $username;
			$_SESSION['auth_password'] = md5($password . $this->salt);
			setcookie("auth_username", $username, time()+60*60*24*30, "/", $this->domain);
			setcookie("auth_password", md5($password . $this->salt), time()+60*60*24*30, "/", $this->domain);
			
			return true;
		}
		return false;
	}		

	function check($username, $password)
	{
		global $db;
		$db->query("SELECT user_id, password FROM users WHERE username = '$username'");
		if(mysql_num_rows($db->result) == 1)
		{
			$db_password = mysql_result($db->result, 0, 1);
			if(md5($db_password . $this->salt) == $password)
			{
				$this->user_id = mysql_result($db->result, 0, 0);
				$this->username = $username;
				$this->ok = true;
				return true;
			}
		}			
		return false;
	}
	
	function logout()
	{
		$this->user_id = 0;
		$this->username = "Guest";
		$this->ok = false;
		
		$_SESSION['auth_username'] = "";
		$_SESSION['auth_password'] = "";

		setcookie("auth_username", "", time() - 3600, "/", $this->domain);
		setcookie("auth_password", "", time() - 3600, "/", $this->domain);
	}

}

PHP 不错的URLS PHP .htaccess

.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
# http://domain/about -> http://domain/about.php

--------------------------------------------------

.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
# http://domain/about -> http://domain/index.php?q=about

<?php
// $url_data is an array
$url_data = explode("/",$HTTP_SERVER_VARS['PATH_INFO']);
?>

PHP 检查有效的电子邮件地址

function is_valid_email($email)
{
	if(preg_match("/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) > 0)
		return true;
	else
		return false;
}