PDA

View Full Version : گفتگو: کلاس های مفید برای پی اچ پی



mohsen_31369
سه شنبه 19 دی 1391, 11:25 صبح
با سلام.
در php کلاس های متنوع و زیادی نوشته شده اند که اکثر برنامه نویسان برای کارهایشان از آنها استفاده می کنند. شاید هم شما کلاس های مفیدی نوشته اید که به درد بقیه هم بخورد.
برای جمع آوری و معرفی کلاس های مفید از تمام دوستان دعوت می کنم که آنها را در اینجا برای استفاده ی سایرین معرفی نمایند.
با تشکر

mohsen_31369
سه شنبه 19 دی 1391, 11:32 صبح
اولین کلاس رو خودم معرفی می کنم
class.upload.php
کلاس آپلود و ویرایش عکس ها. که به آسانی می توان با آن کار کرد
دارای قابلیت هایی مانند resize,crop,rotate,border,tint و ... دموی (http://www.verot.net/php_class_upload_samples.htm) این کلاس
دانلود کلاس (http://www.verot.net/php_class_upload_download.htm)

mohsen_31369
سه شنبه 19 دی 1391, 11:43 صبح
PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/)
از اسمش معلومه. برای پارس کردن فایل های html. دارای امکانتی شبیه به امکانات jQuery DOM
مثلا :


// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');

$html->find('div', 1)->class = 'bar';

$html->find('div[id=hello]', 0)->innertext = 'foo';

echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>

دانلود (http://sourceforge.net/projects/simplehtmldom/files/)

mohsen_31369
سه شنبه 19 دی 1391, 11:50 صبح
LightOpenId (http://gitorious.org/lightopenid)
اگه می خواین تو کارتون لاگین با گوگل و یاهو ... بزارین یه نگاهی به این کلاس بندازین

mohsen_31369
سه شنبه 19 دی 1391, 12:09 عصر
PHPExcel (http://phpexcel.codeplex.com/)
این کلاس رو که همه میشناسن. برای کار با خواندن و ایجاد فایل های xlsx و pdf هست
دانلود (https://github.com/PHPOffice/PHPExcel)

mohsen_31369
سه شنبه 19 دی 1391, 13:43 عصر
PHP PDO Wrapper Class (http://www.imavex.com/php-pdo-wrapper-class/)
اگر می خواین از PDO (http://php.net/manual/en/book.pdo.php) تو پروژتون استفاده کنین می تونین از این کلاس کمک بگیرین
مثال اتصال به دیتابیس


// MySQL
$db = new db("mysql:host=127.0.0.1;port=8889;dbname=mydb", "dbUser", "dbPasswd");
//SQLite
$db = new db("sqlite:db.sqlite");



مثال select


//select Method Declaration
public function select($table, $where="", $bind="", $fields="*") { }

//SELECT #1
$results = $db->select("mytable");

//SELECT #2
$results = $db->select("mytable", "Gender = 'male'");

//SELECT #3 w/Prepared Statement
$search = "J";
$bind = array(
":search" => "%$search"
);
$results = $db->select("mytable", "FName LIKE :search", $bind);

mohsen_31369
سه شنبه 19 دی 1391, 13:55 عصر
CpanelInc / xmlapi-php (https://github.com/CpanelInc/xmlapi-php)
کلاسی برای کار کردن با cPanel's XML-API
با این کلاس می توانید به cpanel دسترسی داشته باشین. به عنوان مثال می توانید email,account,subdomain ایجاد نمایید و خیلی کارهای دیگر انجام دهید
مثال برای ساخت subdomain


require_once('xmlapi.php.inc');
$xmlapi = new xmlapi('127.0.0.1');
$xmlapi->password_auth('root','password');
$xmlapi->set_debug(1);
print $xmlapi->api1_query('accountname','SubDomain','addsubdomain ',array('sub','domain.com',0,0,'/public_html/folder'));

mohsen_31369
سه شنبه 19 دی 1391, 15:09 عصر
PHPCrawl webcrawler (http://phpcrawl.cuab.de/)
یه تجربه ی گوگلی داشته باشین با php!
این یک خزنده (Crawl) می باشد که می توانید اطلاعاتی از قبیل صفحات و لینک ها و ... از سایت های دیگر بدست آورید.
برای شروع شما باید متد handleDocumentInfo را مطابق نیازتون ویرایش نمایید. بهتر است یک کلاس فرزند ایجاد کرده و کدهای این متد را در آنجا وارد نمایید


include("libs/PHPCrawler.class.php");
class MyCrawler extends PHPCrawler
{
function handleDocumentInfo(PHPCrawlerDocumentInfo $PageInfo)
{
// Your code comes here!
// Do something with the $PageInfo-object that
// contains all information about the currently
// received document.

// As example we just print out the URL of the document
echo $PageInfo->url."\n";
}
}



یک مثال از داکیومنت سایت ارائه کننده، این مثال فقط بعضی از صفحات سایت برنامه نویس رو اسپایدر(spider) می کنه تا اینکه محدودیت ترافیک به 1mb برسد و بعد از آن اطلاعات و خلاصه ی اطلاعات رو چاپ می کنه و این کار احتمالا کمی طول بکشد پس set_time_limit(10000); تنظیم شده است. به متد handleDocumentInfo توجه کنید که چطور نوشته شده است


// It may take a whils to crawl a site ...
set_time_limit(10000);

// Inculde the phpcrawl-mainclass
include("libs/PHPCrawler.class.php");

// Extend the class and override the handleDocumentInfo()-method
class MyCrawler extends PHPCrawler
{
function handleDocumentInfo($DocInfo)
{
// Just detect linebreak for output ("\n" in CLI-mode, otherwise "<br>").
if (PHP_SAPI == "cli") $lb = "\n";
else $lb = "<br />";

// Print the URL and the HTTP-status-Code
echo "Page requested: ".$DocInfo->url." (".$DocInfo->http_status_code.")".$lb;

// Print the refering URL
echo "Referer-page: ".$DocInfo->referer_url.$lb;

// Print if the content of the document was be recieved or not
if ($DocInfo->received == true)
echo "Content received: ".$DocInfo->bytes_received." bytes".$lb;
else
echo "Content not received".$lb;

// Now you should do something with the content of the actual
// received page or file ($DocInfo->source), we skip it in this example

echo $lb;

flush();
}
}

// Now, create a instance of your class, define the behaviour
// of the crawler (see class-reference for more options and details)
// and start the crawling-process.

$crawler = new MyCrawler();

// URL to crawl
$crawler->setURL("www.php.net");

// Only receive content of files with content-type "text/html"
$crawler->addContentTypeReceiveRule("#text/html#");

// Ignore links to pictures, dont even request pictures
$crawler->addURLFilterRule("#\.(jpg|jpeg|gif|png)$# i");

// Store and send cookie-data like a browser does
$crawler->enableCookieHandling(true);

// Set the traffic-limit to 1 MB (in bytes,
// for testing we dont want to "suck" the whole site)
$crawler->setTrafficLimit(1000 * 1024);

// Thats enough, now here we go
$crawler->go();

// At the end, after the process is finished, we print a short
// report (see method getProcessReport() for more information)
$report = $crawler->getProcessReport();

if (PHP_SAPI == "cli") $lb = "\n";
else $lb = "<br />";

echo "Summary:".$lb;
echo "Links followed: ".$report->links_followed.$lb;
echo "Documents received: ".$report->files_received.$lb;
echo "Bytes received: ".$report->bytes_received." bytes".$lb;
echo "Process runtime: ".$report->process_runtime." sec".$lb;


اینم نمایش نتایج


Page requested: http://barnamenevis.org/ (200)
Referer-page:
Content received: 192854 bytes

Page requested: http://barnamenevis.org/forum.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 177819 bytes

Page requested: http://barnamenevis.org/register.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 40201 bytes

Page requested: http://barnamenevis.org/faq.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 23602 bytes

Page requested: http://barnamenevis.org/content.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 31281 bytes

Page requested: http://barnamenevis.org/calendar.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 18637 bytes

Page requested: http://barnamenevis.org/group.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 35280 bytes

Page requested: http://barnamenevis.org/album.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 17499 bytes

Page requested: http://barnamenevis.org/forumdisplay.php?do=markread&markreadhash=guest (302)
Referer-page: http://barnamenevis.org/
Content received: 0 bytes

Page requested: http://barnamenevis.org/showgroups.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 94375 bytes

Page requested: http://barnamenevis.org/online.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 44213 bytes

Page requested: http://barnamenevis.org/showthread.php?54352 (200)
Referer-page: http://barnamenevis.org/
Content received: 48021 bytes

Page requested: http://barnamenevis.org/search.php??search_type=1 (200)
Referer-page: http://barnamenevis.org/
Content received: 20213 bytes

Page requested: http://barnamenevis.org/index.php? (200)
Referer-page: http://barnamenevis.org/
Content received: 177664 bytes

Page requested: http://barnamenevis.org/forumdisplay.php?13-اخبار-و-اعلانات& (200)
Referer-page: http://barnamenevis.org/
Content received: 49879 bytes

Page requested: http://barnamenevis.org/forumdisplay.php?200-پادکست-ها-(Podcasts)& (200)
Referer-page: http://barnamenevis.org/
Content received: 33072 bytes

Page requested: http://barnamenevis.org/external.php?type=RSS2&forumids=200 (200)
Referer-page: http://barnamenevis.org/
Content received: 0 bytes

Page requested: http://barnamenevis.org/forumdisplay.php?46-اعلانات-سایت& (200)
Referer-page: http://barnamenevis.org/
Content received: 96782 bytes

Summary:
Links followed: 18
Documents received: 18
Bytes received: 1101392 bytes
Process runtime: 76.125616073608 sec

rezaonline.net
سه شنبه 19 دی 1391, 16:01 عصر
کلاس نمایش صفحه بندی با قابلیت مختصر سازی (ساخت وطن)
http://pagination4php.codeplex.com/

Tarragon
سه شنبه 19 دی 1391, 21:39 عصر
آقا رضا واقعا می گم دکمه تشکر کافی نیست کارتون خیلی عالیه!:تشویق::تشویق::تشویق:

mohsen_31369
سه شنبه 19 دی 1391, 21:46 عصر
PHPMailer (http://phpmailer.worxware.com/index.php?pg=tutorial)
اینم که کلاس معروفیه برای ارسال ایمیل
دانلود (https://github.com/Synchro/PHPMailer)

mohsen_31369
چهارشنبه 20 دی 1391, 12:35 عصر
JpGraoh (http://jpgraph.net/)
با این کلاس شما میتوانید هر نوع نمودار که بخواهید با php ترسیم نمایید. می توانید به سایتش مراجعه نمایید و انواع نمودارها رو ببینید.
البته این کلاس در سه نسخه عرضه میگردد که در نسخه ی رایگانش کارهایی مانند بارکد دوبعدی و ماتریکس رو نمی شود انجام داد.
دانلود (http://jpgraph.net/download/)

2undercover
چهارشنبه 20 دی 1391, 15:32 عصر
PHPMailer (http://phpmailer.worxware.com/index.php?pg=tutorial)
اینم که کلاس معروفیه برای ارسال ایمیل
دانلود (https://github.com/Synchro/PHPMailer)

خوب این کلاس خیلی حجیم هست (البته خودمم از همین کلاس استفاده می کنم!)! یعنی راه بهتری برای ارسال ایمیل نیست!

mohsen_31369
چهارشنبه 20 دی 1391, 16:27 عصر
خوب این کلاس خیلی حجیم هست (البته خودمم از همین کلاس استفاده می کنم!)! یعنی راه بهتری برای ارسال ایمیل نیست!
با سلام. این کلاس حجمش 86 کیلوبایته و این طور که توی جستوجوهام دریافتم در زمینه ی ایمیل بهترین کلاس پی اچ پی است.
با این وجود سه تا کلاس دیگه هم پیدا کردم که حجم ش پایین تره ولی تا حالا خودم ازشون استفاده نکردم

Simple PHP Email Class (http://sourceforge.net/projects/dave-mail-class/) حجمش حدود 8 کیلوبایته

XPertMailer (http://sourceforge.net/projects/xpertmailer/?source=dlp) کلاسی هست که فکر کنم حرفی برای گفتن داره

P3Mailer (http://sourceforge.net/projects/p3mailer/?source=recommended)

http://swiftmailer.org/
اینم یه کلاس نسبتا حجیمیه که امکانات زیادی داره. راهنمای پی دی اف (http://swiftmailer.org/pdf/Swiftmailer.pdf) هم داره (64 صفحه)

mohsen_31369
چهارشنبه 20 دی 1391, 20:42 عصر
GeSHi - Generic Syntax Highlighter (http://qbnz.com/highlighter/index.php)
این کلاسی هست که انجمن ساز phpBB (https://www.phpbb.com/) از اون برای هایلایت کردن کد های برنامه نویسی به زبانهای مختلف به کار می برد
کافی است در دموی سایت (http://qbnz.com/highlighter/demo.php) کدهاتون رو قرار بدهید و هایلایت رو کلیک کنید تا نتایج رو مشاهده نمایید.
دانلود کلاس (http://sourceforge.net/projects/geshi/files/)

mohsen_31369
پنج شنبه 21 دی 1391, 01:31 صبح
کلاس برای سبد خرید
تازگی یک فروشگاه طراحی کردم و توش برای خودم یک کلاس برای سبد خرید درس کردم
حالا یه کم تغییرش دادم تا بقیه ی دوستان هم بتونن ازش استفاده نمایند.
این سبد خرید با سسشن کار می کند. پس قبل از فراخوانی کلاس سسشن را استارت کنید.


session_start();
require ('Cart.php');


ابتدا آی دی محصول به همراه تعداد و فی قیمت را در کلاس ست کنید (می توانید تصویر آدرس تصویر بندانگشتی را نیز ست کنید تا در سبد خرید آن را نمایش دهید)


$cart->id=1;
$cart->num=10;
$cart->unit=1000;
$cart->thumb='img/thumb.jpeg';

و یا اینگونه عمل نمایید


$cart->set(array(
'id'=>1,
'num'=>4,
'unit'=>1200,
));

و پس از ست کردن مقادیر آنها را ذخیره نمایید


if($cart->save())
print_r($cart->getAll());
else
print_r ($cart->getErrors());

خوب اگر آی دی محصول قبلا در سبد خرید باشد، تعداد آن آپدیت می شود و اگر وجود نداشته باشد که به سبد اضافه می گردد

شما می توانید مبلغ کل سبد خرید را از پروپرتی total دریافت نمایید


echo 'total : '.$cart->total;


متد getAll تمامی محصولاتی که در سبد خرید هستند را برای شما بازمیگرداند. و یک آرگومنت اختیاری دریافت می کند.
اگر برابر json باشد داده ها به صورت json بازگردانده می شوند. مقدار پیش فرض برابر array است.


// json is returned
print_r($cart->getAll('json'));
// array is returned
print_r($cart->getAll());


نحوه ی آرایه برگشتی بدین شکل می باشد :


array(
'idProduct1'=>array
('num'=>numProduct1,'unit'=>unitProduct1,'thumb'=>thumbProduct1,'totalItem'=>totalItemProduct1,),
'idProduct2'=>array
('num'=>numProduct2,'unit'=>unitProduct2,'thumb'=>thumbProduct2,'totalItem'=>totalItemProduct2,),
...
)


متد getById :آی دی product رو می گیرد و آرایه ای از آنچه در سبد خرید مربوط به این محصول هست رو بر میگرداند (مثلا تعداد یا قیمت فی یا جمع کل این محصول)

متد destroy :آی دی product رو می گیرد و آن را از سبد پاک می نماید.


$cart->destroy($id);


متد destroyAll : برای پاک کردن محتویات کل سبد خرید


$cart->destroyAll();


متد getErorrs : اگر خطایی رخ ذخیره کردن رخ دهد با این متد خطای مربوطه را مشاهده نماییئ

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:32 صبح
کلاس کار با فایل اکسس :

<?php

class access{

private $file_path = NULL;
private $con = NULL;
function __construct() {


}
public function connect($file){
$db = realpath(dirname(__FILE__)).'\db1.mdb';
$this->file_path = $db;
$db_connection = new COM("ADODB.Connection", NULL, 1251);
$db_connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$this->file_path;
$db_connection->open($db_connstr);
$conn=odbc_connect($db_connstr,'','');
$this->con = $conn;
}
public function sql($table,$sql,$field_array)
{
$info =array();
$rs=odbc_exec($this->con,$sql);
$nbrow=0;
while( odbc_fetch_row( $rs ))
{

foreach ($field_array as $value) {

$info[$nbrow][$value] = iconv("windows-1256", "UTF-8", odbc_result($rs,$value));
}
$nbrow++;
}
print_r($info);
}
public function __destruct(){
odbc_close($this->con);
}
}

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:33 صبح
واسه پشتیبان گیری از دیتابیس و ذخیره در فایل یا ارسال ایمیل :

<?php

class db_backup {
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $db = 'daryamehr';
private $root = null;
private $root_script = null;
public $connect;

function __construct() {
$this->root = dirname(__FILE__);
$this->root_script = dirname(dirname(dirname(__FILE__)));
require_once $this->root.'/db_info.php';
$this->user = $user_info;
$this->host = $host_info;
$this->pass = $pass_info;
$this->db = $db_info;
try {
$this->connect = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db, $this->connect);
if (! $this->connect) {
throw new Exception('MySQL Connection Database Error: ' . mysql_error());
}
else {
return true;
}
}
catch (Exception $e) {
printf('ERROR: %s', $e->getMessage());
exit();
}
}
public function Restore($file)
{
}
public function backup($tables = '*')
{
$return = null;
$return.= 'DROP DATABASE IF EXISTS '.$this->db.';';
mysql_query("SET NAMES utf8");
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE DATABASE '.$this->db));
$return.= "\n\n".$row2[1].";\n\n";
//get all of the tables
if($tables == '*')
{
$tables = array();
mysql_query("SET NAMES utf8");
$result = mysql_query('SHOW TABLES',$this->connect);
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
mysql_query("SET NAMES utf8");
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
mysql_query("SET NAMES utf8");
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table,$this->connect));
$return.= "\n\n".$row2[1].";\n\n";

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}

//save file
$file_name = $this->db.'-db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
$handle = fopen($this->root_script.'/db_back/'.$file_name,'w+');
fwrite($handle,$return);
fclose($handle);


return $file_name;
}

}



?>

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:33 صبح
یه کلاس خوب واسه ارسال ایمیل :

<?php
class AttachmentEmail {
private $from = 'yours@email.com';
private $from_name = '';
private $reply_to = 'yours@email.com';
private $to = '';
private $subject = '';
private $message = '';
private $attachment = '';
private $attachment_filename = '';

public function __construct($from,$to, $subject, $message, $attachment = '', $attachment_filename = '') {
$this -> to = $to;
$from = explode(",", $from);
$this->from = $from[0];
$this->reply_to = $from[0];
$this -> subject = "New Email in F-ramezani.ir( ".$subject." )";
$this -> message = $message;
$this -> attachment = $attachment;
$this -> attachment_filename = $attachment_filename;
}

public function mail() {
if (!empty($this -> attachment)) {
$filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
$path = $this -> attachment;
$mailto = $this -> to;
$from_mail = $this -> from;
$from_name = $this -> from_name;
$replyto = $this -> reply_to;
$subject = $this -> subject;
$message = $this -> message;
$file = $path;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";

if (mail($mailto, $subject, "", $header)) {
return true;
} else {
return false;
}
} else {
$header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
$header .= "Reply-To: ".($this -> reply_to)."\r\n";

if (mail($this -> to, $this -> subject, $this -> message, $header)) {
return true;
} else {
return false;
}

}
}
}

?>

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:34 صبح
کلاس واسه کار با ftp :

<?php
class ftp{

public $con = NULL;
public function connect($server,$username,$password)
{
$id=ftp_connect($server) or die("not connected");
try {
$this->con = ftp_login($id,$username,$password);
}
catch (Exception $e) {
return false;
}

}
public function get_dir($dir)
{
if($this->con)
{
if($dir==""){
ftp_chdir($id,"httpdocs");
$dir=ftp_pwd($id);
}
$list_files=ftp_nlist($id,$dir);
foreach($list_files as $value){
if(strpos($value,".")!==false)
$type="<font color=red>File:</font>";
else
$type="<font color=green>Dir</font>";
if($value!="." and $value!="..")
echo $type." ".$value."";
}
}
else
{
return FALSE;
}
}

}

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:35 صبح
کار با فایل های ini برای تنظیمات

[Software]
Name= xxxxxxx
Description= xxxxxxxx
Version= 1.0.0
...

[Database]
Host= xxxxxx
Port= xxxx
...

[Website]
Title= xxxxxxx
Email= xxxxx
URL= xxxxx
Path= xxxxxx


<?php

$Ary_Tanzimat= (array)parse_ini_file('seting.ini');

print_r($Ary_Tanzimat);

?>

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:36 صبح
آیپی :

<?php
class ip{

public function get_ip()
{
$ip = null;
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;

}
}

?>

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:37 صبح
کلاس کار با تصاویر ریسایز و ...

<?php
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

var $image;
var $image_type;

function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
?>

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:37 صبح
تبدیل به excell

<?php

class toexcell {
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $db = 'teacher';
private $root = null;
public $connect;

function __construct() {
$this->root = dirname(__FILE__);
require_once $this->root.'/db_info.php';
$this->user = $user_info;
$this->host = $host_info;
$this->pass = $pass_info;
$this->db = $db_info;
try {
$this->connect = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db, $this->connect);
if (! $this->connect) {
throw new Exception('MySQL Connection Database Error: ' . mysql_error());
}
else {
return true;
}
}
catch (Exception $e) {
printf('ERROR: %s', $e->getMessage());
exit();
}
}
public function backup($table)
{
mysql_query("SET NAMES 'utf8'",$this->connect);
$query = "SELECT * FROM " . $table;
$result = mysql_query($query, $this->connect) or die("Could not complete database query"); //mysql_query -- Send a MySQL query
$num = mysql_num_rows($result); //mysql_num_rows -- Get number of rows in result
$num2=mysql_num_fields($result);
$query="SHOW COLUMNS FROM ".$table;
$result1=mysql_query($query,$this->connect);
//$fields=mysql_fetch_array($result1);
$header="";
for ($i = 0; $i < $num2; $i++) {
$header .= mysql_field_name($result, $i) . "\t";
}

if ($num != 0) {
//--------------------------------------------------------------------------
// XML Header Tag Goes Here
//--------------------------------------------------------------------------
$_xml ="<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\r\n";

$_xml.="<dataroot xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\r\n";
//--------------------------------------------------------------------------
// This while loop loops throught the data found from the above query and
// generates the XML Doc.
//--------------------------------------------------------------------------
while ($row = mysql_fetch_array($result)) { //mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both.
while ($row=mysql_fetch_array($result)){
$_xml .="\t<Table1>\r\n";
mysql_data_seek($result1,0);
$i=0;
while($f=mysql_fetch_array($result1)){
if($row[$i]<>'') $_xml.="\t\t<".$f[0].">".$row[$i]."</".$f[0].">\r\n";
$i++;
}
$_xml.="\t</Table1>\r";
}
}
$_xml.="</dataroot>";
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
header("Lacation: excel.htm?id=yes");
print "$_xml";
} else {
echo "No Records found";
}
}
}



?>

$ M 3 H R D A D $
پنج شنبه 21 دی 1391, 08:38 صبح
کاربران آنلاین :

<?php
/*
usersOnline.class.php
Author: Ilir Fekaj
Contact: tebrino@hotmail.com
Last updated: July 28, 2005
Version: 1.1
Latest version & info: http://www.sim-php.info
Support: http://forum.sim-php.info/ (if you find bugs or you need help with installation)
Demo: http://www.free-midi.org

This very simple class enables you to track number of visitors online in
an easy and accurate manner. It's free for all purposes, just please don't
claim you wrote it. If you have any problems, please feel free to contact me.
Also if you like this script please put link to http://www.sim-php.info. Thanks

Simply paste this code where you wish your users online count to appear:

include_once ("usersOnline.class.php");
$visitors_online = new usersOnline();

if (count($visitors_online->error) == 0) {

if ($visitors_online->count_users() == 1) {
echo "There is " . $visitors_online->count_users() . " visitor online";
}
else {
echo "There are " . $visitors_online->count_users() . " visitors online";
}
}
else {
echo "<b>Users online class errors:</b><br /><ul>\r\n";
for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
echo "<li>" . $visitors_online->error[$i] . "</li>\r\n";
}
echo "</ul>\r\n";

}


Important: You need to create database connection and select database before creating object! Example connection would look like this:

$host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost
$user = ""; // your MySQL username
$pass = ""; // your MySQL password
$db = ""; // the database to which you're trying to connect to

$conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database.");
mysql_select_db("$db", $conn);

--------------------------------------------
Table structure (paste this code in PHPMyAdmin or whatever program you use for db management):
CREATE TABLE `useronline` (
`id` int(10) NOT NULL auto_increment,
`ip` varchar(15) NOT NULL default '',
`timestamp` varchar(15) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `id`(`id`)
) TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1 ;

*/

class usersOnline {

var $timeout = 600;
var $count = 0;
var $error;
var $i = 0;
var $host = NULL;
var $user = NULL;
var $pass = NULL;
var $db = NULL;
var $root = NULL;

function usersOnline () {

$this->root = dirname(__FILE__);
require_once $this->root.'/db_info.php';
$this->user = $user_info;
$this->host = $host_info;
$this->pass = $pass_info;
$this->db = $db_info;
$this->timestamp = time();
$this->ip = $this->ipCheck();
$this->new_user();
$this->delete_user();
$this->count_users();
}

function ipCheck() {
/*
This function will try to find out if user is coming behind proxy server. Why is this important?
If you have high traffic web site, it might happen that you receive lot of traffic
from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
This function tryes to get real IP address.
Note that getenv() function doesn't work when PHP is running as ISAPI module
*/
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

function new_user() {
mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db);
$insert = mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
if (!$insert) {
$this->error[$this->i] = "Unable to record new visitor\r\n";
$this->i ++;
}
}

function delete_user() {
mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db);
$delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
if (!$delete) {
$this->error[$this->i] = "Unable to delete visitors";
$this->i ++;
}
}

function count_users() {
mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db);
if (count($this->error) == 0) {
$count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
return $count;
}
}

}

?>

mohsen_31369
پنج شنبه 21 دی 1391, 13:42 عصر
Compress css files using php (http://www.dscripts.net/2010/07/06/compress-css-files-using-php/)
اگه می خواین سرعت لود سایتتون رو افزایش دهید حتما از این کلاس استفاده نمایید.
این کلاس فایل های css شما را فشرده و فواصل اضافی آن را حذف می نماید و باعث کاهش شدید حجم فایل می گردد.
این کار در اکثر سایت های بزرگ مثل گوگل یاهو و ... انجام می شود.
تو مثالی که تو خود سایتش گذاشته شش فایل به حجم 30.8 کیلوبایت رو به 4.5 کیلوبایت تبدیل کرده یعنی 85.3% صرفه جویی در پهنای باند
دانلود (http://www.dscripts.net/wp-content/uploads/2011/04/css_compressor_dscripts.tar.gz)

mohsen_31369
پنج شنبه 21 دی 1391, 20:26 عصر
tcPdf (http://www.tcpdf.org/)
کلاسی خوب برای ایجاد فایل های پی دی اف
برخی از ویژگی های این کلاس :
پشتیبانی از ut8
پشتیبانی از زبان های rtl
پشتیبانی از jpeg , png , svg
دانلود (http://sourceforge.net/projects/tcpdf/files/)

mohsen_31369
جمعه 22 دی 1391, 13:12 عصر
pChart (http://pchart.sourceforge.net/index.php)
یه کلاس فوق العاده برای ترسیم هرجور نموداری که دوس داری
داکیومنت (http://pchart.sourceforge.net/documentation.php)خوبی هم داره
دانلود (http://pchart.sourceforge.net/download.php)

$ M 3 H R D A D $
جمعه 22 دی 1391, 13:44 عصر
واسه نمایش فرد با زیر مجموعه هاش هم اگه چیزی داری بزار
مثل نمایش گروه شرکت های هرمی

mohsen_31369
جمعه 22 دی 1391, 15:59 عصر
PHP Tree Graph Ex (http://sourceforge.net/projects/phptreegraphext/)
رسم نموادارهای درختی
http://sourceforge.net/projects/phptreegraphext/screenshots/293275
http://sourceforge.net/projects/phptreegraphext/screenshots/293259

mohsen_31369
جمعه 22 دی 1391, 16:25 عصر
برای رسم نمودار هم می توانید از api google قسمت chart نیز استفاده نمایید. البته باید با javaScript کار کنید.
شما می توانید انواع مختلف نمودار را با آن ترسیم نمایید.
خوبی این روش ( با جاوااسکریپت ) این است که شما می توانید رویدادهای مختلفی نیز بر روی نمودار تعریف کنید مثلا وقتی ماوس روی یک گره رفت چه اتفاقی بیافتد و ...
این هم یک نمونه (https://developers.google.com/chart/interactive/docs/gallery/orgchart)از نمودار که مربوط به تقسیمات زیرمجموعه ای است.

:اشتباه: آقا شرمنده وقتی این تایپیک رو زدم با آی پی غیر از ایران بودم و نمفهمیدم که الان ایران توی این قسمت تحریم شده

mohsen_31369
شنبه 23 دی 1391, 18:06 عصر
PhpMathPublisher (http://www.xm1math.net/phpmathpublisher/)
با این کلاس می توانید فرمول های ریاضی را با php بنویسید
doc (http://www.xm1math.net/phpmathpublisher/doc/help.html)

mohsen_31369
یک شنبه 24 دی 1391, 13:55 عصر
open flash chart (http://teethgrinder.co.uk/open-flash-chart/)

این هم یک کلاس خوب برای رسم نمودارهای زیبا به کمک فلش
شاید نمونه ی مثل این نمودارهای فلش رو تو صفحه ی آمار میهن بلاگ دیده باشید.
از لحاظ گرافیکی فوق العادس. یه نگاه بهش بندازین

SadeghPro19
یک شنبه 24 دی 1391, 14:23 عصر
سلام و خسته نباشید به دوستان
اگه دوستان کلاسی برای کار با فرم و ساخت فرم دارن معرفی کنن ممنون میشم.

mohsen_31369
یک شنبه 24 دی 1391, 14:43 عصر
php form builder class (http://www.imavex.com/pfbc3.x-php5/index.php#whats-new-in-3x)
و
php form builder (https://github.com/joshcanhelp/php-form-builder)

یه نگاه به اینا بنداز. من خودم باهاش کار نمی کنم.
من با فریم ورک یی کار میکنم خودش یه کلاس برای کار با فرم هارو داره که واقعا از همه لحاظ عالیه.

mohsen_31369
یک شنبه 24 دی 1391, 20:10 عصر
phpObjectForms (http://pof.sourceforge.net/)
کلاسی برای کارکردن با فرم ها.
تمام المنت های فرم ها را پوشش می دهد.
و دارای اعتبار سنجی سمت سرور بر اساس regular expressions
اعتبار ستجی با جاوااسکریپت (البته فقط برای textField)
نمونه ها (http://pof.sourceforge.net/samples/)

mohsen_31369
یک شنبه 24 دی 1391, 20:24 عصر
phpLinq (http://phplinq.codeplex.com/)
یه کلاس بسیار خوب برای کار با آرایه ها، xml,
می تونید مثل دیتابیس کوئری sql بزنید و نتایج داده ها را محدود نمایید.


// Create data source
$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric");

$result = from('$name')->in($names)
->where('$name => strlen($name) < 5')
->select('$name');

بدون استفاده از loop.
یا حتی موارد پیشرفته تر


$result = from('$employee')->in($employees)
->where('$employee => strlen($employee->Name) == 4')
->orderBy('$employee => $employee->Name')
->thenByDescending('$employee => $employee->Age')
->select('new {
"EmailAddress" => $employee->Email,
"Domain" => substr($employee->Email, strpos($employee->Email, "@") + 1)
}');


البته شبیه همین کلاس برای جاوااسکریپت هم می باشد
اینم آدرسش jsLinq (http://jslinq.codeplex.com/)

mohsen_31369
یک شنبه 24 دی 1391, 20:49 عصر
XML / SWF chart (http://www.maani.us/xml_charts/index.php?menu=Introduction)
برای ترسیم نمودار به کمک فلش xml و جاوااسکریپت
مقادیری که برای نمودار می خواهیم به صورت xml به فایل می دهیم.

! این یک کلاس پی اچ پی نیست.

mohsen_31369
یک شنبه 24 دی 1391, 20:55 عصر
phpWord (http://phpword.codeplex.com/)
برای کار با فایل های word با php!

mohsen_31369
یک شنبه 24 دی 1391, 20:56 عصر
phpPowerPoint (http://phppowerpoint.codeplex.com/)
کلاسی برای کار با فایل های powerPoint با php

mohsen_31369
یک شنبه 24 دی 1391, 21:03 عصر
phpCaptcha (http://www.phpcaptcha.org/)
کلاسی برای ایجاد کپچا
نمونه
http://www.phpcaptcha.org/securimage3/new/securimage_show.php?sid=263b5139dfa22248650b73f041 7857b8
نمونه با استفاده از ریاضیات
http://www.phpcaptcha.org/securimage3/new/securimage_show_math.php?sid=d77774ad3e0cdebeff6de 42436ede2bb