Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

मैं अपनी साइट पर अद्वितीय विज़िटर की गणना कैसे करूं?

यहाँ एक अच्छा ट्यूटोरियल है, जो आपको चाहिए। (स्रोत: courseweb.net/php-mysql )

ऑनलाइन उपयोगकर्ताओं और आगंतुकों को पंजीकृत करें और दिखाएं

MySQL तालिका का उपयोग करके ऑनलाइन उपयोगकर्ताओं और आगंतुकों की गणना करें

इस ट्यूटोरियल में आप सीख सकते हैं कि अपने वेबपेज में ऑनलाइन उपयोगकर्ताओं और आगंतुकों की संख्या को कैसे पंजीकृत करना, गिनना और प्रदर्शित करना है। सिद्धांत यह है:प्रत्येक उपयोगकर्ता / आगंतुक एक टेक्स्ट फ़ाइल या डेटाबेस में पंजीकृत है। हर बार जब वेबसाइट का कोई पृष्ठ एक्सेस किया जाता है, तो php स्क्रिप्ट एक निश्चित समय (जैसे 2 मिनट) से पुराने सभी रिकॉर्ड हटा देता है, वर्तमान उपयोगकर्ता / आगंतुक जोड़ता है और प्रदर्शित करने के लिए छोड़े गए रिकॉर्ड की संख्या लेता है।

आप ऑनलाइन उपयोगकर्ताओं और आगंतुकों को सर्वर पर या MySQL तालिका में एक फ़ाइल में संग्रहीत कर सकते हैं। इस मामले में, मुझे लगता है कि रिकॉर्ड जोड़ने और पढ़ने के लिए एक टेक्स्ट फ़ाइल का उपयोग उन्हें एक MySQL तालिका में संग्रहीत करने से तेज़ है, जिसके लिए आवश्यक है अधिक अनुरोध।

सबसे पहले यह MySQL तालिका के साथ विधि की तुलना में सर्वर पर एक टेक्स्ट फ़ाइल में रिकॉर्डिंग के साथ विधि प्रस्तुत करता है।

इस ट्यूटोरियल में प्रस्तुत स्क्रिप्ट के साथ फ़ाइलें डाउनलोड करने के लिए, क्लिक करें -> ऑनलाइन गणना करें उपयोगकर्ता और आगंतुक

• दोनों लिपियों को ".php" फ़ाइलों में शामिल किया जा सकता है (include() के साथ) ) , या " . में .html" फ़ाइलें (<script> . के साथ) ) , जैसा कि आप इस पृष्ठ के नीचे प्रस्तुत उदाहरणों में देख सकते हैं; लेकिन सर्वर को PHP चलाना चाहिए।

ऑनलाइन उपयोगकर्ताओं और आगंतुकों को टेक्स्ट फ़ाइल में संग्रहीत करना

PHP के साथ सर्वर पर किसी फ़ाइल में रिकॉर्ड जोड़ने के लिए आपको उस फ़ाइल में CHMOD 0766 (या CHMOD 0777) अनुमतियाँ सेट करनी होंगी, ताकि PHP उसमें डेटा लिख ​​सके।

  1. अपने सर्वर पर एक टेक्स्ट फ़ाइल बनाएं (उदाहरण के लिए, नाम userson.txt ) और इसे CHMOD 0777 give दें अनुमतियाँ (आपके FTP एप्लिकेशन में, उस फ़ाइल पर राइट क्लिक करें, गुण चुनें, फिर Read . चुनें , Write , और Execute विकल्प)।
  2. एक PHP फ़ाइल बनाएं (नाम usersontxt.php ) नीचे दिए गए कोड के साथ, फिर इस php फ़ाइल को उसी निर्देशिका में कॉपी करें जैसे userson.txt

usersontxt.php के लिए कोड ;

<?php
// Script Online Users and Visitors - http://coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();        // start Session, if not already started

$filetxt = 'userson.txt';  // the file in which the online users /visitors are stored
$timeon = 120;             // number of secconds to keep a user online
$sep = '^^';               // characters used to separate the user name and date-time
$vst_id = '-vst-';        // an identifier to know that it is a visitor, not logged user

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)

    $uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the line with visitors
$nrvst = 0;                                       // to store the number of visitors

// sets the row with the current user /visitor that must be added in $filetxt (and current timestamp)

    $addrow[] = $uvon. $sep. time();

