1.nginx ?
nginx tcp/ip http
fastcgi
2. echo 'hello tusheng' ; ?> , , ( : )
short_open_tag = Off,,php.ini : short_open_tag = On
3. , , ?
$tmp = 0 == "a"? 1: 2;
echo $tmp;
?>
1 int string ,0==="a"
0 == 0 true
PHP 。。
$tmp = 0 === "a"? 1: 2;
echo $tmp; 2
4. : $str = "1109063 milo 1";
1109063 $uid, milo $user, 1 $type
list($uid, $user, $type) = explode(" ", $str);
\t
list($uid, $user, $type) = explode("\t", $str);
list($uid, $user, $type) = sscanf($str, "%d %s %d");
$n = sscanf($auth, "%d\t%s %s", $id, $first, $last);
5. TINYINT SMALLINT MEDIUMINT INT
TINYINT-2^7 - 2^7-10 ~ 2^8-1
SMALLINT-2^15 - 2^15-1 0 ~ 2^16-1
MEDIUMINT-2^23 - 2^23-1 0 ~ 2^24-1
INT-2^31 - 2^31-1 0 ~ 2^32-1
6. i am milo! day day up!
$arr = array(
'I', 'AM', 'MILO!', 'DAY', 'DAY', 'UP!'
);
?>
$str = strtolower(implode(" ",$arr));
7. count
function get_list($cnd = array(), &$count = false)
{
// $cnd datas
$datas = 'i am call back';
$count && $count = rand(1, 10000);
return $datas;
}
?>
$count=1;
$data = get_list($cnd,&$count);
echo $count;
8. session ,
mysql、memcache、cookie
9. HTTP ,
200, 301, 404, 502, 503
200
, 。
301
, URI 。 , 。 , 。 URI Location 。 HEAD , URI 。 GET HEAD , , , 。 : HTTP/1.0 , POST 301 , GET 。
404
, 。 。 , 410 , , 。404 。
502
, 。
503
, 。 , 。 , Retry-After 。 Retry-After , 500 。 :503 。 。
200 OK , GET POST 。
301 Moved Permanently , URL Location , URL
404 Not Found 。 。
502 Bad Gateway , , 。
503 Service Unavailable 。 ,Servlet 503。 503 Retry-After 。
10. , mysql user
host: 192.168.0.254
port: 3306
user: one
pass: piece
database: db_user
table: user
$link = mysql_connect("192.168.0.254:3306","one","piece") or die('Could not connect: '.mysql_error());
mysql_select_db('db_user',$link);
$query = mysql_query("select * from user limit 10");
while($rs = mysql_fetch_array($query,MYSQL_ASSOC))
{}
11. autoload($class) Lib
$request->action = lcfirst(implode(array_map(
'ucfirst',
explode('-', strtolower($request->action))
)));
------------------------------------------------------------
function __autoload($class)
{
$cls = strtolower(str_replace("_","/",$class));
if(file_exsits(LIB.$cls.'.php'))
{
include_once(LIB.$cls.'.php');
}
else
{
die("not found {$class} class");
}
}
defined("LIB",'/data/wwwroot/www.xx.com/lib/');
$author = new Lib_Author();
-----------------------------------------------------------
function __authload($class)
{
$cls = explode("_",$class);
if(@is_dir($cls[1]))
{
if(@is_file($cls[2]))
{
include_once("CON_PATH".$cls[1].'/'.$cls[2].".php");
}
else
{
dir('error');
}
}
else if(@is_file($cls[1].".php"))
{
include_once("CON_PATH".$cls[1].".php");
}
else
{
dir('error');
}
}
---------------------------------------
function __autoload($class)
{
$cls = explode("_",$class);
$file = get_file($cls);
if($file=='error')
{
die('error');
}
include_once($file);
}
function get_file($dir)
{
if(is_array($dir))
{
foreach($dir as $k=>$v)
{
$tmpdir .= $v.'/';
if(is_dir('CON_PATH'.$tmpdir))
{
continue();
}
else if(is_file('CON_PATH'.$tmpdir.".php"))
{
return 'CON_PATH'.$tmpdir.".php";
}
else
{
return 'error';
}
}
return 'error';
}
return 'error';
}
defined("CON_PATH","/data/wwwroot/www.xx.com/app/cntroller/");
$sb = new controller_sb();
------------------------------------
function __autoload_my_classes($classname)
{
# ... your logic to include classes here
}
spl_autoload_register('__autoload_my_classes');
-----------------------------------------------------------
12. set_error_handle ,
set_error_handle(callback,level)
function callback(int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] ){
}
function dealErrorHandler($errno,$errstr,$errfile,$errline)
{
switch($errno){
case E_USER_ERROR:
echo "error [$errno] $errstr fatal error on line $errline in file $errfile";
break;
case E_USER_WARNING:
echo "my warning [$errno] $errstr":
break;
case E_USER_NOTICE:
echo "my notice[$errno] $errstr";
break;
default:
echo "unkonwn error type :[$errno] $errstr";
break;
}
}
set_erro_handler(dealErrorHandler);
trigger_error("notice", E_USER_NOTICE);
trigger_error("warning", E_USER_WARNING);
trigger_error("error", E_USER_ERROR);
13. php notice
, php.ini error_reporting
set_error_handler @
1. :error_reporting (E_ALL & ~E_NOTICE);
2. php.ini :error_reporting = E_ALL
:error_reporting = E_ALL & ~E_NOTICE
3.error_reporting(0); php.inidisplay_errors=Off
14. instanceof ,
,
15. 1023 ,
10-2
1023%2=1
511%2 =1
255%2 =1
127%2 =1
63%2 =1
31%2 =1
15%2 =1
7%2 =1
3%2 =1
1%2 =1
0 =0
-------------------------------------------
1023
2^9=<N<2^10
511
k=9
10 9 8 7 6 5 4 3 2 1
1 1 1 1 1 1 1 1 1 1
----------------------
1023 1
1023-1/2=511 1
511-1/2=255 1
255-1/2=127 1
127-1/2=63 1
63-1/2=31 1
31-1/2=15 1
15-1/2=7 1
7-1/2=3 1
3-1/2=1 1
-----------------------------------------------
2-10
, , , , n (n-1) ,
:110011=1*2^0+1*2^1+0*2^2+0*2^3+1*2^4+1*2^5=51
An*2^(n-1) An n ,
n An*2^(n-1)
16. php ? ?
$str = "aa\tbb\tcc";
@list($a, $b, $c) = explode('\t', $str);
echo $a,$b,$c;
?>
aabbcc;//'\t' \t ,explode array(0=>"aa\tbb\tcc") 。。。,'\t' "\t"
17. include require
include ,require
18. , ( 5 50 ),
: php
: func_num_args() func_get_arg() unc_get_args()
function param()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs<br />
";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "<br />
";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "<br />
";
}
}
param(1,2,3,4,5);
/**
2 * , , call_user_func_array(),
3 * , test ,
4 */
5 function otest1 ($a)
6 {
7 echo( ' ' );
8 }
9
10 function otest2 ( $a,$b)
11 {
12 echo( ' ' );
13 }
14
15 function otest3 ( $a,$b,$c)
16 {
17 echo( ' ' );
18 }
19
20 function otest ()
21 {
22 $args=func_get_args();
23 $num=func_num_args();
24 call_user_func_array( 'otest'.$num,$args );
25 }
26
27 otest(1,2);
19. ( return ) , , (global &)
$var=1;
function get_pra()
{
global $var;
$var = 'xxx';
echo $var;
}
echo $var.'--';
get_pra();
echo $var;
----------------------------------
$test = 1;
$test1 = 2;
function get_yinyong()
{
global $test1;
$GLOBALS["test"] = &$test1;
}
echo $test."
";
get_yinyong();
echo $test;
----------------------------
20. user 10 , , sql order by
user uid, username
user , php ,sql id limit ( 10 ), , shuffle ,array_rand 10
21. sql uid , uid , uid in
select uid from user where uid in(10, 1, 3, 8, 11, 4, 7);
1,3,4,7,8,10,11 , id , uid in id
22. PHP **
preg_replace('/[a-zA-Z]*/','**',$str);
str_replace('ooxx','**',$str);
23. 2.php ? ? 1.php->2.php
cookie,cookie time()+3600
24. php json , json
25. mysql sql ' / , sql
mysql_real_escape_string
26. php header
header('');
27. , 2.php
1.php
setcookie('test', 'cookie_test', 3600);
?>
2.php
$cookie = isset($_COOKIE['test'])? $_COOKIE['test']: 'cookie';
echo $cookie;
?>
i am here
1
a. include include_once , 。
b. include include_once , , include, include_once, 。
28. call_user_func
, 。 call_user_func_array
29. nginx server_name www.120.net xxx.120.net
http://www.120.net/index.php http://xxx.120.net/index.php
$_SERVER["SERVER_NAME"] $_SERVER["REQUEST_URI"]
www.120.net xxx.120.net
/index.php /index.php
30. linux drwxr-xr-x
755 u g 、 o
31. 1Mbps KBps,
1*1024/8
1M=1024KB
1KB=1024B
1B=8bit
1. + abstract class Example{ // The parameterized factory method public static function factory($type) { if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}}// Load a MySQL Driver$mysql = Example::factory('MySQL');
// Load a SQLite Driver
$sqlite = Example::factory('SQLite');
definded('DRIVER','/data/wwwroot/www.want.com/core/driver/');abstract class Example(){ private function __construct() { } public static function factory($type) { if(include_once(DRIVER.$type.'.php')) { return ExampleSon::singleton($type); } else { throw new Exception("Driver is not found!"); } } }class ExampleSon implements Example{ // Hold an instance of the class private static $instance; // // A private constructor; prevents direct creation of object private function __construct() { echo 'I am constructed'; } // The singleton method public static function singleton() { if (!isset(self::$instance)) { // , $c = __CLASS__; // self::$instance = new $c } return self::$instance; } // Example method public function bark() { echo 'Woof!'; } // Prevent users to clone the instance public function __clone() // { trigger_error('Clone is not allowed.', E_USER_ERROR);
}} :
1
2 __CLASS__
3
4 __clone()
---- : , --------
2. , ? ?
1。__construct()
,
__construct ,__construct , 。
2。__destruct()
。
3。__call()
,
, ;
, __call 。
4。__get()
,
, ;
, __get 。
5。__set()
,
, ;
, __set 。
6。__toString()
。 echo $obj; print $obj;
7。__clone()
。 :$t=new Test();$t1=clone $t;
8。__sleep()
serialize 。 , , 。
9。__wakeup()
unserialize , 。
10。__isset()
。 :isset($c->name)。
11。__unset()
unset 。 :unset($c->name)。
12。__set_state()
var_export , 。 __set_state var_export 。
13。__autoload()
, , 。
1。__LINE__
。
2。__FILE__
。 , 。 PHP 4.0.2 ,__FILE__ , 。
3。__FUNCTION__
(PHP 4.3.0 )。 PHP 5 ( )。 PHP 4 。
4。__CLASS__
(PHP 4.3.0 )。 PHP 5 ( )。 PHP 4 。
5。__METHOD__
(PHP 5.0.0 )。 ( )。
3.
4.
threads
posts
threads tid
posts pid, tid
tid threads posts
posts 1 , threads 2000 , 5
, posts threads mysql
5. mysql / , php mysql php ? , model , slave , master : , , :
6. UCenter
, , , 。
, ajax (javascript src ) 。
p3p , , (ucenter cookie)
ajax , 10 , 。
7. linux http://www.120.net/test-1.0.0.tar.gz
a. /usr/local/src
b. /usr/local/test
c. mysql , /usr/local/mysql
wget - c http://www.120.net/test-1.0.0.tar.gz/usr/local/srctar zxvf /usr/local/src/test-1.0.0.tar.gzcd /usr/local/src/test-1.0.0./configure --prefix=/usr/local/test --exec--prefix=/usr/local/mysqlmake testmake install
8. php memcache ( )
a. mysql , memcache
b. mysql , mysql , memcache
public function get_cache($key) { if($this->memcahe) { $var = $this->memcahe->get($this->pre.$key); $valid = $this->memcahe->get($this->pre.$key.'_valid'); if($var && !$valid) { $lock = $this->memcahe->get($this->pre.$key.'_lock'); if(!$lock) { $this->memcahe->set($this->pre.$key.'_lock', true, 0, 60); return false; } } return $var; } return false; }
public function set_cache($key, $var = null, $expire = 0) { if($this->memcahe) { $expire = (int)$expire; $expire = ($expire ? $expire : $this->expire); $this->memcahe->set($this->pre.$key, $var, 0, $expire+300); $this->memcahe->set($this->pre.$key.'_lock', false, 0, $expire); $this->memcahe->set($this->pre.$key.'_valid', true, 0, $expire); return true; } return false; }
9. ,
, , , , ,
?
, 。 , 。 , 。 , 。 。
10. arrayaccess
ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( string $offset )
abstract public mixed offsetGet ( string $offset )
abstract public void offsetSet ( string $offset , string $value )
abstract public void offsetUnset ( string $offset )
}
class Single implements ArrayAccess{ private $name; private static $_Instance = null; private function __construct() { } static function load() { if(null == self::$_Instance) { self::$_Instance = new Single(); } return self::$_Instance; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } /** * * offsetExists(), * offsetGet(), * offsetSet(), * offsetUnset(), **/ public function offsetSet($offset, $value) { if (is_null($offset)) { $this->container[] = $value; } else { $this->container[$offset] = $value; } } public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; } public function offsetExists($offset) { return isset($this->container[$offset]); } public function offsetUnset($offset) { unset($this->container[$offset]); }}$s = Single::load();$s->setName("jack");$s["name"] = "mike";echo $s->getName(); //jackecho $s["name"]; //mike
11. coreseek /usr/local/coreseek
/usr/local/coreseek/etc/test.conf
post
a.
b.
c. ( )
indexer -c /usr/local/coreseek/etc/test.conf --allsearchd -c /usr/local/coreseek/etc/test.conf indexer -c /usr/local/coreseek/etc/test.conf --all --rotate12. posts sphinx ,
“ + ” , , , id, id , 。
13. php :
$i = 97;$a = ($i++) + (++$i) + $i ;$b = (--$i) + ($i--) + $i + 6;
echo "$i, $a, $b";
97, 295, 299
97
97+99+99
98+98+97+6
14. , IP: if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $onlineip = getenv('HTTP_CLIENT_IP');} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $onlineip = getenv('HTTP_X_FORWARDED_FOR');} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $onlineip = getenv('REMOTE_ADDR');} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $onlineip = $_SERVER['REMOTE_ADDR'];} HTTP_ header , , PHP HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR ?
15. google,baidu , ( PC ) URL , , ? , 。
user_agent ,
wap accept
16. php.ini magic_quotes_gpc magic_quotes_runtime ?onoff17. PHP http file_get_contents, , PHP , , ,PHP ?
file_get_contents $ctx = stream_context_create(array( 'http' => array( 'timeout' => 1
)
)
);
file_get_contents("http://www.want.com/", 0, $ctx);
curl http , curl_setopt($s,CURLOPT_TIMEOUT,$timeout);
18. , DNS ?19. mysql set names * ?(ACE) A、Character_set_client B、Character_set_system C、Character_set_results D、Character_set_server E、Character_set_connection F、Character_set_database20. ?(A) A、utf8_general_ci B、utf8_general_cs C、utf8_general_bin21. XSS ?
strip_tags , , ascii 23. CSRF ?