In this tutorial, we will learn how to check the uptime of a Linux/Unix system. Linux and Unix systems allow one to easily check how long the system has been up for. The command which does this is called uptime.
The uptime for a system is very important especially in a server environment where maximum availability is an important requirement. Using the uptime command will help you keep an eye on how well your servers are running and helps you to ensure that they are not rebooting or crashing unnecessarily.
In this PHP tutorial, you will learn how to convert a PHP string to int. Depending on what calculation you will be doing with a variable, it may be important that it is of the particular type of integer.
For example, suppose you were processing (from a submitted form) a vote which was from 1 to 10; you would ideally want this data stored in a numeric variable rather than as a string which the $_GET or $_POST array would provide it to you as.
Converting a string to int in PHP is quite easy using casting as we will see below.
PHP comes with a dedicated configuration setting for the timezone that it is supposed to use. On both Windows and Linux this setting usually comes blank and PHP defaults to using the system timezone when doing calculations using the date function for example.
On both Windows and Linux systems, the configuration to set the timezone is in the main php.ini file.
Managing the date and time on a Linux system is fairly simple using the command line. Once you are familiar with the relevant command and its syntax, it is very easy to get and set the system date/time properly.
The commands shown below are tested on Ubuntu Linux but should work well on most if not all other popular variants of Linux.
The following tutorial explains.
To get the current Ubuntu system date/time, execute the following command in a terminal:
There are various ways of setting the timezone that the MySQL server uses. To explicitly set the timezone that MySQL uses on Ubuntu, all we have to do is add a line to the MySQL configuration file.
Note that it is usually not necessary to explicitly set the timezone for the MySQL server as MySQL is pretty good at getting the correct timezone from the system. Use the method outlined below only if you have special requirements.
Open a terminal, and run the following command as root:
PHP array size is important when we will be manipulating the contents of our array. By virtue of us using an array to store our variables in the first place, it usually means that we plan to operate on them as a unit.
Since we usually use for loops to process arrays, we need to know the array size so that we can know when to end the loop.
The following tutorial on the PHP array size explains.
By using virtual host configurations, it is possible for more than one website to be served by the same Apache web server on the same machine. Virtual hosting makes it easy to run multiple websites from the same server. You simply make a new virtual host configuration for each new website that you want to set up and then reload the Apache configuration.
It is very easy to change the timezone on your Ubuntu desktop or server system when using the command line. Debian-based versions of Linux, like Ubuntu, make this task as easy as reconfiguring a package via the package management system. To change the timezone on your Ubuntu system, execute the following in a terminal as root:
The package tzdata should already be installed but in case it isn't just install it by running the following command as root:
apt-get install tzdata
PHP random numbers and strings are easily generated using the PHP rand function. This function can be made to generate pseudo-random numbers within certain boundaries based on the arguments passed to it. Have a look at our PHP random tutorial below.
Note: We might use the term "random" throughout this tutorial, but kindly note that what the 'rand' function generates is pseudo-random numbers.
The PHP mail function is what we use if we want to send email using a PHP script. This function is appropriately named mail, and when invoked with the right parameters, it allows us to send an email fairly easily.
For the PHP mail function to work properly, PHP itself must be configured to use a mail transfer agent. We will look more at that below. Right now let's get into the syntax of the PHP mail function.
The syntax for the simplest form of the PHP mail function is as follows:
In this PHP tutorial, we will learn how to use PHP to delete a directory and any files which may be contained in the directory. The rmdir command is used to tell PHP to remove a directory, but this only works on empty directories. If we want to remove a directory which contains subdirectories (or subfolders if you like) and files in these subdirectories we need to write our own function which gets a bit more involved.
In this tutorial we will look at how to find out which shell is currently being used. Let's say that you just got shell access on an Ubuntu Linux box or some other box with a Linux distribution. The first thing you should want to know is what shell you are using. This is because different shells offer different features. You may well come to realise that if you are not using a shell that you are familiar with, you may be less productive.
To find out which shell you are currently using, type the following command:
On the internet, referrer refers to the page that contains the link that was followed to get to the current page. The referrer is set in the HTTP request behind the scenes with usually no intervention from the user.
The referrer is not always set by browsers when it should be, and may even be modified (or "spoofed") by the user, so it is not something that can be trusted.
The PHP memory limit will need to changed sooner or later as we start writing and running more resource intensive scripts. The idea of a memory limit is important on a web server since the server may be serving many requests at a given time, and we wouldn't want any one request or script consuming too many resources. Also, it is useful to limit how much memory a script can consume in a setting (like a VPS) where resources like RAM are limited.
Suppose you want to know how much of your website traffic comes from Google or another search engine. It's very easy if you use Awstats or Google analytics, but what if you haven't configured these tools? Using only the web server log files and some shell commands will enable us to quickly parse log files and give a count of how many referrals we had from a search engine.
For the purposes of this tutorial, we will be assuming an Apache web server, and that the search engine that we are counting the referrals from is Google.
Google recently launched an instant previews feature in Google webmaster tools. This is a very impressive feature which is undoubtedly useful to webmasters. Instant previews allows webmasters to easily determine if Google is having difficulty in crawling their pages. The reasoning behind it is that if what users see is different from what Googlebot sees, then it might indicate an issue with crawling the website.
In this tutorial, we will look at how to change file permissions in PHP. File permissions specify to what extent users are allowed to interact with a file.
These interactions are usually reading the file, writing to the file, and executing the file. Users may also be denied all access to a file.
The PHP chmod function is used to change the permissions of a file. It's syntax is given below:
chmod ($filename, $permissions);
Outputting a variable as part of a string in PHP using double-quotes is straightforward. We all know that when strings are placed in double-quotes, the PHP interpreter examines them to see if there are any variables inside which we intend to output.
This is a very useful feature as it saves us from having to concatenate the variable to the string as in some other languages.
For example, this is how we would normally output a variable as part of a string in PHP:
<?php $name = 'Sally'; echo "I love $name"; ?>
The result would be: