「php」文字列をエスケープ処理(escape)するコード

phpコード
function phpescapeFunc($str){
preg_match_all(“/[\x80-\xff].|[\x01-\x7f]+/”,$str,$newstr);
$ar = $newstr[0];
foreach($ar as $k=>$v){
if(ord($ar[$k])>=127){
$tmpString=bin2hex(iconv(“GBK”,”ucs-2″,$v));
if (!eregi(“WIN”,PHP_OS)){
$tmpString = substr($tmpString,2,2).substr($tmpString,0,2);
}
$reString.=”%u”.$tmpString;
} else {
$reString.= rawurlencode($v);
}
}
return $reString;
}

「PHP」header()でファイルをダウンロードするコード

phpコード

fileDownloadFunc($_GET [‘filename’]);

function fileDownloadFunc($file)
{
$file = “.//images//” . $file;
//First, see if the file exists

if (! is_file ( $file ))
{
die ( “<b>404 File not found!</b>” );
}

// Gather relevent info about file
$len = filesize ( $file );
$filename = basename ( $file );
$file_extension = strtolower ( substr ( strrchr ( $filename, “.” ), 1 ) );

// This will set the Content-Type to the appropriate setting for the file
switch ($file_extension)
{
case “pdf” :
$ctype = “application/pdf”;
break;
case “exe” :
$ctype = “application/octet-stream”;
break;
case “zip” :
$ctype = “application/zip”;
break;
case “doc” :
$ctype = “application/msword”;
break;
case “xls” :
$ctype = “application/vnd.ms-excel”;
break;
case “ppt” :
$ctype = “application/vnd.ms-powerpoint”;
break;
case “gif” :
$ctype = “image/gif”;
break;
case “png” :
$ctype = “image/png”;
break;
case “jpeg” :
case “jpg” :
$ctype = “image/jpg”;
break;
case “mp3” :
$ctype = “audio/mpeg”;
break;
case “wav” :
$ctype = “audio/x-wav”;
break;
case “mpeg” :
case “mpg” :
case “mpe” :
$ctype = “video/mpeg”;
break;
case “mov” :
$ctype = “video/quicktime”;
break;
case “avi” :
$ctype = “video/x-msvideo”;
break;

// The following are for extensions that shouldn’t be downloaded
// (sensitive stuff, like php files)
case “php” :
case “htm” :
case “html” :
case “txt” :
die ( “<b>Cannot be used for ” . $file_extension . ” files!</b>” );
break;

default :
$ctype = “application/force-download”;
}

$file_temp = fopen ( $file, “r” );

// Begin writing headers
header ( “Pragma: public” );
header ( “Expires: 0” );
header ( “Cache-Control: must-revalidate, post-check=0, pre-check=0” );
header ( “Cache-Control: public” );
header ( “Content-Description: File Transfer” );
// Use the switch-generated Content-Type
header ( “Content-Type: $ctype” );
// Force the download
$header = “Content-Disposition: attachment; filename=” . $filename . “;”;
header ( $header );
header ( “Content-Transfer-Encoding: binary” );
header ( “Content-Length: ” . $len );

//@readfile ( $file );
echo fread ( $file_temp, filesize ( $file ) );
fclose ( $file_temp );

exit ();
}

?>

PHPフレームワーク「Codeigniter」のcookieを設定する方法まとめ

1.cookieの設定方法1
setcookie(“user_id”,$user_info[‘user_id’],86500);
setcookie(“username”,$user_info[‘username’],86500);
setcookie(“password”,$user_info[‘password’],86500);
//echo $_COOKIE[‘username’];

2.cookieの設定方法2
$this->input->set_cookie(“username”,$user_info[‘username’],60);
$this->input->set_cookie(“password”,$user_info[‘password’],60);
$this->input->set_cookie(“user_id”,$user_info[‘user_id’],60);
//echo $this->input->cookie(“password”);
//echo $this->input->cookie(“username”);
//echo $_COOKIE[‘username’];
//echo $_COOKIE[‘password’];

3.cookieの設定方法3
set_cookie(“username”,$user_info[‘username’],60);
set_cookie(“password”,$user_info[‘password’],60);
set_cookie(“user_id”,$user_info[‘user_id’],60);
//echo get_cookie(“username”);

