久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔

十天學會PHP/第八天:Sessions、發(fā)送電子郵件

來自站長百科
跳轉(zhuǎn)至: 導航、? 搜索

十天學會PHP

PHP session 變量用于存儲有關用戶會話的信息,或更改用戶會話的設置。Session 變量保存的信息是單一用戶的,并且可供應用程序中的所有頁面使用。

PHP Session變量[ ]

  • 當您運行一個應用程序時,您會打開它,做些更改,然后關閉它。這很像一次會話。計算機清楚你是誰。它知道你何時啟動應用程序,并在何時終止。但是在因特網(wǎng)上,存在一個問題:服務器不知道你是誰以及你做什么,這是由于HTTP地址不能維持狀態(tài)。
  • 通過在服務器上存儲用戶信息以便隨后使用,PHP session解決了這個問題(比如用戶名稱、購買商品等)。不過,會話信息是臨時的,在用戶離開網(wǎng)站后將被刪除。如果您需要永久儲存信息,可以把數(shù)據(jù)存儲在數(shù)據(jù)庫中。
  • Session 的工作機制是:為每個訪問者創(chuàng)建一個唯一的id (UID),并基于這個UID來存儲變量。UID 存儲在 cookie 中,亦或通過URL進行傳導。

開始PHP Session[ ]

  • 在您把用戶信息存儲到 PHP session 中之前,首先必須啟動會話。
  • 注釋:session_start() 函數(shù)必須位于 <html> 標簽之前:
<?php session_start(); ?>

<html>
<body>

</body>
</html>
  • 上面的代碼會向服務器注冊用戶的會話,以便您可以開始保存用戶信息,同時會為用戶會話分配一個 UID。

存儲 Session 變量[ ]

  • 存儲和取回session 變量的正確方法是使用PHP $_SESSION 變量:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>

<html>
<body>

<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>

</body>
</html>
  • 輸出
  • Pageviews=1在下面的例子中,我們創(chuàng)建了一個簡單的 page-view 計數(shù)器。isset() 函數(shù)檢測是否已設置 "views" 變量。如果已設置 "views" 變量,我們累加計數(shù)器。如果 "views" 不存在,則我們創(chuàng)建 "views" 變量,并把它設置為 1:
<?php
session_start();

if(isset($_SESSION['views']))
  $_SESSION['views']=$_SESSION['views']+1;

else
  $_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
  • 終結 Session
  • 如果您希望刪除某些 session 數(shù)據(jù),可以使用 unset() 或 session_destroy() 函數(shù)。
  • unset() 函數(shù)用于釋放指定的 session 變量:
<?php
unset($_SESSION['views']);
?>
  • 您也可以通過 session_destroy() 函數(shù)徹底終結 session:
<?php
session_destroy();
?>

PHP 允許您從腳本直接發(fā)送電子郵件。

PHP mail() 函數(shù)[ ]

PHP mail() 函數(shù)用于從腳本中發(fā)送電子郵件。

  • 語法
  • mail(to,subject,message,headers,parameters)參數(shù) 描述
  • to 必需。規(guī)定 email 接收者。
  • subject 必需。規(guī)定 email 的主題。注釋:該參數(shù)不能包含任何新行字符。
  • message 必需。定義要發(fā)送的消息。應使用 LF (\n) 來分隔各行。
  • headers 可選。規(guī)定附加的標題,比如 From、Cc 以及 Bcc。
  • 應當使用 CRLF (\r\n) 分隔附加的標題。
  • parameters 可選。對郵件發(fā)送程序規(guī)定額外的參數(shù)。
  • 注釋:PHP 需要一個已安裝且正在運行的郵件系統(tǒng),以便使郵件函數(shù)可用。所用的程序通過在 php.ini 文件中的配置設置進行定義。請在我們的 PHP Mail 參考手冊閱讀更多內(nèi)容。

PHP簡易E-Mail[ ]

  • 通過 PHP 發(fā)送電子郵件的最簡單的方式是發(fā)送一封文本email。
  • 在下面的例子中,我們首先聲明變量($to, $subject, $message, $from, $headers),然后我們在 mail() 函數(shù)中使用這些變量來發(fā)送了一封 e-mail:
<?php

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

?>PHP Mail Form
  • 通過 PHP,您能夠在自己的站點制作一個反饋表單。下面的例子向指定的 e-mail 地址發(fā)送了一條文本消息:
<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ; 
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail( "someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>
  • 例子解釋
  • 首先,檢查是否填寫了郵件輸入框
  • 如果未填寫(比如在頁面被首次訪問時),輸出 HTML 表單
  • 如果已填寫(在表單被填寫后),從表單發(fā)送郵件
  • 當點擊提交按鈕后,重新載入頁面,顯示郵件發(fā)送成功的消息

PHPE-mail注入[ ]

  • 首先,請看上一節(jié)中的PHP代碼:
<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ; 
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>
  • 以上代碼存在的問題是,未經(jīng)授權的用戶可通過輸入表單在郵件頭部插入數(shù)據(jù)。
  • 假如用戶在表單中的輸入框內(nèi)加入這些文本,會出現(xiàn)什么情況呢?
someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@example.com
  • 與往常一樣,mail() 函數(shù)把上面的文本放入郵件頭部,那么現(xiàn)在頭部有了額外的 Cc:, Bcc: 以及 To: 字段。當用戶點擊提交按鈕時,這封 e-mail 會被發(fā)送到上面所有的地址!

PHP 防止E-mail注入[ ]

  • 防止e-mail注入的最好方法是對輸入進行驗證。
  • 下面的代碼與上一節(jié)類似,不過我們已經(jīng)增加了檢測表單中email字段的輸入驗證程序:
<html>
<body>
<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail 
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  
  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST['email'] ; 
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }
else
  {//if "email" is not filled out, display the form
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>
  • 在上面的代碼中,我們使用了PHP過濾器來對輸入進行驗證:
  • FILTER_SANITIZE_EMAIL 從字符串中刪除電子郵件的非法字符
  • FILTER_VALIDATE_EMAIL 驗證電子郵件地址
  • 您可以在我們的 PHP 過濾器這一節(jié)中閱讀更多有關過濾器的內(nèi)容。

參考來源[ ]

使用手冊導航

十天學會php

第一天:PHP基礎教程|第二天:PHP基礎語法|第三天:運算符、循環(huán)與數(shù)組|第四天:數(shù)組、函數(shù)、表單|第五天:$_GET、$_POST |第六天:PHP日期、引用|第七天:文件上傳、Cookies|第八天:Sessions、發(fā)送電子郵件|第九天:錯誤處理、異常處理、過濾器|第十天:PHP學習必看