Php-mysql Course

The counterUrl() function presented in this page can be used to make a counter for the number of visits of your web site pages, with data (URL of each accessed page, number of visits) stored into a text file on server, in JSON format.
- The function returns an array with: ['p'=>page_address, 'v'=>nr_visits]. For other details and usage, see the comments in code.
- Click to select it.
/* PHP Script Counter Page Visits (PHP 5.4+) /from: https://coursesweb.net/
- Counter data is saved in JSON format into a text file on server
- Create a file on your server with the name added in $file_json, and permissions CHMOD 0755 (or 0777)
*/

// HERE add the path and Name of the file where to save the counter data
$file_json = 'count_url.json';

//function to get and save a Counter for accessed pages
//receives the page address, and the address of the file where to save data (in json format)
//returns array with: ['p':page_address, 'v':nr_visits]
function counterUrl($p, $f){
  $url_c = ['p'=>$p, 'v'=>0];  //counter of current accessed page

  //if $file_json exists, gets is data into an array
  $ar_c = file_exists($f) ? json_decode(file_get_contents($f), true) :[];

  //if array $ar_c, traverse it, checks if current $pg_url is saved and increments its counter
  if(is_array($ar_c)){
    $nr = count($ar_c);
    for($i=0; $i<$nr; $i++){
      if($ar_c[$i]['p'] == $url_c['p']) {
        //store counter of current url, remove this item from array
        $url_c['v'] = $ar_c[$i]['v'];
        unset($ar_c[$i]);
        $ar_c = array_values($ar_c);
        break;
      }
    }
  }

  //increment counter and save data in json file (show error message if unable to save)
  $url_c['v']++;
  $ar_c[] = $url_c;
  if(!file_put_contents($f, json_encode($ar_c))) echo 'Unable to save data in: '. $f;
  return $url_c;
}

//Usage of the function, output the current page-address (without domain name) and number of visits
$url_c = counterUrl($_SERVER['REQUEST_URI'], $file_json);
echo 'Page: '. $url_c['p'] .' - Visits: '. $url_c['v'];
// Output: Page: /php-mysql/counter-page-visits_cs - Visits: 89

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
Counter Page Visits

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142308)
  2. Get and Modify content of an Iframe (32367)
  3. $_GET, $_POST and $_REQUEST Variables (33884)
  4. Ajax-PHP Chat Script (49508)
  5. JavaScript Course - Free lessons (31647)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (311)
  2. The Mastery of Love (48)
  3. CSS cursor property - Custom Cursors (36)
  4. Read Excel file data in PHP - PhpExcelReader (35)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (31)