Monthly Archives: 12月 2016

phpでmt_rand()を使って乱数を生成するサンプル

PHPコード:
$rand = mt_rand(0,1);
if( $rand==0 )
{
$array = array(36,35,16,29,30);
}
elseif( $rand==1 )
{
$array = array(32,42,37,400,444);
}
foreach( $array as $v => $vv )
{
echo “$vvn”;
}

PHPでユーザエージェントを使ってデバイスを判別する方法

phpコード
<?php
//USER AGENTを取得
$agent = strtolower($_SERVER[‘HTTP_USER_AGENT’]);
//データを分析
$is_pc = (strpos($agent, ‘windows nt’)) ? true : false;
$is_iphone = (strpos($agent, ‘iphone’)) ? true : false;
$is_ipad = (strpos($agent, ‘ipad’)) ? true : false;
$is_android = (strpos($agent, ‘android’)) ? true : false;
//データ出力
if($is_pc){
echo “PCデバイス”;
}
if($is_iphone){
echo “iPhoneデバイス”;
}
if($is_ipad){
echo “iPadデバイス”;
}
if($is_android){
echo “Androidデバイス”;
}
?>

phpでスマホを判別するサンプル

phpコード:
<?php function devicetype_get(){
$agent = strtolower($_SERVER[‘HTTP_USER_AGENT’]);
$type = ‘other’;
if(strpos($agent, ‘iphone’) || strpos($agent, ‘ipad’) ){
$type = ‘ios’;
}
if(strpos($agent, ‘android’)){
$type = ‘android’;
}
return $type;
} ?>

C#入門–foreachの使い方

C#コード
using System;namespace ArrayApplication{
class InfoArray
{
static void Main(string[] args)
{
int [] n = new int[10];
for ( int i = 0; i < 10; i++ )
{
n[i] = i + 100;
}

foreach (int j in n )
{
int i = j-100;
Console.WriteLine(“Element[{0}] = {1}”, i, j);
}
Console.ReadKey();
}
}}

実行結果
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

C#入門–配列Arrayの使い方

C#コード
using System;namespace ArrayApplication{
class MyArray
{
static void Main(string[] args)
{
int [] n = new int[10];
int i,j;

for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100;
}

for (j = 0; j < 10; j++ )
{
Console.WriteLine(“Element[{0}] = {1}”, j, n[j]);
}
Console.ReadKey();
}
}}
実行結果
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

PHP入門–headerの使い方

PHPコード
<?php
header(‘HTTP/1.1 200 OK’);
header(‘HTTP/1.1 404 Not Found’);
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: http://www.sample.org/’);
header(‘Refresh: 10; url=http://www.sample.org/’);
print ‘You will be redirected in 10 seconds’;
// <meta http-equiv=”refresh” content=”10;http://www.sample.org/ />
// override X-Powered-By: PHP:
header(‘X-Powered-By: PHP/4.4.0’);
header(‘X-Powered-By: Brain/0.6b’);
header(‘Content-language: en’);
$time = time() – 60; // or filemtime($fn), etc
header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s’, $time).’ GMT’);
header(‘HTTP/1.1 304 Not Modified’);

header(‘Content-Length: 1234’);
header(‘Content-Type: application/octet-stream’);
header(‘Content-Disposition: attachment; filename=”sample.zip”‘);
header(‘Content-Transfer-Encoding: binary’);
// load the file to send:
readfile(‘sample.zip’);
//キャッシュを無効
header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate’);
header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’); // Date in the past
header(‘Pragma: no-cache’);
//コンテンツタイプ
header(‘Content-Type: text/html; charset=iso-8859-1’);
header(‘Content-Type: text/html; charset=utf-8’);
header(‘Content-Type: text/plain’);
header(‘Content-Type: image/jpeg’); //JPG
header(‘Content-Type: application/zip’); // ZIP
header(‘Content-Type: application/pdf’); // PDF
header(‘Content-Type: audio/mpeg’);
header(‘Content-Type: application/x-shockwave-flash’); //Flash
//ログインダイアログ
header(‘HTTP/1.1 401 Unauthorized’);
header(‘WWW-Authenticate: Basic realm=”Top Secret”‘);
print ‘Text that will be displayed if the user hits cancel or ‘;
print ‘enters wrong login data’;
?>

shellで複数のユーザを追加する方法

shellコード:
#!/bin/bash
#useradd andpassword
echo “please input username:”
read name
echo “please input number:”
read num
n=1
while [ $n -le $num ]
do
useradd $name$n
n=expr $n + 1
done
#/etc/shasow info
echo “please input passwd”
read passwd
m=1
while [ $m -le $num ]
do
echo $passwd |passwd –stdin $name$m >/dev/null
m=expr $m + 1
done

Shellでcsvファイルを読み込んでMySQLに更新する

shellコード:
#!/bin/bash
FILE=’20161204cft.csv’
MYSQL=’/usr/local/mysql/bin/mysql’
USER=’root’
PASSWD=’changfa989′
DB=’JIKEDB’

while read i
do
eval $(echo $i|awk -F’,’ ‘{ printf(“a=%s\nb=%d\nc=%d\nd=%d\ne=%d\nf=%s”,$1,$2,$3,$4,$5,$6); }’)
SQL=”update it_reportb set activeuser=$b,ctnpush=$c,wangmengnu=$d,wangmenguser=$e where name=’$a’ and date=’$f'”
$MYSQL -u$USER -p$PASSWD $DB -e “$SQL”
done < $FILE

shellでランダム乱数作成($RANDOM)方法

方法1
numbers=(122 134 132 154 160 189)

while true
do
index=$(($RANDOM % ${#numbers[*]}))
echo numbers[$index]=${numbers[$index]}
sleep 1
done

方法2
while true
do
RANDOM=$((10#$(date +%N)))
echo random=$RANDOM
sleep 1
done

方法3
random ()
{
local l_min=$1
local l_max=$2
local l_div=$3
local l_range=$(($l_max – $l_min + 1))

RANDOM=date +%N
local number=$((($RANDOM % $l_range + $l_min + $l_div – 1) / $l_div * $l_div))

echo $number
}

update_count ()
{
case $1 in
3) ((numbers[3]++)) ;;
6) ((numbers[6]++)) ;;
9) ((numbers[9]++)) ;;
12) ((numbers[12]++)) ;;
15) ((numbers[15]++)) ;;
18) ((numbers[18]++)) ;;
21) ((numbers[21]++)) ;;
24) ((numbers[24]++)) ;;
27) ((numbers[27]++)) ;;
30) ((numbers[30]++)) ;;
esac
}

count=0
readonly max_count=600
readonly g_min=1
readonly g_max=30
readonly g_div=3

while [ $count -lt $max_count ]
do
update_count random $g_min $g_max $g_div

((count++))
done

echo numbers[3]=${numbers[3]}
echo numbers[6]=${numbers[6]}
echo numbers[9]=${numbers[9]}
echo numbers[12]=${numbers[12]}
echo numbers[15]=${numbers[15]}
echo numbers[18]=${numbers[18]}
echo numbers[21]=${numbers[21]}
echo numbers[24]=${numbers[24]}
echo numbers[27]=${numbers[27]}
echo numbers[30]=${numbers[30]}

3 / 3123