顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2010年1月7日 星期四

[PHP] 進行 HTTP 認証

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>


使用
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
可使瀏覽器跳出要求輸入帳號密碼驗證的對話視窗

使用
$_SERVER['PHP_AUTH_USER']
$_SERVER['PHP_AUTH_PW']
可取得輸入的帳號及密碼

資料來源:
PHP 手冊 http://ftp.cse.yzu.edu.tw/pub/Mirror/php_manual/features.http-auth.html



2009年11月3日 星期二

[PHP]指定範圍字串擷取、字串(數字)範圍判斷

因為某些特殊原因,所以做了這兩個FUNCTION.....

◎可以在字串中取出兩特定關鍵字內的字串

function CatchStr($Str, $StaKey, $EndKey){
 $StaPos = strpos($Str, $StaKey);
 $EndPos = strpos($Str, $EndKey);
 $StaLen = strlen($StaKey);
 $EndLen = strlen($EndKey);

 $CatchKey = substr($Str, $StaPos + $StaLen , $EndPos - $StaPos - $EndLen);
 $OtherKeyA = substr($Str, 0, $StaPos);
 $OtherKeyB = substr($Str, $EndPos + $EndLen);

 return array($CatchKey, $OtherKeyA, $OtherKeyB);
}


CatchStr(字串, 開頭關鍵字, 結尾關鍵字)

例:
$Str = "ABCDE{1234567890}FGHIJK";
$StrArray = CatchStr($Str, "{", "}");
結果:
$StrArray[0] = 1234567890
$StrArray[1] = ABCDE
$StrArray[2] = FGHIJK



◎可以判斷把字串中數字做範圍比較


function SearchNum($Str, $Target, $CountSym = ",", $ToSym = "-"){
 $StrArray = explode($CountSym, $Str);

 foreach ($StrArray as &$value) {
  if (strpos($value, $ToSym)) {
   $VarArray = explode($ToSym, $value);
   if ($VarArray[0] <= $Target && $Target <= $VarArray[1]){
    $Touch = "true";
   }else{
    $Touch = "false";
   }
  }else{
   if ($value == $Target){
    $Touch = "true";
   }else{
    $Touch = "false";
   }
  }
 }
 return $Touch;
}

SearchNum(字串, 欲尋找的目標數值, 每筆資料的分隔符號(預設 , ), 連續資料的連接符號(預設 - ))

例:
$Str = "1,2,3,4,5,6-10,11,12,13";
$StrAns = SearchNum($Str, 8);
結果:
$StrAns = "true"


例:
$Str = "1,2,3,4,5,6,10,11,12,13";
$StrAns = SearchNum($Str, 8);
結果:
$StrAns = "false"



其實這兩個Function還沒有很完美,
CatchStr()還不能判斷字串中有多個關鍵符號,目前只能取第一次出現的關鍵符號。
SearchNum()還不能判斷連續數字的大小順序,只能由小到大判斷。

以後有空在改版~ :P

2009年7月27日 星期一

使用PHP網頁發送Mail(By PHPMailer)

先下載PHPMailer ( 官方下載網址 ) ,解壓縮至網頁資料夾。

建立PHP網頁,下例為MailSend.php

以下插入<head>中

<?php
if ($_POST[MailTitle]){
//載入PHPMailer
require_once("phpmailer/class.phpmailer.php");

$SendTo = "";
$SendTo = $_POST["MailTo"];
$SendNum = "公告已送出!共送出 " . substr_count($SendTo, '@') . " 封";

//建立Mail
$mail = new PHPMailer();

//設定發送
//$mail->IsSMTP();
$mail->Mailer = "mail";
//寄件人
$mail->From = "send@mail.com.tw";
//寄件人名稱
$mail->FromName = "SENDER";
//密件副本收件者
$mail->AddBCC($SendTo);
//回信Email及名稱
$mail->AddReplyTo("send@mail.com.tw", "SENDER");
//設定編碼
$mail->CharSet="utf-8";
//設定信件編碼
$mail->Encoding = "base64";
//設置郵件格式為HTML
$mail->IsHTML(true);
//每50字換行
$mail->WordWrap = 50;

if (is_uploaded_file($_FILES['AttFile']['tmp_name']))
{
$AtF = $_FILES['AttFile']['name'];
}else {
}
//傳送附檔
$mail->AddAttachment($_FILES['AttFile']['tmp_name'], $AtF);
//郵件標題
$mail->Subject= $_POST["SendClass"] . $_POST["MailTitle"];
//郵件內容
$mail->Body ="
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
</head>
<body>
" . ereg_replace("\n", "<BR>\n", $_POST["MailBody"]) . "
</body>
</html>
";

//附加內容
$mail->AltBody = $_POST["MailBody"];

//寄送郵件
if(!$mail->Send())
{
echo "郵件無法順利寄出!";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
if ($_FILES['AttFile']['tmp_name'])
{
unlink($_FILES['AttFile']['tmp_name']);
}
?>
<script labguage="javascript">
alert("<?php echo $SendNum; ?>");
</script>
<?php
}else{
}
?>


以下插入<body>中

<form action="MailSend.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100" height="50" nowrap="nowrap">
<strong>收件者:</strong>
</td>
<td width="300" nowrap="nowrap">
<label>
<input name="MailTo" type="text" id="MailTo" size="47" />
</label>
</td>
</tr>
<tr>
<td width="100" height="50" nowrap="nowrap">
<strong>標題:</strong>
</td>
<td width="300" nowrap="nowrap">
<label>
<select name="SendClass" id="SendClass">
<option value="[公告]">[公告]</option>
<option value="[通知]">[通知]</option>
<option value="">自訂</option>
</select>
</label>
<label>
<input name="MailTitle" type="text" id="MailTitle" size="47" />
</label>
</td>
</tr>
<tr>
<td width="100" nowrap="nowrap">
<strong>內文:</strong>
</td>
<td width="300">
<label>
<textarea name="MailBody" id="MailBody" cols="50" rows="5"></textarea>
</label>
</td>
</tr>
<tr>
<td nowrap="nowrap">
<strong>附件:</strong>
</td>
<td width="300"><label>
<input type="file" name="AttFile" id="AttFile" />
</label>
</td>
</tr>
</table>
<label>
<input type="submit" name="MailSend" id="MailSend" value="送出" />
</label>
</form>

##EasyReadMore##