ログイン |  新規登録
メインメニュー
広告
リンク
テーマ選択

(6 テーマ)

Counter: 421, today: 1, yesterday: 2

PHP関連のメモ


メール

メールを送信する

単純なメールなら php の mail() 関数で送れる。

mail($to, $subject, $body);

ヘッダーを細かく制御したいなどの場合は pear の Mailクラスが便利。

Net_SMTP クラスと Mail クラスが必要なので入ってなかったら pear でインストールする。

# pear install Net_SMTP
# pear install Mail

php でメールを送る関数のサンプル。(ソースファイルの文字コードや内部エンコーディングが関わるので注意が必要)

include('Mail.php');

function my_mail($from, $to, $subject, $body)
{
    // SMTPサーバ
    $mail_options = array(
                          'host'      => MAIL_HOST, // ホスト名
                          'port'      => MAIL_PORT, // ポート番号
                          'auth'      => false,          // 認証必要?
                          'username'  => "",        // ユーザー名
                          'password'  => "",        // パスワード
                          'localhost' => 'localhost',   // ?
                          );

     $headers['From']    = $from;
     $headers['To']      = $to;
     $headers['Subject'] = mb_encode_mimeheader( $subject );
     $headers['Date'] = date('r');

     // JIS変換
    $body = mb_convert_encoding( $body, "ISO-2022-JP", "auto" );

    $mail_object =& Mail::factory("SMTP",$mail_options);  // SMTP送信準備

    $mail_object->send($to, $headers, $body);
}

日時関連

日付の文字列を得る

echo date("Y/m/d G:i:s ");

出力はこうなる

2008/07/04 12:48:56

曜日を知りたい

曜日だけ知りたい

$week = date("w");
// 0 が日曜日、1 が月曜日

未整理

include パスを追加

set_include_path(get_include_path() . ':/path/to/dir');

もしくは

ini_set('include_path', get_include_path() . PATH_SEPARATOR . '/path/to/dir');

カレントディレクトリ名を取得

$curr_dir = basename(dirname(__FILE__));

PHPで301リダイレクトを行う方法

<?php
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://www.example.com/" );
?>

PHPで生POSTを得る

$stdin = fopen("php://input", "r");
$get = fgets($stdin, 10000);
fclose($stdin);

PHPで使えるCAPTCHA画像作成ライブラリまとめ

kcapcha を使ってみてよい感じだった。