4.コアコントローラクラス
<?php
class Demo_Controller extends CI_Controller{

public function __construct(){
parent::__construct();
$this->load->helper(“url”);
$this->load->model(“user_model”);
$this->cur_user=$this->user_model->is_login();
if($this->cur_user === false){
header(“location:”.site_url(“index/login”));
}else{

$this->input->set_cookie(“username”,$this->cur_user[‘username’],60);
$this->input->set_cookie(“password”,$this->cur_user[‘password’],00);
$this->input->set_cookie(“user_id”,$this->cur_user[‘user_id’],60);
}
}
}
?>

「Java」keySet()と entrySet()でMapの中身を全て取得する方法

//keySet()  
Set<String> set = map.keySet();
for (String s:set) {
System.out.println(s+”,”+map.get(s));
}

  //entrySet()
Set<Map.Entry<String, String>> entryseSet=map.entrySet();
for (Map.Entry<String, String> entry:entryseSet) {
System.out.println(entry.getKey()+”,”+entry.getValue());
}

JavaScriptでURLパラメーターを取得するコード

JSコード
var url=location.search;
var cft;
var Request = new Object();
if(url.indexOf(“?”)!=-1)
{
var str = url.substr(1);
strs= str.split(“&”);
for(var i=0;i
{
Request[strs[i].split(“=”)[0]]=(strs[i].split(“=”)[1]);
}
}
cft = Request[“cft”];

「javaScript」Array.remove()で配列の要素を削除するメモ

サンプルコード
Array.prototype.remove = function(b) {
var a = this.indexOf(b);
if (a >= 0) {
this.splice(a, 1);
return true;
}
return false;
};

「java」List配列(数値)の最大値・最小値を取得するコード

1.List<double[]>配列
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 11.3, 15.5, 13.8, 16.8, 23.4, 24.4, 26.4, 25.1, 23.6, 34.3, 17.2,
13.9 });
values.add(new double[] { 10, 10, 12, 35, 20, 24, 26, 26, 23, 18, 14, 11 });
values.add(new double[] { 6, 5.3, 8, 32, 37, 22, 24.2, 24, 19, 15, 9, 6 });
values.add(new double[] { 27, 10, 31, 15, 39, 23, 34, 25, 56, 18, 13, 10 });

2.最大値(max value)
public double ListDoubleArrMaxVal(List<double[]> dataList) {
double max = Double.MIN_VALUE;
for (double[] ds : dataList) {
for (double d : ds) {
if (d > max) {
max = d;
}
}
}
return max;
}
3.最小値(min value)
public double ListDoubleArrMinVal(List<double[]> dataList) {
double min = Double.MAX_VALUE;
for (double[] ds : dataList) {
for (double d : ds) {
if (d < min) {
min = d;
}
}
}
return min;
}

「nginxサーバ」.htaccessファイルを設定するメモ

1.ファイル.htaccessの作成
# nginx rewrite rule
rewrite ^(.*?)/article/.*?-(d+)-(d+).html$  $1/display.html?id=$2&page=$3   break;
rewrite ^(.*?)/category/.*?-(d+)-(d+).html$ $1/listdata.html?id=$2&page=$3   break;
rewrite ^(.*?)/tag/([^/]+)/?$  $1/tag.html?w=$2    break;
rewrite ^(.*?)/tag/([^/]+)/(d+)$    $1/tag.html?w=$2&page=$3    break;
# end nginx rewrite rule

2.nginx.confの修正
# vim  /usr/local/etc/nginx/nginx.conf
下記追加
include /usr/www/.htaccess
nginx再起動
# /usr/local/etc/rc.d/nginx restart

「Apacheサーバ」 time_wait接続数が大量現象を解決する方法メモ

1.TIME_WAIT数をコマンドで確認
$ netstat -anp|grep TIME_WAIT

2.カーネルパラメータの設定変更
#vim /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30

3.設定反映
# /sbin/sysctl -p

「Linux」シェルプログレスバー(処理の進捗)を表示するコード

サンプルコード
#!/bin/bash

b=”
i=0
while [ $i -le  100 ]
do
printf “progress:[%-50s]%d%%\r” $b $i
sleep 0.1
i=`expr 2 + $i`
b=#$b
done
echo