最大的網(wǎng)站源碼資源下載站,
[回顧]:上集介紹了"調(diào)試程序","如何使用session","規(guī)范sql語(yǔ)句"等15個(gè)問(wèn)題(php高手帶路--問(wèn)題匯總解答[1])。本集繼續(xù)作出16條常見(jiàn)問(wèn)題的解答。
16:我想修改mysql的用戶(hù),密碼
首先要聲明一點(diǎn),大部分情況下,修改mysql是需要有mysql里的root權(quán)限的,
所以一般用戶(hù)無(wú)法更改密碼,除非請(qǐng)求管理員.
方法一
使用phpmyadmin,這是最簡(jiǎn)單的了,修改mysql庫(kù)的user表,
不過(guò)別忘了使用password函數(shù)。
方法二
使用mysqladmin,這是前面聲明的一個(gè)特例。
mysqladmin -u root -p password mypasswd
輸入這個(gè)命令后,需要輸入root的原密碼,然后root的密碼將改為mypasswd。
把命令里的root改為你的用戶(hù)名,你就可以改你自己的密碼了。
當(dāng)然如果你的mysqladmin連接不上mysql server,或者你沒(méi)有辦法執(zhí)行mysqladmin,
那么這種方法就是無(wú)效的。
而且mysqladmin無(wú)法把密碼清空。
下面的方法都在mysql提示符下使用,且必須有mysql的root權(quán)限:
方法三
mysql> insert into mysql.user (host,user,password)
values('%','jeffrey',password('biscuit'));
mysql> flush privileges
確切地說(shuō)這是在增加一個(gè)用戶(hù),用戶(hù)名為jeffrey,密碼為biscuit。
在《mysql中文參考手冊(cè)》里有這個(gè)例子,所以我也就寫(xiě)出來(lái)了。
注意要使用password函數(shù),然后還要使用flush privileges。
方法四
和方法三一樣,只是使用了replace語(yǔ)句
mysql> replace into mysql.user (host,user,password)
values('%','jeffrey',password('biscuit'));
mysql> flush privileges
方法五
使用set password語(yǔ)句,
mysql> set password for [email protected]"%" = password('biscuit');
你也必須使用password()函數(shù),
但是不需要使用flush privileges。
方法六
使用grant ... identified by語(yǔ)句
mysql> grant usage on *.* to [email protected]"%" identified by 'biscuit';
這里password()函數(shù)是不必要的,也不需要使用flush privileges。
注意: password() [不是]以在unix口令加密的同樣方法施行口令加密。
17:我想知道他是通過(guò)哪個(gè)網(wǎng)站連接到本頁(yè)
php代碼:
<?php
//必須通過(guò)超級(jí)連接進(jìn)入才有輸出
echo $_server['http_referer'];
?>
18:數(shù)據(jù)放入數(shù)據(jù)庫(kù)和取出來(lái)顯示在頁(yè)面需要注意什么
入庫(kù)時(shí)
$str=addslashes($str);
$sql="insert into `tab` (`content`) values('$str')";
出庫(kù)時(shí)
$str=stripslashes($str);
顯示時(shí)
$str=htmlspecialchars(nl2br($str)) ;
<?php
//$content來(lái)自數(shù)據(jù)庫(kù)
$content=nl2br(htmlspecialchars($content));
$content=str_replace(" "," ",$content);
$content=str_replace("/n","<br>/n",$content);
?>
19:如何讀取當(dāng)前地址欄信息
php代碼:
<?php
$s="http://{$_server['http_host']}:{$_server["server_port"]}{$_server['script_name']}";
$se='';
foreach ($_get as $key => $value) {
$se.=$key."=".$value."&";
}
$se=preg_replace("/(.*)&$/","$1",$se);
$se?$se="?".$se:"";
echo $s."?$se";
?>
20:我點(diǎn)擊后退按鈕,為什么之前填寫(xiě)的東西不見(jiàn)
這是因?yàn)槟闶褂昧藄ession.
解決辦法:
php代碼:
<?php session_cache_limiter('private, must-revalidate');session_start();
.....................?>
21:怎么在圖片里顯示ip地址
php代碼:
<? header("content-type: image/png");
$img = imagecreate(180,50);
$ip = $_server['remote_addr'];
imagecolortransparent($img,$bgcolor);
$bgcolor = imagecolorallocate($img, 0x2c,0x6d,0xaf); // 背景顏色
$shadow = imagecolorallocate($img, 250,0,0); // 陰影顏色
$textcolor = imagecolorallocate($img, oxff,oxff,oxff); // 字體顏色
imagettftext($img,10,0,78,30,$shadow,"d:/windows/fonts/tahoma.ttf",$ip);
//顯示背景
imagettftext($img,10,0,25,28,$textcolor,"d:/windows/fonts/tahoma.ttf","your ip is".$ip);
// 顯示ip
imagepng($img);
imagecreatefrompng($img);
imagedestroy($img);
?>
22:如何取得用戶(hù)的真實(shí)ip
php代碼:
<? function iptype1 () {
if (getenv("http_client_ip"))
{
return getenv("http_client_ip");
}
else
{
return "none";
}
}
function iptype2 () {
if (getenv("http_x_forwarded_for"))
{
return
getenv("http_x_forwarded_for");
}
else {
return "none";
}
}
function iptype3 () {
if (getenv("remote_addr"))
{
return getenv("remote_addr");
}
else {
return "none";
}
}
function ip() {
$ip1 = iptype1();
$ip2 = iptype2();
$ip3 = iptype3();
if (isset($ip1) && $ip1 != "none" && $ip1 != "unknown")
{
return $ip1;
}
elseif (isset($ip2) && $ip2 != "none" && $ip2 != "unknown")
{
return $ip2;
}
elseif (isset($ip3) && $ip3 != "none" && $ip3 != "unknown")
{
return $ip3;
}
else
{ return "none"; }
}
echo ip();
?>
23:如何從數(shù)據(jù)庫(kù)讀取三天內(nèi)的所有記錄
首先表格里要有一個(gè)datetime字段記錄時(shí)間,
格式為'2003-7-15 16:50:00'
select * from `xltxlm` where to_days(now()) - to_days(`date`) <= 3;
24:如何遠(yuǎn)程鏈接mysql數(shù)據(jù)庫(kù)
在增加用戶(hù)的mysql表里有一個(gè)host字段,修改為"%",或者指定允許連接的ip地址,這樣,你就可以遠(yuǎn)程調(diào)用了。
$link=mysql_connect("192.168.1.80:3306","root","");
25:正則到底怎么用
點(diǎn)擊這里
正則表達(dá)式中的特殊字符
26:用apache后,主頁(yè)出現(xiàn)亂碼
方法一:
adddefaultcharset iso-8859-1 改為 adddefaultcharset off
方法二:
adddefaultcharset gb2312
27:為什么單引號(hào),雙引號(hào)在接受頁(yè)面變成(/'/")
解決方法:
方法一:在php.ini中設(shè)置:magic_quotes_gpc = off
方法二: $str=stripcslashes($str)
28:怎么讓程序一直運(yùn)行下去,而不是超過(guò)30秒就停止
set_time_limit(60)//最長(zhǎng)運(yùn)行時(shí)間一分鐘
set_time_limit(0)//運(yùn)行到程序自己結(jié)束,或手動(dòng)停止
29:計(jì)算當(dāng)前在線人數(shù)
例子一:用文本實(shí)現(xiàn)
php代碼:
<?php
//首先你要有讀寫(xiě)文件的權(quán)限
//本程序可以直接運(yùn)行,第一次報(bào)錯(cuò),以后就可以
$online_log = "count.dat"; //保存人數(shù)的文件,
$timeout = 30;//30秒內(nèi)沒(méi)動(dòng)作者,認(rèn)為掉線
$entries = file($online_log);
$temp = array();
for ($i=0;$i<count($entries);$i++) {
$entry = explode(",",trim($entries[$i]));
if (($entry[0] != getenv('remote_addr')) && ($entry[1] > time()))
{
array_push($temp,$entry[0].",".$entry[1]."/n"); //取出其他瀏覽者的信息,并去掉超時(shí)者,保存進(jìn)$temp
}
}
array_push($temp,getenv('remote_addr').",".(time() + ($timeout))."/n");
//更新瀏覽者的時(shí)間
$users_online = count($temp); //計(jì)算在線人數(shù)
$entries = implode("",$temp);
//寫(xiě)入文件
$fp = fopen($online_log,"w");
flock($fp,lock_ex); //flock() 不能在nfs以及其他的一些網(wǎng)絡(luò)文件系統(tǒng)中正常工作
fputs($fp,$entries);
flock($fp,lock_un);
fclose($fp);
echo "當(dāng)前有".$users_online."人在線";
?>
30:什么是模板,怎么用
我用的是phplib模板
下面是其中幾個(gè)函數(shù)的使用
$t->set_file("隨便定義","模板文件.tpl");
$t->set_block("在set_file中定義的","<!-- 來(lái)自模板 -->","隨便定義");
$t->parse("在set_block中定義的","<!-- 來(lái)自模板 -->",true);
$t->parse("隨便輸出結(jié)果","在set_file中定義的");
設(shè)置循環(huán)格式為:
<!--(多于一個(gè)空格) begin $handle(多于一個(gè)空格)-->
如何將模板生成靜態(tài)網(wǎng)頁(yè)
php代碼:
<?php
//這里使用phplib模板
............
............
$tpl->parse("output","html");
$output = $tpl->get("output");// $output 為整個(gè)網(wǎng)頁(yè)內(nèi)容
function wfile($file,$content,$mode='w') {
$oldmask = umask(0);
$fp = fopen($file, $mode);
if (!$fp) return false;
fwrite($fp,$content);
fclose($fp);
umask($oldmask);
return true;
}
// 寫(xiě)到文件里
wfile($file,$output);
header("location:$file");//重定向到生成的網(wǎng)頁(yè)
}
?>
phplib下載地址 smarty下載地址
31:怎么用php解釋字符
比如:輸入2+2*(1+2),自動(dòng)輸出8 可以用eval函數(shù)
php代碼:
<form method=post action="">
<input type="text" name="str"><input type="submit">
</form>
<?php
$str=$_post['str'];
eval("/$o=$str;");
echo "$o";
?>
到此,php的問(wèn)題解答就為大家介紹完畢,希望能對(duì)各位有所幫助。
新聞熱點(diǎn)
疑難解答