[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
Модератор форума: Mangoost  
звёздный рейтинг 6 звёзд, php txt
ПРОФИЛЬ
TomAlkoДата: Понедельник, 15.Июл.2013, 13:31:55 | Сообщение # 1
Сержант
Группа: Пользователи
Сообщений: 29
Статус: Offline
Решил поделится скриптом мож каму поможет:
PHP: index.php
Код
<?php
/** checked ff-2.11, IE-8, Op-12.8, GC-28.0 CSS.
   * Star Rating Script wrote: TomAlko 11.07.12 | PHP, TXT (version 2.0).
   * Unlike version 1.0 => 2.0 Fixed a bug in the CSS IE. Added JS stars.
   * Detailed settings:
   * $ip         - address of the client (here can be applied to determine its function),
   * $v_ip      - validity check ip address,
   * $id         - test file (note id is set only at the numbers) -> Replace preg_match [rating_stars],
   * $dir         - storage directory rating,
   * $file      - the name of the file rating,
   * $rp1         - interest rating,
   * $eol         - PHP 4.3.10 or PHP 5.0.2
   * $limit_votes   - how much can you vote,
   * $limit_lines - restriction on the entry line (10000 line = 126 kb) n|nnn.nnn.nnn.nnn,   
   * $retes_proc   - 0/100 the initial appearance of stars,
   * $i         - lost
   */
     
/** определение ip адреса вспомогательная функция*/
function getRealIpAddr()
{
      if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
      } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
          $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
          $ip = $_SERVER['REMOTE_ADDR'];
      }
      return $ip;
}

/** настройка */
$ip            = @getRealIpAddr();
$v_ip         = ('/^([0-9]{1,3};)\.([0-9]{1,3};)\.([0-9]{1,3};)\.([0-9]{1,3};)$/i');
$id            = ("123456789"); //test
$dir         = ("./ratings/");
$file         = ($_REQUEST['rating_stars']);
$rp1         = array('1' => '16', '2' => '33', '3' => '50', '4' => '67', '5' => '84', '6' => '100');
$eol         = PHP_EOL;
$limit_votes   = ('10');
$limit_lines   = ('10000');
$retes_proc      = ('0');
$i            = ('0');

/** перебор Ip адрессов в файле */
function count_array_ip ($id, $limit_votes) {
     Global $eol, $v_ip, $ip;
     if (preg_match($v_ip, $ip)) {
        if (file_exists($id)) {
           $rater_file = fopen($id, "r");
           $rater_str  = "";
           $test = array();
           $rater_str  = fread($rater_file, 1024);
           if ($rater_str != "") {
              $rater_data  = explode($eol, $rater_str);
              foreach ($rater_data as $d) {
                 if (preg_match("/\b{$ip}\b/", $d)) {
                    $d = explode("¤", $d);
                    $test[] .= $d['1'];
                 }
              }
           }
           fclose($rater_file);
           if (count($test) < ($limit_votes)) {
              return true;
           } else {
              return false;
           }
        } else {
           $fp = fopen($id, "a");
           fclose($fp);
           header("Location: " . $_SERVER['REQUEST_URI']);
           exit();
        }
     }
     return false;
}

/** проверка + запись */
if (preg_match('/^[0-9]{9}$/i', $_REQUEST['rating_stars']) && preg_match($v_ip, $ip))
{
     if (isset($_POST['rating_1']) || isset($_POST['rating_2']) || isset($_POST['rating_3']) ||
        isset($_POST['rating_4']) || isset($_POST['rating_5']) || isset($_POST['rating_6']))
     {
        foreach ($_POST as $k)
        {
           if (preg_match('/^[0-9]{1}$/i', trim($k)))
           {
              $rater = ($k);
           }
        }
          
        if (count_array_ip($dir . "item_" . $id . ".rating", $limit_votes) && $id == $_REQUEST['rating_stars'])
        {

           if(!file_exists($dir . '.htaccess'))
           {
              $fh = fopen($dir . '.htaccess', "w");
              fwrite($fh, "deny from all");
              fclose($fh);
           }

           $li = @file($dir . 'item_' . $file . '.rating');
           $fp = fopen($dir . 'item_' . $file . '.rating', "a+");
           if (isset($ip) && (count($li) < $limit_lines))
           {
              if (flock($fp, LOCK_EX))
              {
                 $rater_str = fwrite($fp, "{$rater}¤{$ip}{$eol}");
                 fflush($fp);
                 flock($fp, LOCK_UN);
              }
           }
           fclose($fp);
           header('Location: index.html');
        }
        else
        {
           header('Location: index.html');
        }
     }
}