// check if the file from $filetxt exists and is writable

    if(is_writable($filetxt)) {
      // get into an array the lines added in $filetxt
      $ar_rows = file($filetxt, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      $nrrows = count($ar_rows);

            // number of rows

  // if there is at least one line, parse the $ar_rows array

      if($nrrows>0) {
        for($i=0; $i<$nrrows; $i++) {
          // get each line and separate the user /visitor and the timestamp
          $ar_line = explode($sep, $ar_rows[$i]);
      // add in $addrow array the records in last $timeon seconds
          if($ar_line[0]!=$uvon && (intval($ar_line[1])+$timeon)>=time()) {
            $addrow[] = $ar_rows[$i];
          }
        }
      }
    }

$nruvon = count($addrow);                   // total online
$usron = '';                                    // to store the name of logged users
// traverse $addrow to get the number of visitors and users
for($i=0; $i<$nruvon; $i++) {
 if(preg_match($rgxvst, $addrow[$i])) $nrvst++;       // increment the visitors
 else {
   // gets and stores the user's name
   $ar_usron = explode($sep, $addrow[$i]);
   $usron .= '<br/> - <i>'. $ar_usron[0]. '</i>';
 }
}
$nrusr = $nruvon - $nrvst;              // gets the users (total - visitors)

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. $nruvon. '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// write data in $filetxt
if(!file_put_contents($filetxt, implode("\n", $addrow))) $reout = 'Error: Recording file not exists, or is not writable';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result
?>
  1. यदि आप उपरोक्त स्क्रिप्ट को ".php" फ़ाइल में शामिल करना चाहते हैं, तो उस स्थान पर निम्न कोड जोड़ें जहां आप ऑनलाइन उपयोगकर्ताओं और आगंतुकों की संख्या दिखाना चाहते हैं:

4. ".html" फ़ाइल में ऑनलाइन विज़िटर / उपयोगकर्ताओं की संख्या दिखाने के लिए, इस कोड का उपयोग करें:

<script type="text/javascript" src="usersontxt.php?uvon=showon"></script>

यह स्क्रिप्ट (और नीचे प्रस्तुत दूसरी) $_SESSION के साथ काम करती है। PHP फ़ाइल की शुरुआत में, जिसमें आप इसका उपयोग करते हैं, आपको जोड़ना होगा:session_start ();। MySQL तालिका का उपयोग करके ऑनलाइन उपयोगकर्ताओं और आगंतुकों की गणना करें

एक MySQL तालिका में ऑनलाइन आगंतुकों और उपयोगकर्ताओं की संख्या को पंजीकृत करने, गिनने और दिखाने के लिए, तीन SQL क्वेरी करने की आवश्यकता होती है:एक निश्चित समय से पुराने रिकॉर्ड हटाएं। नए उपयोगकर्ता / आगंतुक के साथ एक पंक्ति डालें, या, यदि यह पहले से ही है डाला गया है, इसके कॉलम में टाइमस्टैम्प अपडेट करें। शेष पंक्तियों का चयन करें। यहां एक स्क्रिप्ट के लिए कोड है जो ऑनलाइन उपयोगकर्ताओं और आगंतुकों को संग्रहीत और प्रदर्शित करने के लिए एक MySQL तालिका ("उपयोगकर्ता नाम" का उपयोग करता है) का उपयोग करता है।

  1. सबसे पहले हम 2 कॉलम (uvon, dt) के साथ "उपयोगकर्ता" तालिका बनाते हैं। "उवन" कॉलम में उपयोगकर्ता का नाम (यदि लॉग इन किया गया है) या विज़िटर का आईपी संग्रहीत किया जाता है। "डीटी" कॉलम में टाइमस्टैम्प (यूनिक्स टाइम) के साथ एक नंबर स्टोर किया जाता है जब पेज एक्सेस किया जाता है।
  • निम्न कोड को php फ़ाइल में जोड़ें (उदाहरण के लिए, "create_userson.php" नाम से):

के लिए कोड create_userson.php :

<?php
header('Content-type: text/html; charset=utf-8');

// HERE add your data for connecting to MySQ database
$host = 'localhost';           // MySQL server address
$user = 'root';                // User name
$pass = 'password';            // User`s password
$dbname = 'database';          // Database name

// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);

// check connection
if (mysqli_connect_errno()) exit('Connect failed: '. mysqli_connect_error());

// sql query for CREATE "userson" TABLE
$sql = "CREATE TABLE `userson` (
 `uvon` VARCHAR(32) PRIMARY KEY,
 `dt` INT(10) UNSIGNED NOT NULL
 ) CHARACTER SET utf8 COLLATE utf8_general_ci"; 

// Performs the $sql query on the server to create the table
if ($conn->query($sql) === TRUE) echo 'Table "userson" successfully created';
else echo 'Error: '. $conn->error;

