Sunday, May 19, 2013

Brushing up on php

Well, I still haven't written any articles for that articleport blog yet (I need $$$ motivation) but I will eventually.  As procrastination though I'm going to be brushing up on my php tonight and the next few nights (depending on what I finish today) through phpacademy: https://phpacademy.org/.  Today's notes will be posted below...

Tutorial one - unique hit counter | Part1: http://www.youtube.com/watch?v=BFv6wa7Xgh8

  • They're starting off telling you they're going to make a hit counter (keeping track of ips) but it's keeping the ips in a text file which is bandwidth wasting and moronic compared to using a db.  I'll go along with it for now though to learn the php file methods they'll be teaching.
  • This tutorial uses an index.php, ip.txt and a counter.txt.
  • Now I've declared two vars $filename and $ip_filename and assigned them to 'counter.txt' and 'ip.txt'.
  • Next I declared a function and declared the two vars as global inside of them to make it "global to the function to use it inside".  I'm not sure I'd have to do this and I think this is unnecessary but who am I to judge?  I'll test that out later.
  • Then above this and in the function, I'm making an $ip var and assigning it $_SERVER['REMOTEADDR'] which is the users ip.
  • Next we are creating $current_value = () ? : ;
    • In () goes the condition to be evaluated.
    • After the ? goes the code for if the condition is true.
    • After the : goes the code for if the condition is false.
  • $current_value = (file_exists($filename)) ? file_get_contents($filename) : 0 ;
    • file_exists obviously checks if counter.txt exists and file_get_contents obviously grabs the text from it if it exists which should be a number else it returns 0.
  • file_put_contents($filename, ++$current_value);
    • Adds 1 to the value retrieved from counter.txt.
The video is over and the current code is: http://pastebin.com/JzFbHgcr  Upon running it, the code works and 1 is added to counter.txt every reload.

Tutorial one - unique hit counter | Part2: http://www.youtube.com/watch?v=xkJI3TNQ9uk
  • For video 2, as you might have guessed, the goal is to check for the ip in the ip.txt and add it if it's not there and add one to the counter.  If it is already there it will do nothing.
  • To start off, they wrapped the $current_value and file_put_contents in: if(!in_array($ip, file($ip_filename, FILE_IGNORE_NEW_LINES))) { }
    • in_array checks if a string is in an array.  With the ! in front it's saying if it isn't in the array then do whatever.
    • $ip will be the users ip to check in the ip.txt text array.
    • file() retrieves a text array and FILE_IGNORE_NEW_LINES will prevent white spaces in this array.
  • Then they insert, file_put_contents($ip_filename, $ip."\n", FILE_APPEND);, in the loop.
    • This adds the ip + a newline to the file.  FILE_APPEND makes sure you append to the file and not rewrite it like you do with the counter.txt.
  • lol then the guy in the video figures out he spelled "ignore" wrong.  Tired people these days...
Anyways when he runs it again it works so the final code is: http://pastebin.com/DWJHbGXG

I'm done with tutorials for today (I know it was just one but summarizing is hard work for a lazy person).  I did all this in a new folder on my localhost (I use wampserver).  This taught me a couple cool methods for future use but nothing amazing.  Those include:
The rest of tonight I'm just derping around my usual forums.  Post more about this tomorrow if no one responds to my free psd to html service still.

No comments:

Post a Comment