最近在製作一隻 PHP 系統,遇到資料庫中文出現亂碼,但是在頁面上正常顯示,寫入更新都是亂碼。
由於 php.ini 內的預設編碼為 default_charset = "iso-8859-1",所以只要修改成與資料庫編碼相同的編碼即可,而我則改為 UTF-8。
另一種方法,就是在連結資料庫後加上 mysql_query('SET NAMES utf8'); 讓編碼成為 UTF-8 即可。
回 PHP 目錄
回首頁
學習程式設計過程中總會碰到疑難雜症,而這些疑難雜症又不是一時能解決,不論是以往或是從未遇過的,都可將這些在程式設計上所碰到問題的解決方法筆記下來,或許解決了就算了,但等到下次有相同問題時,可將這程式設計筆記再拿來參考,就不枉做筆記了。
2012年6月1日 星期五
2012年5月17日 星期四
PHP登入-使用 Bootstrap 美化
以下程式碼基於PHP研習中的實作練習下的管理者登入。
1. 先建立一個”admin”資料庫,並建立” manager”資料表,資料表中新增三欄位分別為「idno」”(主索引)、「 id」 char(12)、「pwd」 char(12)。
2. 在config.php內設定資料庫環境。
3. connect.php插入config.php用來連結資料庫,並下選擇資料庫的指令。
4. login.php為使用介面,以post方式送出。
(該範例使用Boostrap美化,使用過程可參考: Asp.net 的基本使用)
5. check.php將login.php傳來兩欄位”id”及” pwd”值加以判斷帳密是否正確,若登入成功會以session方式暫存帳號並顯示於畫面中。
6. logout.php登出,清除session值。
回 PHP 目錄
回首頁
1. 先建立一個”admin”資料庫,並建立” manager”資料表,資料表中新增三欄位分別為「idno」”(主索引)、「 id」 char(12)、「pwd」 char(12)。
2. 在config.php內設定資料庫環境。
<?php //======資料庫變數======// $cfgDB_HOST = 'localhost'; //資料庫所在主機位置 $cfgDB_PORT ="3306"; //資料庫PORT(預設為3306) $cfgDB_USERNAME ="hebe"; //資料庫帳號 $cfgDB_PASSWORD ="12345"; //資料庫密碼 $cfgDB_NAME = "admin"; //使用的資料庫 ?>
3. connect.php插入config.php用來連結資料庫,並下選擇資料庫的指令。
<?php
// 建立資料庫連線
function create_connection()
{
include 'config.php';
$link = mysql_connect($cfgDB_HOST . ":" . $cfgDB_PORT, $cfgDB_USERNAME, $cfgDB_PASSWORD);
return $link;
}
// 選擇資料庫
function execute_sql($cfgDB_NAME, $sql, $link)
{
mysql_select_db($cfgDB_NAME, $link);
$result = mysql_query($sql, $link);
return $result;
}
?>
4. login.php為使用介面,以post方式送出。
(該範例使用Boostrap美化,使用過程可參考: Asp.net 的基本使用)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
</head>
<body>
<div class="span4" >
<form action="check.php" method="post" >
<table class="table table-bordered" >
<tr>
<th colspan="2">管理者登入</th>
</tr>
<tr>
<td> 帳 號 </td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td> 密 碼 </td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登入" class="btn btn-primary"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
5. check.php將login.php傳來兩欄位”id”及” pwd”值加以判斷帳密是否正確,若登入成功會以session方式暫存帳號並顯示於畫面中。
<?php
//連結資料庫
session_start();
require_once("connect.php");
$link = create_connection();
$id = $_POST["id"]; //login.php傳來帳號欄位輸入值
$pwd = $_POST["pwd"]; //login.php傳來密碼欄位輸入值
$sql = "select id from manager where id='$id' and pwd='$pwd'";
$rs = execute_sql("admin", $sql, $link);
$total= mysql_num_rows($rs);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
</head>
<body>
<div class="span4" >
<?php
//判斷所選取資料的數目是否大於0,若大於0則表示資料庫有這筆帳號和密碼,即登入成功
if($total>0){
$_SESSION['username'] = $id;
echo "歡迎" . $_SESSION['username'] . "登入成功!<br/>";
echo '<a class="btn btn-danger btn-small" href="logout.php">登出</a>';
}
else{
echo "帳號或密碼輸入有誤,請再重試一次!<br/>";
echo '<a class="btn btn-info btn-small" href="logout.php">返回</a>';
}
?>
</div>
</body>
</html>
6. logout.php登出,清除session值。
<?php session_start(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php //將session清空 unset($_SESSION['username']); echo '登出中......'; echo '<meta http-equiv=REFRESH CONTENT=1;url=login.php>'; ?>
完成圖
回 PHP 目錄
回首頁
標籤:
PHP 程式設計筆記
2012年5月12日 星期六
PHP上傳檔案 - 使用 Boostrap 美化
這次小女子我來分享PHP上傳檔案的方法啦~~就請大家參考參考一下羅~ :D
以下程式碼基於PHP 研習中的實作練習下的檔案上傳程式碼。
(須先自行於同目錄下建立"uploads"資料夾哦~)
完成圖
回 PHP 目錄
回首頁
以下程式碼基於PHP 研習中的實作練習下的檔案上傳程式碼。
1.
於config.php設定存放檔案的資料夾及檔案限制大小。(須先自行於同目錄下建立"uploads"資料夾哦~)
<?php //存放檔案的資料夾,這裡我是開一個「uploads」的資料夾來存放上傳的檔案。// $cfg_path = "uploads/"; //上傳檔案大小限制,可根據自己的需求來作修改// $cfg_max_size = 2*1024*1024; ?>
2.
work.php以_FILES函數處裡文件上傳,並用move_uploaded_file()函數將資料上傳至指定資料夾中,其中還包含了上傳後的檔案命名。<?php
include ("config.php"); //插入config.php
//如果檔案上傳有錯誤,可以顯示錯誤代碼。
if ($_FILES['upfile']['error'] > 0)
{
echo "Error: " . $_FILES['upfile']['error'] . "<br />";
}
else
{
//上傳檔案的原始名稱。
$upfile_name = $_FILES['upfile']['name'];
//上傳的檔案原始大小。
$upfile_size = $_FILES['upfile']['size'];
//上傳的檔案類型。
$upfile_type = $_FILES['upfile']['type'];
//上傳檔案後的暫存資料夾位置。
$upfile_tmp_name = $_FILES['upfile']['tmp_name'];
}
//==== 檢查是否確實有上傳檔案 ====//
if($upfile_size==0){
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
<script language=javascript>
alert("請確實選擇要上傳的檔案喔!");
history.go(-1); //返回
</script>
</body>
</html>
<?php
}
//檢查上傳的檔案是否有超過限制的大小
if($upfile_size>$cfg_max_size){
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
<script language="javascript">
alert("上傳檔案的大小不得超過 <? echo $cfg_max_size /1024/1024 ?> MB,\n請您重新上傳!");
history.go(-1);
</script>
</body>
</html>
<?php
exit;
}
$yearDir = date("Y");
$monthDir = date("m");
$dayDir = date("d");
$full_path = "$cfg_path" . "$yearDir/$monthDir/$dayDir/";
$year_path = "$cfg_path" . "$yearDir/";
$month_path = "$cfg_path" . "$yearDir/$monthDir/";
//判定是否有建立"年","月","日" 資料夾,若沒有就建立資料夾
if(!is_dir($year_path))
mkdir($year_path);
if(!is_dir($month_path))
mkdir($month_path);
if(!is_dir($full_path))
mkdir($full_path);
/*
explode(separator,string,limit)
separator 必需。規定在哪里分割字串。
string 必需。要分割的字串。
limit 可選。規定所返回的數組元素的最大數目。
"a.b.c.d.png" => $file_name = abcd
=> $file_subname = png
array[] {"a", "b", "c", "d", "png"} count = 5
*/
$file_arr = explode(".", $_FILES['upfile']['name']);
$file_subname = $file_arr[count($file_arr) - 1]; //副檔名
$file_name = "";
for ($i = 0; $i < count($file_arr) - 1; $i ++ )
$file_name .= $file_arr[$i];
//上傳後檔案命名
$upfile_name = $file_name . '-' . date("His") . '.' . $file_subname;
//====== 檔案上傳至資料夾 =====//
$cp = move_uploaded_file($upfile_tmp_name, "$full_path/$upfile_name");
//判斷是否上傳成功
if($cp)
{
echo "Upload Success!<br>";
echo "檔案大小:".$upfile_size."<br>";
echo "檔案名稱:".$upfile_name;
}
else
{
echo "Fail!";
}
?>
3.
可至Apache資料夾下的bin資料夾中找到php.ini檔,將其date.timezone = UTC改成date.timezone = "Asia/Taipei",此動作是將時間改成台灣的標準時間,修改後將伺服器重新啟動即可。4.
upload.php為使用介面程式碼,其中我為了美化整體畫面套用了Boostrap,你可至bootstrap 網站下載相關.css及.js檔。 ( 使用過程可參考:Bootstrap 於 Asp.net 的基本使用)<?php
include ("config.php"); //插入config.php
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<link rel="stylesheet" type="text/css" href="bootstrap.css" />
<script language="javascript">
function checkform()
{
if(document.thisform.upfile.value=='')
{
alert('您還沒選擇要上傳的檔案喔!')
return false;
}
return true;
}
</script>
</head>
<body>
<br><br>
<center>
<form action="work.php" method="post" enctype="multipart/form-data" name="thisform">
※上傳檔案大小以 <font color=red><?php echo $cfg_max_size/1024/1024; ?></font> MB 為限<br>
<table ><tr><td>
<table class="table table-striped table-bordered table-condensed">
<tr>
<td> 選擇檔案 </td>
<td> <input type="file" name="upfile" class="input-file" size="20" /></td>
</tr>
<tr>
<td>
<input type="hidden" name="work" value="upload" />
<input type="submit" value="確定送出" onclick="return checkform()" class="btn btn-primary" />
</td>
</tr>
</table>
</td></tr></table>
</form>
</body>
</html>
完成圖
回 PHP 目錄
回首頁
標籤:
PHP 程式設計筆記
訂閱:
文章
(
Atom
)