Thursday, 21 April 2011

Gửi mail trong php sử dụng xampp,wampp và code ví dụ

 HÀM MAIL() CỦA PHP
Send email trong PHP rất đơn giản, ta chỉ cần dùng duy nhất 1 hàm mail() được PHP cung cấp sẵn là đủ. Cú pháp của hàm mail() như sau:
bool mail(string to, string subject, string message[, string additional_headers])
Các tham số của hàm mail() có ý nghĩa như sau: 


56 TIPS/TRICKS tăng tốc độ hoạt động của PHP


1_ Nếu có thể khai báo hàm tĩnh thì có thể tăng tốc độ lên 4 lần.
2_ Echo thì nhanh hơn Print.
3_ Sử dụng nhiều câu lệnh Echo thay vì Echo 1 chuỗi nối vào nhau.
4_ Đặt giá trị lớn nhất cho vòng lặp for trước, và ko đặt trong vòng lặp for.
5_ Hủy các biến bằng hàm unset() để giải phóng bộ nhớ sau khi sử dụng, đặc biệt là những mảng lớn.
6_ Tránh dùng các định nghĩa kiểu: __get, __set, __autoload.
7_ Tránh dùng hàm require_once().
8_ Sử dụng đường dẫn đầy đủ trong includes và requires.
9_ Nếu cần tính thời gian lúc code được thực thi, thì nên dùng $_SERVER['REQUEST_TIME'] thay vì time().

Free PHP captcha - Chống tự động đăng nhập form





Xem thêm về captcha tại đây. Ở đây tôi tập trung hướng dẫn bạn cách ứng dụng free php captcha class vào website của bạn.
Download code và ví dụ mẫu

Căn bản Fix lỗi thường gặp trong PHP

- Vấn nạn đầu tiên trong việc thiết kế web đó là post or get bừa bãi tạo nên 1 hệ thống tạm gọi là tùm lum.
- Trong một số vidu hacking cho thấy có một kiểu hack điển hình đó là post form từ client lên dữ liệu của server , Ở đây tác giả thứ nhất không để ý đến session hoặc cookie lợi dụng kẽ hở này hacker có thể rất dễ dàng post 1 file thông qua 1 form html ở dưới máy của bạn hoặc ở 1 nơi nào đó để form với các tên của phần tử trong form trùng với tên của các phần tử web sẽ nhận
Ví dụ
http://www.milw0rm.com/exploits/6127


1. Chỉ sử dụng PHP khi bạn cần đến nó – Rasmus Lerdorf
      Không ai có thể sử dụng PHP thành thạo hơn chính người tạo ra nó. Vào năm 1995, Rasmus Lerdorf đã tạo ra ngôn ngữ lập trình PHP và từ đó ngôn ngữ này nhanh chóng được giới phát triển ứng dụng và làm thay đổi bộ mặt Internet. Tuy nhiên, Rasmus Lerdorf không tạo ra PHP với mục đích đó, PHP được tạo ra nhằm mục đích giải quyết các vấn đề của các nhà lập trình và phát triển ứng dụng web.

Friday, 8 April 2011

Tổng hợp bộ 100 ebook php link mediafire

Download mái thoải đi các bạn::
Link download 90 ebook php: http://www.mediafire.com/?54uxxp5fgfhyb

Link download 1800 scripts php: http://www.mediafire.com/?j1zfgg087r43m

Link download một số template và code source để tham khảo : http://www.mediafire.com/?k5u7ep24ld9md

Nguồn: direct 

Sunday, 3 April 2011

Phân biệt Include, require, include_one,require_once




ạn thường dùng include và require để chèn file từ ngoài vào dùng trong trang php nhưng bạn đang băn khoăn không biết giữa include và require khác và giống nhau nhu thế nào? khi nào thì nên dùng include, khi nào thì nên dùng require?

Phân biệt include – require

Include: nếu file chèn vào có lỗi thì nó cảnh báo lỗi và tiếp tục thực hiện file cho tới cuối cùng
Require: nếu có lỗi thì nó báo lỗi và dùng tại đó, ko thực hiện câu lệnh tiếp theo nữa

Khi sử dụng require() thì web server phải dịch lại tất cả nội dung bên trong nó. Ngược lại include() thì không.

require() <-- mang ý nghĩa của một câu lệnh khai báo thêm file.
Ví dụ bạn viết một chương trình. Có một file chứa tất cả các hàm dùng chung hết cả site thì dùng require. include() .. cái này nên sử dụng nếu bạn xét qua một điều kiện, hay vòng lặp, hay gì gì đó rùi mới quyết định file thêm vào ...
ví dụ 1. PHP Code:

if ($a == $b)
{
include 'lib_1.php';
} else {
include 'lib_2.php';
}