/* output php */
if (file_exists($dir . "item_" . $id . ".rating")) {
     $file = @file($dir . "item_" . $id . ".rating");   
     for ($i=0; $i<count($file); $i++) {
        $ret = explode('¤', $file["{$i}"]);
        $ret_st += ($rp1["{$ret['0']}"]);
     }
       
     if ($i) {
        $retes_proc = number_format(($ret_st/$i), 0, '.', '');
     }
}

/* example | output html*/
print ("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">".
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ru-ru\" lang=\"ru-ru\" dir=\"ltr\" >".
"<head>".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">".
"<link rel=\"stylesheet\" type=\"text/css\" href=\"./css/style.css\" />".
"<script src=\"./js/rating_stars.js\" type=\"text/javascript\"></script>".
"</head>".
"<body>".
// main ========
"<span class=\"rating\">Рейтинг: {$i}/{$retes_proc}%".
     "<div class=\"rating_stars\">".
        "<div id=\"id3\" style=\"width: {$retes_proc}%;\" class=\"rating_img_bg\"><div class=\"rating_img\"></div></div>".
        ((count_array_ip ($dir . "item_" . $id . ".rating", $limit_votes)) ?
        "<form id=\"id1\" method=\"post\" action=\"rating_stars-{$id}.html\">".
           "<input type=\"submit\" value=\"1\" name=\"rating_1\" onMouseover=\"chrating(1,{$retes_proc};)\" />".
           "<input type=\"submit\" value=\"2\" name=\"rating_2\" onMouseover=\"chrating(2,{$retes_proc};)\" />".
           "<input type=\"submit\" value=\"3\" name=\"rating_3\" onMouseover=\"chrating(3,{$retes_proc};)\" />".
           "<input type=\"submit\" value=\"4\" name=\"rating_4\" onMouseover=\"chrating(4,{$retes_proc};)\" />".
           "<input type=\"submit\" value=\"5\" name=\"rating_5\" onMouseover=\"chrating(5,{$retes_proc};)\" />".
           "<input type=\"submit\" value=\"6\" name=\"rating_6\" onMouseover=\"chrating(6,{$retes_proc};)\" />".
        "</form>" : "").
     "</div>".
     "<span id=\"id2\"></span>".
"</span>".
// end =========
"</body>".
"</html>");
?>
CSS: style.css
Код
body
{
        font-family: Tahoma, Arial, Verdana, sans-serif;
        font-size: 11px;
        color: #5c5c5c;
}

.rating, #id2
{
        color: #809DC0;
        overflow: hidden;
        font-family: Tahoma, Arial, Verdana, sans-serif;
        cursor: default;
        font-size: 10px;
        margin-left: 3px;
}

#id2
{
        color: #CCCCCC;
}

.rating_stars
{
        background-color: #FFFFFF;
        padding: 0px;
        border-radius: 3px;
        -moz-border-radius: 3px;
        -webkit-border-radius: 3px;
           overflow: hidden;
        height: 19px;
        width: 102px;
}

.rating_img
{
        background-image: url('../img/rates6.png');
        margin: 0;
        padding: 0;
        height: 19px;
        width: 102px;
        z-index: 9999;
}

.rating_img_bg
{
        background-image: url('../img/favorites.png');
        position: relative;
        margin: 0px;
        padding: 0px;
             
}

.rating_stars input
{
        background-color: transparent;
        position: relative;
        border: 0;
        padding: 0px;
        margin: 0px;
        top: -20px;
        color: #FFBB17;
        cursor: pointer;
        outline: none;
        height: 19px;
        width: 17px;
        font-size: 0px;
}


JS: rating_stars.js
Код
function chrating(v, w) {var id1 = document.getElementById('id1'),id2 = document.getElementById('id2'),id3 = document.getElementById('id3');switch (v) {case 1:id3.style.width='17%';break;case 2:id3.style.width='34%';break;case 3:id3.style.width='50%';break;case 4:id3.style.width='67%'; break;case 5:id3.style.width='84%';break;case 6:id3.style.width='100%';break;default:id3.style.width=w+'%';break;}id2.innerHTML='Оценка: '+v;id1.onmouseout = function () {id2.innerHTML='';id3.style.width = (w+'%');}}

.htaccess

AddDefaultCharset windows-1251
DirectoryIndex index.php index.html

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^rating_stars-([^-]*)\.html$ /index.php?rating_stars=$1
RewriteRule ^index.html$ /index.php [L,QSA]
</IfModule>

картинка: 102x19 (6 звёзд с прозрачным фоном) + (1px border у всей картинки, ето для любимого IE)
по настройкам разберётесь специалбно всё вынес для индивидуальности.

Папки: css,img,js,ratings.
Не проверялось в старых версиях браузеров.


Сообщение отредактировал TomAlko - Вторник, 16.Июл.2013, 01:13:45
 
  • Страница 1 из 1
  • 1
Поиск: