
echo $_SERVER["REMOTE_ADDR"];
$ip = $_SERVER["REMOTE_ADDR"]; echo ip2long($ip);
host
$host = "syncer.jp"; echo gethostbyname($host);
随机应变 ABCD: Always Be Coding and … : хороший

echo $_SERVER["REMOTE_ADDR"];
$ip = $_SERVER["REMOTE_ADDR"]; echo ip2long($ip);
host
$host = "syncer.jp"; echo gethostbyname($host);
Order Allow,Deny Allow from all Deny from .abcde.com Deny from 111.122.133. Deny from 111.122.133.192/26
referer
SetEnvlf Referer "http://hogehoge.com" Ref1 SetEnvlf Referer "http://hogeweb.com" Ref2 Order Deny, Allow Deny from all Allow from env=Ref1 Allow from env=Ref2
files match
<FilesMatch "\.(gif|.jpg?g|png)$"> SetEnvlf Referer "http://hogehoge.com" Ref1 Order Deny, Allow Deny from all Allow from env=Ref1 </FilesMatch>
SetEnvIf REFERER "sample.com" Ref1 Order Deny,Allow Deny from all Allow from env=Ref1
ip
SetEnvIf REFERER "sample.com" Ref1 Order Deny,Allow Deny from all Allow from env=Ref1 <Files ~"\.log$"> Deny from all </Files>
DOSS
<?php
/**
* $item_ids => 商品IDの配列[1,2,3,4,5]の配列
*/
foreach($item_ids as $item_id1){
$base = $Redis->lRange('Viewer:Item' . $item_id1, 0, 999);
if (count($base) === 0){
continue;
}
foreach ($item_ids as $item_id2){
if ($item_id1 === $item_id2){
continue;
}
$target = $Redis->lRange('Viewer:Item:' . $item_id2, 0, 999);
if (count($target) === 0){
continue;
}
#ジャッカード指数を計算
$join = floatval(count(array_unique(array_merge($base, $target))));
$intersect = floatval(count(array_intersect($base, $target)));
if ($intersect == 0 || $join == 0){
continue;
}
$jaccard = $intersect / $join;
$redis->zAdd('Jaccard:Item:' . $item_id1, $jaccard, $item_id2);
}
}
<?
/**
* $item_id => 商品id
* $user_id => ユーザid
*/
$Redis->lRem('Viewer:Item' . $item_id, $user_id);
$Redis->lPush('Viewer:Item' . $item_id, $user_id);
$Redis->lTrim('Viewer:Item' . $item_id, 0, 999);
<?php
$Redis->zRevRange('Jaccard:Item:' . $item_id, 0, -1);

redisがインストールされた状態
git clone git://github.com/nicolasff/phpredis.git cd phpredis phpize ./configure make make install
php iniにextension=redis.soを追加。
[vagrant@localhost rss11]$ sudo vi /etc/php.ini [vagrant@localhost rss11]$ php -m | grep redis redis
phpでredis操作
<?php
$redis = new Redis();
$redis->connect("127.0.0.1",6379);
// PINGで確認する
echo $redis->ping();
set
<?php
$redis = new Redis();
$redis->connect("127.0.0.1",6379);
// PINGで確認する
echo $redis->set('hoge', 'huga');
$value = $redis->get('hoge');
echo $value;
Python client for Redis key-value store
$ sudo pip install redis
index.python
# _*_ coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
def jaccard(e1, e2):
"""
:param e1: list of int
:param e2: list of int
:rtype: float
"""
set_e1 = set(e1)
set_e2 = set(e2)
return float(len(set_e1 & set_e2)) / float(len(set_e1 | set_e2))
def get_key(k):
return 'JACCARD:PRODUCT:{}'.format(k)
# 商品xを購入した人が1,3,5
product_x = [1, 3, 5]
product_a = [2, 4, 5]
product_b = [1, 2, 3]
product_c = [2, 3, 4, 7]
product_d = [3]
product_e = [4, 6, 7]
# 商品データ
products = {
'X': product_x,
'A': product_a,
'B': product_b,
'C': product_c,
'D': product_d,
'E': product_e,
}
#redis
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
for key in products:
base_customers = products[key]
for key2 in products:
if key == key2:
continue
target_customers = products[key2]
# ジャッカード指数
j = jaccard(base_customers, target_customers)
# redis Sortedに記録
r.zadd(get_key(key), key2, j)
# 例1 商品xを買った人はこんな商品も買っています
print(r.zrevrange(get_key('X'), 0, 2))
# 例2 商品Eを買った人はこんな商品も買っています。
print(r.zrevrange(get_key('E'), 0, 2))
[vagrant@localhost rss10]$ python index.py [b'B', b'D', b'A'] [b'C', b'A', b'X']
レコメンドアルゴリズムには、協調フィルタリングと内容ベース(コンテンツベース)フィルタリングがある
サーバー起動
redis-server
クライアントの起動と終了
redis-cli
データベースの選択
0~15までデフォルトで設定されている
> select 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>
データの保存
> bgsave
データはredis serverを立ち上げたディレクトリにdump.rdbで保存される
string
-set key value
mset key value key value
-get key
mget key key key
数字の操作
incr,decrby
key
> keys *
> keys *m*
> exits scroe
> rename age neneri
> del nenrei
> randomkey
list
push, pop
>rpush mycolor pink
>rpush mycolor green
追加した要素を見る
>lrange mycolor 0 3
>lrange mycolor 0 -1
削除
> rpop mycolor (rightから)
> lpop mycolor (leftから)
> lindex myclor 2
> llen mycolor
> ltrim mycolor 0 2
Set
追加: sadd
削除: srem
一覧: smembers
和集合: sunion
積集合: sinter (共通部分)
差集合: sdiff
>suionstore myunion myset1 myset2
sorted set
追加: zadd
削除: zrem
一覧: zrange
The Jaccard index, also known as the Jaccard similarity coefficient (originally coined coefficient de communauté by Paul Jaccard), is a statistic used for comparing the similarity and diversity of sample sets.
Jaccard(A,B)=|A∩B| / |A∪B|