$conn->close();
?>
  1. अब हम स्क्रिप्ट बनाते हैं जो userson में डेटा को सम्मिलित, हटाता और चुनता है तालिका (कोड के बारे में स्पष्टीकरण के लिए, स्क्रिप्ट में टिप्पणियाँ देखें)।
  • नीचे दिए गए कोड को किसी अन्य php फ़ाइल में जोड़ें (नाम usersmysql.php ):दोनों फाइलों में आपको MySQL डेटाबेस से कनेक्ट करने के लिए अपना व्यक्तिगत डेटा वेरिएबल में जोड़ना होगा:$host , $user , $pass , और $dbname

usersmysql.php के लिए कोड:

<?php
// Script Online Users and Visitors - coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();         // start Session, if not already started

// HERE add your data for connecting to MySQ database
$host = 'localhost';           // MySQL server address
$user = 'root';                // User name
$pass = 'password';            // User`s password
$dbname = 'database';          // Database name

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)
$vst_id = '-vst-';         // an identifier to know that it is a visitor, not logged user
$uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the rows with visitors
$dt = time();                                    // current timestamp
$timeon = 120;             // number of secconds to keep a user online
$nrvst = 0;                                     // to store the number of visitors
$nrusr = 0;                                     // to store the number of usersrs
$usron = '';                                    // to store the name of logged users

// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);

// Define and execute the Delete, Insert/Update, and Select queries
$sqldel = "DELETE FROM `userson` WHERE `dt`<". ($dt - $timeon);
$sqliu = "INSERT INTO `userson` (`uvon`, `dt`) VALUES ('$uvon', $dt) ON DUPLICATE KEY UPDATE `dt`=$dt";
$sqlsel = "SELECT * FROM `userson`";

// Execute each query
if(!$conn->query($sqldel)) echo 'Error: '. $conn->error;
if(!$conn->query($sqliu)) echo 'Error: '. $conn->error;
$result = $conn->query($sqlsel);

// if the $result contains at least one row
if ($result->num_rows > 0) {
  // traverse the sets of results and set the number of online visitors and users ($nrvst, $nrusr)
  while($row = $result->fetch_assoc()) {
    if(preg_match($rgxvst, $row['uvon'])) $nrvst++;       // increment the visitors
    else {
      $nrusr++;                   // increment the users
      $usron .= '<br/> - <i>'.$row['uvon']. '</i>';          // stores the user's name
    }
  }
}

$conn->close();                  // close the MySQL connection

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. ($nrusr+$nrvst). '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result
?>
  1. अपने सर्वर पर इन दो php फ़ाइलों को बनाने के बाद, "उपयोगकर्ता" तालिका बनाने के लिए अपने ब्राउज़र पर "create_userson.php" चलाएँ।

  2. usersmysql.php शामिल करें php फ़ाइल में फ़ाइल जिसमें आप ऑनलाइन उपयोगकर्ताओं और आगंतुकों की संख्या प्रदर्शित करना चाहते हैं।

  3. या, यदि आप इसे ".html" फ़ाइल में सम्मिलित करना चाहते हैं, तो यह कोड जोड़ें:

इन लिपियों का उपयोग करने वाले उदाहरण

• php फ़ाइल में "usersontxt.php` को शामिल करना:

काउंटर ऑनलाइन उपयोगकर्ता और आगंतुक

• html फ़ाइल में "usersmysql.php" को शामिल करना:

<!doctype html>
<html>
<head>
 <meta charset="utf-8" />
 <title>Counter Online Users and Visitors</title>
 <meta name="description" content="PHP script to count and show the number of online users and visitors" />
 <meta name="keywords" content="online users, online visitors" />
</head>
<body>

<!-- Includes the script ("usersontxt.php", or "usersmysql.php") -->
<script type="text/javascript" src="usersmysql.php?uvon=showon"></script>

</body>
</html>

दोनों स्क्रिप्ट (सर्वर पर टेक्स्ट फ़ाइल में या MySQL तालिका में डेटा संग्रहीत करने के साथ) इस तरह एक परिणाम प्रदर्शित करेंगे:ऑनलाइन:5

आगंतुक:3उपयोगकर्ता:2

  • मारप्लो
  • मारियस


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL डेटाबेस से php/html तालिका में डेटा प्रदर्शित करें

  2. WHERE क्लॉज में CASE का उपयोग करना

  3. क्या मैं तैयार कथन में तालिका के नाम को माप सकता हूं?

  4. PHP का उपयोग करके एक्सेल या सीएसवी को MySQL डेटा बेस पर कैसे अपलोड करें?

  5. उल्लंघन अद्वितीय बाधा के कारण mysql अद्यतन/सम्मिलन विफलता का पता लगाएं