Thật ra thì chẳng có gì khác nhau hết, bạn ko nhất thiết phải dùng như trên (nhưng tốt nhất nên dùng thế).
Sự khác nhau chủ yếu là khi có lỗi xảy ra. require() báo lỗi fatal, và ngừng tất cả lại.
Còn include() chỉ warning và sau đó chạy tiếp.
Còn include_once và require_once. nhìn là biết. Nó chỉ thêm file vào một lần duy nhất.

ví dụ 2. calling.php

PHP Code:

1. <?php
2. include_once 'included.php';
3. include_once 'included.php';
4. ?>

Khi gọi calling.php . sẽ cho ra

1. mr47 loves PHP.

Còn nếu trong calling.php dùng include() sẽ cho ra

1. mr47 loves PHP.
2. mr47 loves PHP.

Khi thêm các file Lib hoặc Class vào chương trình, include_once và require_once được khuyến khích dùng.

Nguồn:leech

Ajax là cái gì ? Đơn giản mà chẳng hiểu gì !

Ajax là cái gì ? Đơn giản mà chẳng hiểu gì !

Đơn giản:

Nó chỉ là một công cụ hỗ trợ cho người lập trình, được đưa vào ứng dụng kết hợp với ngôn ngữ lập trình, để cho ứng dụng gần với thực tế.

Ngôn ngữ lập trình bình thường sẽ là " tự nó POST- GET", cho thêm thằng Ajax để nó làm việc này cho mình nhàn hơn.
....còn muồn đọc thêm thì search google !
Đây chỉ là ví dụ về ajax sưu tập được ngoài ra các bạn có thể vào :


Hãy phân biệt Ajax trong Asp.net( được xây dựng sẵn) , Ajax với Javascript, Ajax thuần Ajax

Ajax với PHP

Code này các bạn xài tạm nha rất căn bản + rất tốt đó đừng coi thường nó cơ bản nha
Code đã được chôm chỉa tại 1 trang wed nước ngoài trong những ngày cơ cực mình tìm hiểu về PHP để làm Forum cho lớp tiếc là do lâu ko đóng tiền forum này đã sập tiệm

đầu tiên là file j-s+html để gọi ajax:

<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var sex = document.getElementById('sex').value;
var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option>m</option>
<option>f</option>
</select>
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
</body>
</html>

còn đây là file Ajax code bằng PHP

<?php
$dbhost = "localhost";
$dbuser = "dbusername";
$dbpass = "dbpassword";
$dbname = "dbname";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
//build query
$query = "SELECT * FROM ajax_example WHERE ae_sex = '$sex'";
if(is_numeric($age))
$query .= " AND ae_age <= $age";
if(is_numeric($wpm))
$query .= " AND ae_wpm <= $wpm";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[ae_name]</td>";
$display_string .= "<td>$row[ae_age]</td>";
$display_string .= "<td>$row[ae_sex]</td>";
$display_string .= "<td>$row[ae_wpm]</td>";
$display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>

Nguồn: leech

Saturday, 2 April 2011

Phân biệt mysql_fetch_assoc,mysql_fetch_array,mysql_fetch_object



Lấy dữ liệu từ DB ra tưởng như đơn giản nhưng lựa chọn cách lấy nào ? lấy ra sao ? nó sẽ hiển thị thế nào?
Đây là câu trả lời:

Các lệnh mysql_fetch_assoc,mysql_fetch_array,mysql_fetch_object đều dùng để fetch dữ liệu từ câu query, tuy nhiên dữ liệu trả về sẽ có dạng khác nhau ứng với mỗi câu lệnh.
Ví dụ:
• $sql = mysql_query("SELECT * FROM table WHERE id=id");



+ mysql_fetch_assoc():
• $rs = mysql_fetch_assoc($sql);

Dữ liệu trả về sẽ có dạng:
Array(
'tên_field1'=>giá trị 1,
'tên_field2'=>giá trị 2,
'tên_field3'=>giá trị 3,
);

Và 1 mảng như vậy gọi là associative array
Hiển thị dữ liệu:
• echo $rs['tên_field1'],$rs['tên_field2']
+ mysql_fetch_row():
PHP CodeCopy To Clipboard
• $rs = mysql_fetch_row($sql);

Dữ liệu trả về sẽ có dạng:
Array(
Mã PHP: (SELECT ALL)
0=>giá trị 1,
1=>giá trị 2,
2=>giá trị 3,
);

Và 1 mảng như vậy gọi là enumerated array
Hiển thị dữ liệu
echo $rs[0],$rs[1]
+ mysql_fetch_array():
$rs = mysql_fetch_array($sql,mode_fetch);
trong đó mode_fetch có các giá trị:
+ MYSQL_ASSOC: trả về associative array(giống mysql_fetch_assoc())
+ MYSQL_NUM: trả về enumerated array(giống mysql_fetch_row())
+MYSQL_BOTH : (mặc định)
Dữ liệu trả về sẽ có dạng:
Array(
'tên_field1'=>giá trị 1,
0=>giá trị 1,
'tên_field2'=>giá trị 2,
1=>giá trị 2,
'tên_field3'=>giá trị 3,
2=>giá trị 3,
);

Hiển thị dữ liệu:
• echo $rs['tên_field1'],$rs['tên_field2']; //hoặc
• echo $rs[0],$rs[1]; // 0,1 là thự tự của các field trong table

+ mysql_fetch_object():
• $rs = mysql_fetch_object($sql);

Dữ liệu trả về sẽ có dạng:
Object (
'tên_field1'=>giá trị 1,
'tên_field2'=>giá trị 2,
'tên_field3'=>giá trị 3,
);

Hiển thị dữ liệu:
• echo $rs->tên_fiel1,$rs->tên_fiel2,..

Còn đây là một số hàm của Mysql_ trong PHP
PHP MySQL Functions

Còn đây là một số hàm của Mysql_ trong PHP .

Đừng nản lòng khi nhìn những hàm này. Bạn sẽ chẳng bao giờ dùng hết chúng !

Function Description PHP
mysql_affected_rows() Returns the number of affected rows in the previous MySQL operation 3
mysql_change_user() Deprecated. Changes the user of the current MySQL connection 3
mysql_client_encoding() Returns the name of the character set for the current connection 4
mysql_close() Closes a non-persistent MySQL connection 3
mysql_connect() Opens a non-persistent MySQL connection 3
mysql_create_db() Deprecated. Creates a new MySQL database. Use mysql_query() instead 3
mysql_data_seek() Moves the record pointer 3
mysql_db_name() Returns a database name from a call to mysql_list_dbs() 3
mysql_db_query() Deprecated. Sends a MySQL query. Use mysql_select_db() and mysql_query() instead 3
mysql_drop_db() Deprecated. Deletes a MySQL database. Use mysql_query() instead 3
mysql_errno() Returns the error number of the last MySQL operation 3
mysql_error() Returns the error description of the last MySQL operation 3
mysql_escape_string() Deprecated. Escapes a string for use in a mysql_query. Use mysql_real_escape_string() instead 4
mysql_fetch_array() Returns a row from a recordset as an associative array and/or a numeric array 3
mysql_fetch_assoc() Returns a row from a recordset as an associative array 4
mysql_fetch_field() Returns column info from a recordset as an object 3
mysql_fetch_lengths() Returns the length of the contents of each field in a result row 3
mysql_fetch_object() Returns a row from a recordset as an object 3
mysql_fetch_row() Returns a row from a recordset as a numeric array 3
mysql_field_flags() Returns the flags associated with a field in a recordset 3
mysql_field_len() Returns the maximum length of a field in a recordset 3
mysql_field_name() Returns the name of a field in a recordset 3
mysql_field_seek() Moves the result pointer to a specified field 3
mysql_field_table() Returns the name of the table the specified field is in 3
mysql_field_type() Returns the type of a field in a recordset 3
mysql_free_result() Free result memory 3
mysql_get_client_info() Returns MySQL client info 4
mysql_get_host_info() Returns MySQL host info 4
mysql_get_proto_info() Returns MySQL protocol info 4
mysql_get_server_info() Returns MySQL server info 4
mysql_info() Returns information about the last query 4
mysql_insert_id() Returns the AUTO_INCREMENT ID generated from the previous INSERT operation 3
mysql_list_dbs() Lists available databases on a MySQL server 3
mysql_list_fields() Deprecated. Lists MySQL table fields. Use mysql_query() instead 3
mysql_list_processes() Lists MySQL processes 4
mysql_list_tables() Deprecated. Lists tables in a MySQL database. Use mysql_query() instead 3
mysql_num_fields() Returns the number of fields in a recordset 3
mysql_num_rows() Returns the number of rows in a recordset 3
mysql_pconnect() Opens a persistent MySQL connection 3
mysql_ping() Pings a server connection or reconnects if there is no connection 4
mysql_query() Executes a query on a MySQL database 3
mysql_real_escape_string() Escapes a string for use in SQL statements 4
mysql_result() Returns the value of a field in a recordset 3
mysql_select_db() Sets the active MySQL database 3
mysql_stat() Returns the current system status of the MySQL server 4
mysql_tablename() Deprecated. Returns the table name of field. Use mysql_query() instead 3
mysql_thread_id() Returns the current thread ID 4
mysql_unbuffered_query() Executes a query on a MySQL database (without fetching / buffering the result) 4

Nguồn : laptrinhcoban.blogspot.com