<!doctype html> <html> <head> <title>検索画面</title> <meta charset="utf-8"> </head> <body> <h1>検索画面</h1> <form action="pdo_search.php" method="post"> 検索用語を入力:<input type="text" name="yourname"> <input type="submit" value="検索する"> </form> </body> </html>

mysqlのテーブルからfetch
<?php
header("Content-type: text/html; charset=utf-8");
if(empty($_POST)){
header("Location: pdo_search_form.html");
exit();
} else {
//名前入力判定
if(!isset($_POST['yourname']) || $_POST['yourname'] === ""){
$errors['name'] = "名前が入力されていません。";
}
}
if(count($errors) === 0){
$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
$user = 'dbuser';
$password = 'xxxx';
try {
$dbh = new PDO($dsn, $user, $password);
$statement = $dbh->prepare("select * from rss where title LIKE (:title)");
if($statement){
$yourname = $_POST['yourname'];
$like_yourname = "%".$yourname."%";
//プレースホルダへ実際の値を設定
$statement->bindValue(':title',$like_yourname, PDO::PARAM_STR);
if($statement->execute()){
//レコード件数取得
$row_count = $statement->rowCount();
while($row = $statement->fetch()){
$rows[] = $row;
}
} else {
$errors['error'] = "検索失敗しました。";
}
$dbh = null;
}
}catch (PDOException $e){
print('Error:'.$e->getMessage());
$errors['error'] = "データベース接続失敗しました。";
}
}
?>
<!doctype html>
<html>
<head>
<title>検索結果</title>
<meta charset="utf-8">
</head>
<body>
<?php if (count($errors) === 0): ?>
<p><?=htmlspecialchars($yourname, ENT_QUOTES, 'utf-8')."さんで検索しました。"?></p>
<p><?=$row_count?>件です。</p>
<table border='1'>
<tr><td>id</td><td>title</td></tr>
<?php
foreach($rows as $row){
?>
<tr>
<td><?=$row['id']?></td>
<td><?=htmlspecialchars($row['title'],ENT_QUOTES,'utf-8')?></td>
</tr>
<?php
}
?>
<?php elseif(count($errors) > 0): ?>
<?php
foreach($errors as $value){
echo "<p>".$value."</p>";
}
?>
<?php endif; ?>
</body>
</html>
mysql重複カラムを除外
SELECT ALL col_name, … FROM tbl_name;
example:
mysql> select distinct address from personal;
mysqlにデータを挿入する場合は、uniqueキーを設定して、重複しないようにする。
crontab:設定した時間になったら定期的にコマンドを実行
crontab -e //クロン編集
MAILTO=”” //結果をメールで送信
0 9 * * * /home/dir/taisho.sh //時間指定と起動するシェルの指定
時間の設定例
分 時 日 月 曜日 説明
0 * * * * 毎時0分にソース実行
0,45 * * * * 毎時0分と45分にソースが実行
0 3 15 * * 毎月15日の3時にソースが実行
linux
ファイル名.sh
#! /bin/sh
command1
command2
command3
1. cronが起動しているかの確認
[vagrant@localhost rss6]$ /etc/rc.d/init.d/crond status crond (pid 2624) を実行中...
2.cronに既にバッチ処理が設定されているか確認
[vagrant@localhost rss6]$ crontab -l no crontab for vagrant
3. cronの設定ファイルを確認
[vagrant@localhost rss6]$ less /etc/crontab
4.バッチの設定
crontab -e 0 0 * * * /bin/bash /home/xxx/cron_all.sh > /dev/null 2>&1