学习php总结(一)

学习接触PHP也快一年了。

平时也没太注意:把一些经常用到的结总一下,强当回忆

1。php连接mysql数据库 

$db_link=mysql_connect("localhost","root","passwd") or die("connect error:". mysql_error());

mysql_select_db("databasename",$db_link);

$sql="select * from table ";

$result=mysql_query($sql,$db_link);

$rows=mysql_fetch_array($result);//获取记录

$row=mysql_num_rows($result);//得到总行数。

$id=$rows["id"];

2。php中session的使用

在每个页面开始之前用

session_start();

$_SESSION["ID"]="userid";

3。php中cookie的使用

setcookie("TestCookie", $value, time()+3600, "/", ".example.com");

设置了一个TestCookie的值为$value的一个小时后过期的cookie变量

清空cookie

setcookie("TestCookie", "", time()-2*3600, "/", ".example.com");

4。php取页面地址

$_SERVER['HTTP_REFERER'] //可以得到上一页的地址

$_SERVER[PHP_SELF] //得到当前页面地址

$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"] //这个可以得到带参数的地址

5。php时间

date函数和mktime可以生成相应的日期:

$today=date("Y-m-d"); //2007-03-15(格式可以自由定义)

$date=date("Ym");

$year=substr($date,0,4);

$month=substr($date,4,2);

$next_month=date("Y-m-d",mktime(0,0,0,$month+1,1,$year));

$last_month=date("Y-m-d",mktime(0,0,0,$month-1,1,$year));

6。php不显示错误信息

error_reporting(0);

?>

7。php删除确认

删除

8。javascript中写php

9.PHP上传文件







$uploaddir = '/upload/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded./n";
} else {
    echo "Possible file upload attack!/n";
}
?>

10。PHP显示客户IP,操作系统和浏览器

$os=$_SERVER["HTTP_USER_AGENT"]; // 浏览者操作系统及浏览器
//echo $os;
//分析浏览器
if(strpos($os,"NetCaptor")) $explorer="NetCaptor";
elseif(strpos($os,"Opera")) $explorer="Opera";
elseif(strpos($os,"Firefox")) $explorer="Firefox";
elseif(strpos($os,"MSIE 6")) $explorer="MSIE 6.x";
elseif(strpos($os,"MSIE 5")) $explorer="MSIE 5.x";
elseif(strpos($os,"MSIE 4")) $explorer="MSIE 4.x";
elseif(strpos($os,"Netscape")) $explorer="Netscape";
else $explorer="Other";

// 分析操作系统
if(strpos($os,"Windows NT 5.0")) $os="Windows 2000";
elseif(strpos($os,"Windows NT 5.1")) $os="Windows XP";
elseif(strpos($os,"Windows NT 5.2")) $os="Windows 2003";
elseif(strpos($os,"Windows NT")) $os="Windows NT";
elseif(strpos($os,"Windows 9")) $os="Windows 98";
elseif(strpos($os,"unix")) $os="Unix";
elseif(strpos($os,"linux")) $os="Linux";
elseif(strpos($os,"SunOS")) $os="SunOS";
elseif(strpos($os,"BSD")) $os="FreeBSD";
elseif(strpos($os,"Mac")) $os="Mac";
else $os="Other";
//ip
unset($ip);
if($_SERVER['HTTP_CLIENT_IP']){
$ip=$_SERVER['HTTP_CLIENT_IP'];
} else if($_SERVER['HTTP_X_FORWARDED_FOR']){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else{
$ip=$_SERVER['REMOTE_ADDR'];
}
//显示浏览者信息
echo "IP地址:".$ip."
";
echo "操作系统:".$os."
";
echo "浏览器:".$explorer."
";
?>
11。PHP导出excel
 
header("Content-Type: application/vnd.ms-excel");

header("Content-Disposition: attachment;filename=downdata.xls");

header("Pragma: no-cache");

header("Expires: 0");

echo "aa/t/n";
 
echo "bb/t/n";
 
12。PHP指定服务器内存:ini_set();
 
ini_set("upload_max_filesize","20M");
ini_set("post_max_size","32M");
ini_set("memory_limit","32M");
13。PHP页面跳转
header("Location:www.baidu.com");
 

你可能感兴趣的:(php)