Category Archives: php

How to Get User IP Address in PHP

How to Get User IP Address in PHP

There may be many occasions we need to get user IP address to track user’s activity. In PHP, you can simply get the visitor’s IP address using the $_SERVER[‘REMOTE_ADDR’] variable.

  • $_SERVER[‘REMOTE_ADDR’] – It Returns the IP address of the current user.

Example:-

But, $_SERVER[‘REMOTE_ADDR’] may not always return the true IP address of the client. If user is connected to the Internet through Proxy Server then $_SERVER[‘REMOTE_ADDR’] in PHP just returns the the IP address of the proxy server not of the client’s machine. So here, I have create a simple function in PHP to find the real IP address of the visitor’s machine.

Simple PHP function to get client IP address

PHP Variable

Variable is a identified or name that used to store a value. Variables are used for storing values such as numeric values, characters, character strings, or memory addresses so that they can be used in any part of the program.

Guideline for variable in PHP:

  • Variables name must start with a letter or underscore “_”.
  • Variables in PHP are represented by a dollar sign followed by the name of the variable.
  • Variable name can only contain alpha-numeric characters and underscores.
  • Spaces are not allowed in variable name.
  • PHP variable name is case-sensitive
  • PHP is a loosely type language

Declaring PHP variables:

 

PHP is a loosely type language

In language such as C, C++ and Java the programmer must declare the name and type of the variable before use it. In PHP the type of the variable does not need to be declared before use it, because types are associated with values rather than variables. As a result a variable can change the type of its value as much as we want.

PHP variable variables

if you want the name your variable is a variable itself? In PHP, you have Variable Variables, so you may assign a variable to another variable.

 

PHP Regular Expression

PHP Regular Expression are sequence of characters used for the pattern-matching functionality.Using regular expression you can find a particular string in another string, Replace one string by another string using regular exp. and you can split a string into many parts.PHP offers verity of functions under pattern matching functionality.

There are 2 types of regular expressions:

  • POSIX Extended
  • Perl Compatible

The ereg, eregi, … are the POSIX versions and preg_match, preg_replace, … are the Perl version. It is important that using Perl compatible regular expressions the expression should be enclosed in the delimiters, a forward slash (/), for example. However this version is more powerful and faster as well than the POSIX one.

The regular expressions basic syntax

To use regular expressions first you need to learn the syntax of the patterns. We can group the characters inside a pattern like this:

  • Normal characters which match themselves like hello
  • Start and end indicators as ^ and $
  • Count indicators like +,*,?
  • Logical operator like |
  • Grouping with {},(),[]

An example pattern to check valid emails looks like this:

Function Description
ereg_replace() The ereg_replace() function finds for string specified by pattern and replaces pattern with replacement if found.
eregi_replace() The eregi_replace() function works similar to ereg_replace(), except that the search for pattern in string is not case sensitive.
preg_replace() The preg_replace() function works similar to ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.
preg_match() The preg_match() function finds string of a pattern and returns true if pattern matches false otherwise.
Expression Description
[0-9] It matches any decimal digit from 0 through 9.
[a-z] It matches any character from lowercase a through lowercase z.
[A-Z] It matches any character from uppercase A through uppercase Z.
[a-Z] It matches any character from lowercase a through uppercase Z.
p+ It matches any string containing at least one p.
p* It matches any string containing zero or more p’s.
p? It matches any string containing zero or more p’s. This is just an alternative way to use p*.
p{N} It matches any string containing a sequence of N p’s
p{2,3} It matches any string containing a sequence of two or three p’s.
p{2, } It matches any string containing a sequence of at least two p’s.
p$ It matches any string with p at the end of it.
^p It matches any string with p at the beginning of it.
[^a-zA-Z] It matches any string not containing any of the characters ranging from a through z and A through Z.
p.p It matches any string containing p, followed by any character, in turn followed by another p.
^.{2}$ It matches any string containing exactly two characters.
<b>(.*)</b> It matches any string enclosed within <b> and </b>.
p(hp)* It matches any string containing a p followed by zero or more instances of the sequence hp.

PHP Date & Time

PHP Date() Function

PHP date() function returns a formatted string representing a date.

Syntax:

Parameter Description
format Required. Specifies the format of the timestamp. d – Represents the day of the month (01 to 31) ,m – Represents a month (01 to 12),Y – Represents a year (in four digits)
timestamp Optional. Specifies a timestamp. Default is the current date and time

Example1:

Example2:

Example3:

This function provides many possible formatting options. You can also customize the output from the format string. Following is a full list of the formatting options available:

a “am” or “pm”
A “AM” or “PM”
B Swatch Internet time
d day of the month, 2 digits with leading zeros; i.e. “01” to “31”
D day of the week, textual, 3 letters; i.e. “Fri”
F month, textual, long; i.e. “January”
g hour, 12-hour format without leading zeros; i.e. “1” to “12”
G hour, 24-hour format without leading zeros; i.e. “0” to “23”
h hour, 12-hour format; i.e. “01” to “12”
H hour, 24-hour format; i.e. “00” to “23”
i minutes; i.e. “00” to “59”
I (capital i) “1” if Daylight Savings Time, “0” otherwise
j day of the month without leading zeros; i.e. “1” to “31”
l (lowercase ‘L’) day of the week, textual, long; i.e. “Friday”
L boolean for whether it is a leap year; i.e. “0” or “1”
m month; i.e. “01” to “12”
M month, textual, 3 letters; i.e. “Jan”
n month without leading zeros; i.e. “1” to “12”
r RFC 822 formatted date; i.e. “Thu, 21 Dec 2000 16:01:07 +0200” (added in PHP 4.0.4)
s seconds; i.e. “00” to “59”
S English ordinal suffix, textual, 2 characters; i.e. “th”, “nd”
t number of days in the given month; i.e. “28” to “31”
T Timezone setting of this machine; i.e. “MDT”
U seconds since the epoch
w day of the week, numeric, i.e. “0” (Sunday) to “6” (Saturday)
Y year, 4 digits; i.e. “1999”
y year, 2 digits; i.e. “99”
z day of the year; i.e. “0” to “365”
Z timezone offset in seconds (i.e. “-43200” to “43200”).

PHP time() Function

PHP time() function gives information about the current date and time. It requires no parameters but returns an integer.The integer returned by time() represents the number of seconds elapsed since midnight GMT on January 1, 1970.

Example:

PHP Require

The require() Function

PHP Include

The include() Function

PHP Foreach Loop

The foreach Loop

The foreach statement is used to iterate over all elements of an array. It executes same piece of code for every element of an array

 

PHP Do…While Loop

The do…while loop statement

The do…while statement will execute a block of code and then test the condition for next iteration and executes next only if condition is true, block of code executes at least once.

PHP For Loop

The for loop statement

The for loop is used when we want to execute block of code known times.

Property Description
initialization Sets a counter variable
condition It test the each loop iteration for a condition. If it returns TRUE, the loop continues. If it returns FALSE, the loop ends.
increment Increment counter variable

PHP While Loops

The while loop statement

The while loop will execute a block of statement as long as a test expression is true.

PHP Switch Statement

The Switch Statement

The switch statement is simplified form of the nested if … else if statement , it avoids long blocks of if..elseif..else code.

 

PHP If…Else

The If…Else Statement

When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.