Jump to content
xisto Community
Sign in to follow this  
Saint_Michael

Some Basic Php Code Snippets For All Levels Of Experience

Recommended Posts

Most of the code snippets are usually used for community driven sites but they do give some general idea on how php works. Don't forget if your starting out php for the first time that when saving php files that you need to have the .php extension on your files or they will not work.

 

Display Browser info

 

This piece of code displays a user's browser info on how they are seeing the website

 

<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>

Actual Display-

Firefox - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3

 

Internet Explorer - Browser Info: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; InfoPath.1; .NET CLR 2.0.50727)

 

Opera - Opera/9.01 (Windows NT 5.1; U; en)

 

Netscape - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20060912 Netscape/8.1.2


Display IP Address

 

The next piece of code is easy to figure out, all it does is display the users current IP address and that is including using a fake IP number as well which is done by using another proxy or using a proxy site

 

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

Display Last Modify

 

With this piece of code, if you remeber back in the javascript craze that there was a piece of code that would display the last tiem the page has been updated, well the same concept but in PHP

 

<?//where the main page you want to display for modification$last_modified = filemtime("thepageyouwant.php");//print it all outprint("Page Last Edited");print(date("m/j/y h:i", $last_modified));?>

Display ISP host/IP number

 

With this piece of code it will display the ISP service you are using along with your IP number.

 

<?php$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);echo $hostname;?>

Actual Display-

c-75-68-74-121.hsd1.vt.comcast.net

Display Load Time

 

this piece of code shows a user how long it took a page to load, usually anything passed the decimal point is a bad thing, but usually hte coding is good enough that it get pretty low

<?php $load = microtime();print (number_format($load,2));?>

Update Copyright Date

 

I find this a interesting php script, which saves you about 30 seconds of updating all your pages when a new year comes, so with this little script all you do is put in your domain name and or any other info within that echo statement.

 

<?php$year = date("Y"); //defines the yearecho("Š $year yoursite.com"); //the echoing?>

Thats a few I have come across and actually bookmark from the many sites I have come across, the best part you don't have to make a major alterations to these code in order for them to work. Of course for those who have being coding in php for awhile then you will be able to adapt these in your php scripts.

Share this post


Link to post
Share on other sites

Hey man, thanky you for these snippets. It is nice to see something clean and simple, because usually these codes are long and complex so it is hard to understand anything if you are not a skilled programmer. I know the basics of php and this stuff is really helpful...Thanks again for that mate, Bye

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.