$url = "http://hoge.com/";
$data = array();
$data['TITLE'] = "hhh";
$data['NAME'] = "aaa";
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
));
$content = file_get_contents($url, false, stream_context_create($options));
$nLines = count( $http_response_header ); // after file_get_contents
$match = "/Set-Cookie: JSESSIONID=(.+):/";
$cookie = "";
$matches = array();
for ( $i = 0; $i < $nLines; $i++ )
{
$line = $http_response_header[$i];
if (preg_match( $match,$line,$matches))
{
$cookie = $matches[1];
break;
}
}
echo $cookie;
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Thursday, October 22, 2009
Thursday, August 27, 2009
Wednesday, October 08, 2008
Thursday, September 11, 2008
apache1.3とPHP4とPHP5
apacheとPHP4で運用していてphp5を試したいときにの方のための記述
php5(cgi)で行う。
インストールは例--with-apxs2や--with-apxsをつけないでインストールする。
その後以下のようにシンボリックリンクを張ります。
apache/cgi-bin/php5 -> /bin/php-cgi
php-cgiは コマンドラインから実行するとヘッダ (text/html)を出力する
apacheの設定ファイルを以下のようにします。
<Directory "/**/apache/cgi-bin">
Options +ExecCGI +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/home/tomo/htdocs">
AddHandler php5-script .php
Action php5-script /cgi-bin/php5
Options +ExecCGI +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
指定の場合
/home/tomo/htdocsの下のphpはphp5のcgiとして実行されます。
php5(cgi)で行う。
インストールは例--with-apxs2や--with-apxsをつけないでインストールする。
その後以下のようにシンボリックリンクを張ります。
apache/cgi-bin/php5 -> /bin/php-cgi
php-cgiは コマンドラインから実行するとヘッダ (text/html)を出力する
apacheの設定ファイルを以下のようにします。
<Directory "/**/apache/cgi-bin">
Options +ExecCGI +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/home/tomo/htdocs">
AddHandler php5-script .php
Action php5-script /cgi-bin/php5
Options +ExecCGI +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
指定の場合
/home/tomo/htdocsの下のphpはphp5のcgiとして実行されます。
Monday, September 08, 2008
PHP キャッシュされないため
header("Content-type: text/html; charset=utf-8");
header("Expires: -1");
header("Last-Modified: ". gmdate("D, d M Y H:i:s"). " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: -1");
header("Last-Modified: ". gmdate("D, d M Y H:i:s"). " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
Smarty 設定確認用PHP
単純にSmartyをインストールしたときに動作するかの確認
phpinfo();
で確認してから
---
<?php
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
// $objSmarty->assign('name','Smarty man');
$objSmarty->display('hello.html');
?>
phpinfo();
で確認してから
---
<?php
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
// $objSmarty->assign('name','Smarty man');
$objSmarty->display('hello.html');
?>
PHP mysqli_connectのサンプル
if (!($cn = mysqli_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysqli_select_db($cn,"test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysqli_query($cn,$sql))) {
die;
}
$i=0;
while ($item = mysqli_fetch_array($rs)) {
print "${item['id']} ";
print "${item['name']} ";
print "
";
$data[$i]['name']= $item['name'];
$data[$i]['tel']= $item['tel'];
$i++;
}
mysqli_close($cn);
die;
}
if (!(mysqli_select_db($cn,"test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysqli_query($cn,$sql))) {
die;
}
$i=0;
while ($item = mysqli_fetch_array($rs)) {
print "${item['id']} ";
print "${item['name']} ";
print "
";
$data[$i]['name']= $item['name'];
$data[$i]['tel']= $item['tel'];
$i++;
}
mysqli_close($cn);
Thursday, September 04, 2008
PHP 日本語送信
よくあるパターンなのですが、PEARとか使えない環境のため
<?php
mb_internal_encoding( "UTF-8");
mb_language("Japanese");
header("Content-Type: text/html; charset=UTF-8");
// UTF-8
if (my_email("atesaki@example.com")){
print "メールを送信しました。";
}else{
print "メールの送信に失敗。";
}
function my_email ($to) {
$from = mb_encode_mimeheader("送信者") . "<from@example.net>";
$sbj = "タイトル";
$msg = "本文です。";
$header = "From: {$from}";
return mb_send_mail($to , $sbj , $msg , $header);
}
?>
<?php
mb_internal_encoding( "UTF-8");
mb_language("Japanese");
header("Content-Type: text/html; charset=UTF-8");
// UTF-8
if (my_email("atesaki@example.com")){
print "メールを送信しました。";
}else{
print "メールの送信に失敗。";
}
function my_email ($to) {
$from = mb_encode_mimeheader("送信者") . "<from@example.net>";
$sbj = "タイトル";
$msg = "本文です。";
$header = "From: {$from}";
return mb_send_mail($to , $sbj , $msg , $header);
}
?>
Saturday, June 14, 2008
MVC コントローラー URLによる動的にモジュールを呼ぶ
----- test.php
<?php
/*
e.g
test.php/c=user_proile/m=register/
*/
$path_info = explode('/', $_SERVER['PATH_INFO']);
while(list($cnt,$path) = each($path_info)) {
if ($path == "") continue;
list($key,$value) = explode('=', $path);
if ($key =="c") {
$MyClass = $value;
continue;
}
if ($key =="m") {
$MyMethod = $value;
continue;
}
$_GET[$key] = $value;
}
// overloading is also ok, if PHP5.
if ($MyClass != "" && is_file($MyClass.".php")) {
include_once($MyClass.".php");
if (class_exists($MyClass)) {
$MyModel = new $MyClass;
if (method_exists($MyModel,"dispatch")) {
$MyModel->dispatch($MyMethod);
}
}
}
?>
-----user_proile.php
<?php
class user_proile
{
function dispatch($action)
{
echo $action;
}
}
?>
refer to:
2008/03/php-mvc-controller.html
2005/01/pathinfo.html
<?php
/*
e.g
test.php/c=user_proile/m=register/
*/
$path_info = explode('/', $_SERVER['PATH_INFO']);
while(list($cnt,$path) = each($path_info)) {
if ($path == "") continue;
list($key,$value) = explode('=', $path);
if ($key =="c") {
$MyClass = $value;
continue;
}
if ($key =="m") {
$MyMethod = $value;
continue;
}
$_GET[$key] = $value;
}
// overloading is also ok, if PHP5.
if ($MyClass != "" && is_file($MyClass.".php")) {
include_once($MyClass.".php");
if (class_exists($MyClass)) {
$MyModel = new $MyClass;
if (method_exists($MyModel,"dispatch")) {
$MyModel->dispatch($MyMethod);
}
}
}
?>
-----user_proile.php
<?php
class user_proile
{
function dispatch($action)
{
echo $action;
}
}
?>
refer to:
2008/03/php-mvc-controller.html
2005/01/pathinfo.html
Tuesday, June 10, 2008
rss reader by PHP5 (sample)
<?php
$feed = 'http://test.com/?feed=rss2';
$rss = simplexml_load_file($feed);
$title = $rss->channel->title;
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<?php
foreach ($rss->channel->item as $item) {
echo "<h3><a href='" . $item->link . "'>" . $item->title . "</a></h3>";
$pdate = $item->pubDate;
echo date("Y/m/d G:H:i",strtotime($pdate));
echo "<p>" . $item->description . "</p>";
echo "<p>" . $item->category . "</p>";
}
?>
</body>
</html>
$feed = 'http://test.com/?feed=rss2';
$rss = simplexml_load_file($feed);
$title = $rss->channel->title;
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<?php
foreach ($rss->channel->item as $item) {
echo "<h3><a href='" . $item->link . "'>" . $item->title . "</a></h3>";
$pdate = $item->pubDate;
echo date("Y/m/d G:H:i",strtotime($pdate));
echo "<p>" . $item->description . "</p>";
echo "<p>" . $item->category . "</p>";
}
?>
</body>
</html>
Tuesday, June 03, 2008
PHP memcache のインストール
簡単にインストールできると思ったら失敗したのでここに記す。
pecl download memcache
pecl install memcache-2.2.3.tgz << 落としたものにする
インストールに設定したら、php.iniの編集
extension=memcache.so
動かない場合は インストール成功時に表示されるパスを書けばOK
extension="/usr/local/lib/php/extensions/no-debug-non-zts-20060613/memcache.so"
最後にapacheの再起動
再起動をしないとphpは反映されません。
pecl download memcache
pecl install memcache-2.2.3.tgz << 落としたものにする
インストールに設定したら、php.iniの編集
extension=memcache.so
動かない場合は インストール成功時に表示されるパスを書けばOK
extension="/usr/local/lib/php/extensions/no-debug-non-zts-20060613/memcache.so"
最後にapacheの再起動
再起動をしないとphpは反映されません。
Thursday, March 27, 2008
PHP MVC controllerの勉強
class base {
function __construct() {
}
function dispatch($action){
try{
$this->$action();
}catch( Exception $e ){
$this->errorAction($e->getMessage());
}
}
function errorAction($str=null){
echo "overridden";
}
}
class Front extends Base {
function __construct() {
}
function page() {
throw new Exception( 'Template error.' );
echo "page";
}
function errorAction($str=null){
echo "Front:".$str;
}
}
$f = new Front;
$f->dispatch("page");
function __construct() {
}
function dispatch($action){
try{
$this->$action();
}catch( Exception $e ){
$this->errorAction($e->getMessage());
}
}
function errorAction($str=null){
echo "overridden";
}
}
class Front extends Base {
function __construct() {
}
function page() {
throw new Exception( 'Template error.' );
echo "page";
}
function errorAction($str=null){
echo "Front:".$str;
}
}
$f = new Front;
$f->dispatch("page");
Wednesday, December 12, 2007
checkbox すべてをON・OFF PHPの場合
function checkAll() {
if(document.myForm.checkall.checked){
for(i=0;i if (document.myForm.elements[i].name == "ch[]"){
document.myForm.elements[i].checked = true;
}
}
}else{
for(i=0;i if (document.myForm.elements[i].name == "ch[]"){
document.myForm.elements[i].checked = false;
}
}
}
}
-----
<input type="checkbox" name="checkall" value="" onclick="checkAll(); ">
<input type="checkbox" name="ch[]" value="1">
<input type="checkbox" name="ch[]" value="2">
if(document.myForm.checkall.checked){
for(i=0;i
document.myForm.elements[i].checked = true;
}
}
}else{
for(i=0;i
document.myForm.elements[i].checked = false;
}
}
}
}
-----
<input type="checkbox" name="checkall" value="" onclick="checkAll(); ">
<input type="checkbox" name="ch[]" value="1">
<input type="checkbox" name="ch[]" value="2">
Thursday, May 10, 2007
Smarty MySQLとの連携方法1のちょっと改造した場合
appendからassignにSmartyの呼び出し方法を変更しましたが、結果はそれほど変わらず、Smartyの場合、Smarty文法の方が優先されるようだ。
ちょっと改造した方法
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
$i=0;
while ($item = mysql_fetch_array($rs)) {
$arg[]=array(
'id'=>$item['id'],
'name'=>$item['name'],
'cell'=>$item['tel'],
'email'=>$item['email'],
);
}
mysql_close($cn);
$objSmarty->assign('contacts', $arg);
$objSmarty->display('hoge1.tmpl');
Smarty MySQLとの連携方法1
Smarty MySQLとの連携方法2
ちょっと改造した方法
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
$i=0;
while ($item = mysql_fetch_array($rs)) {
$arg[]=array(
'id'=>$item['id'],
'name'=>$item['name'],
'cell'=>$item['tel'],
'email'=>$item['email'],
);
}
mysql_close($cn);
$objSmarty->assign('contacts', $arg);
$objSmarty->display('hoge1.tmpl');
Smarty MySQLとの連携方法1
Smarty MySQLとの連携方法2
PHP-HTML::Templateを使った方法
http://phphtmltemplate.sourceforge.net/のPerlのHTML::Templateライクなテンプレートの方法を紹介します。
1回目のアクセスはSmartyより早いですが、2回目以降はSmartyの方が早いです。あたり前か
--php
include("template.php");
$f1 = "templates/htmlhtml.tmpl";// テンプレートファイル
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
while ($item = mysql_fetch_array($rs)) {
$arg[]=array(
'id'=>$item['id'],
'name'=>$item['name'],
'cell'=>$item['tel'],
'email'=>$item['email'],
);
}
mysql_close($cn);
$options = array("filename"=>$f1, "debug"=>0, "die_on_bad_params"=>0);
$template =& new Template($options);
$template->AddParam('loop', $arg);
$template->EchoOutput();
---html---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
<TMPL_LOOP NAME="loop">
<p>
<TMPL_VAR NAME="name">
<TMPL_VAR NAME="id">
<TMPL_VAR NAME="cell">
<TMPL_VAR NAME="email">
</p>
</TMPL_LOOP>
</body>
</html>
1回目のアクセスはSmartyより早いですが、2回目以降はSmartyの方が早いです。あたり前か
--php
include("template.php");
$f1 = "templates/htmlhtml.tmpl";// テンプレートファイル
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
while ($item = mysql_fetch_array($rs)) {
$arg[]=array(
'id'=>$item['id'],
'name'=>$item['name'],
'cell'=>$item['tel'],
'email'=>$item['email'],
);
}
mysql_close($cn);
$options = array("filename"=>$f1, "debug"=>0, "die_on_bad_params"=>0);
$template =& new Template($options);
$template->AddParam('loop', $arg);
$template->EchoOutput();
---html---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
<TMPL_LOOP NAME="loop">
<p>
<TMPL_VAR NAME="name">
<TMPL_VAR NAME="id">
<TMPL_VAR NAME="cell">
<TMPL_VAR NAME="email">
</p>
</TMPL_LOOP>
</body>
</html>
Smarty MySQLとの連携方法2
前回紹介した方法より若干速度が速い。ただしプログラム側にデータベースのカラムを追加する必要がある。
array配列を使用したSmaryのassignのサンプル sectionの場合
---php---
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
$id= array();
$name= array();
$tel= array();
$email= array();
while ($item = mysql_fetch_array($rs)) {
array_push($id,$item['id']);
array_push($name,$item['name']);
array_push($tel,$item['tel']);
array_push($email,$item['email']);
}
$objSmarty->assign('id',$id);
$objSmarty->assign('name',$name);
$objSmarty->assign('tel',$tel);
$objSmarty->assign('email',$email);
mysql_close($cn);
$objSmarty->display('html.tmpl');
---html----
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$id}
<p>
name: {$name[customer]}<br />
id: {$id[customer]}<br />
cell: {$tel[customer]}<br />
e-mail: {$mail[customer]}
</p>
{/section}
</body>
</html>
array配列を使用したSmaryのassignのサンプル sectionの場合
---php---
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
$id= array();
$name= array();
$tel= array();
$email= array();
while ($item = mysql_fetch_array($rs)) {
array_push($id,$item['id']);
array_push($name,$item['name']);
array_push($tel,$item['tel']);
array_push($email,$item['email']);
}
$objSmarty->assign('id',$id);
$objSmarty->assign('name',$name);
$objSmarty->assign('tel',$tel);
$objSmarty->assign('email',$email);
mysql_close($cn);
$objSmarty->display('html.tmpl');
---html----
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$id}
<p>
name: {$name[customer]}<br />
id: {$id[customer]}<br />
cell: {$tel[customer]}<br />
e-mail: {$mail[customer]}
</p>
{/section}
</body>
</html>
Smarty MySQLとの連携方法1
この方法のメリットはデータベースのカラム名はテンプレートのみに記述するのでプログラム管理が簡単になる。ただし、後から紹介する方法の方が速度は若干速い。
--PHP部分--
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
while ($item = mysql_fetch_array($rs)) {
$objSmarty->append('contacts',$item);
}
mysql_close($cn);
$objSmarty->display('hoge1.tmpl');
---hoge1.tmpl---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}
id: {$contacts[customer].id}
cell: {$contacts[customer].tel}
e-mail: {$contacts[customer].email}
</p>
{/section}
</body>
</html>
--PHP部分--
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
while ($item = mysql_fetch_array($rs)) {
$objSmarty->append('contacts',$item);
}
mysql_close($cn);
$objSmarty->display('hoge1.tmpl');
---hoge1.tmpl---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}
id: {$contacts[customer].id}
cell: {$contacts[customer].tel}
e-mail: {$contacts[customer].email}
</p>
{/section}
</body>
</html>
Smarty 独自ファイルクラス化
PHP5.0/7.Smartyを使う
HTMLファイルの変更がすぐに反映されないので以下を追加
<:?php
require('Smarty/Smarty.class.php');
class MySmarty extends Smarty {
function MySmarty () {
$this->Smarty();
$this->compile_check = true; // << ここを追加
$mydir = dirname(__FILE__);
$this->template_dir = "$mydir/templates/";
$this->compile_dir = "$mydir/templates_c/";
$this->config_dir = "$mydir/configs/";
$this->cache_dir = "$mydir/cache/";
$this->caching = 0;
}
}
?>
HTMLファイルの変更がすぐに反映されないので以下を追加
<:?php
require('Smarty/Smarty.class.php');
class MySmarty extends Smarty {
function MySmarty () {
$this->Smarty();
$this->compile_check = true; // << ここを追加
$mydir = dirname(__FILE__);
$this->template_dir = "$mydir/templates/";
$this->compile_dir = "$mydir/templates_c/";
$this->config_dir = "$mydir/configs/";
$this->cache_dir = "$mydir/cache/";
$this->caching = 0;
}
}
?>
Tuesday, May 08, 2007
Subscribe to:
Posts (Atom)