Tuesday, June 26, 2012

PHP Learning - How to Create Password Protected Page

Most often, visitors are required to enter username and password to view a page. You can create user auth either with HTTP and Apache or specify in PHP code. Today, let's take a look how to with HTTP and Apache.

You use instructions to Apache, called directives, to set up your authentication application. Directives allow you to specify the files that are password-protected and the valid user names and IDs. The simplest way to use Apache directives for authentication is to create a file called .htaccess

For example, I want to protect my folder called "user". Thus I will create a file called .htaccess. in that file, I specifies the following lines:

AuthUserFile c:\secret\.htpass

AuthGroupFile /dev/null

AuthName Accounting Department

AuthType Basic

Require valid-user

then in the Windows command prompt, i can type

c:\Apache\bin\htpasswd -c c:\secret\.htpass jeff

to create a username for Jeff (in order to do this, you need to go to your httpd.txt file and change "AllowOverride None" to "AllowOverride Authconfig")

Another way to create a page that requires user name and password is through using Http Auth in PHP. Unlike Http auth with Apache, Http auth with PHP have username and password stored in database, in my example, i will use Mysql (since it is my favorite database)

First, I want to create a database, called "Jefftest"

Create database Jefftest;

Then create a table in the database

CREATE TABLE User (

user_name CHAR(10) NOT NULL,

password CHAR(255) NOT NULL,

create_date DATE NOT NULL,

PRIMARY KEY(user_name) );

Then you can access the database from your PHP script with the MySQL functions that are built into PHP. In here, i won't tell you how to access Mysql by using php built-in function (I might in the future) since it is fairly complex to php beginners. But at least you got the idea of how this function is achieved by using HTTP with the apache server.


View the original article here

PHP Learning - Working with Data(4)

PHP provides many built-in functions for manipulating strings. When you want to remove the leading or trailing spaces, you can use the following statement:
$string = trim($string) ; # removes leading & trailing spaces
$string = ltrim($string) ; # removes leading spaces
$string = rtrim($string) ; # removes trailing spaces

PHP can also help you split a string into words. The general
form of this function is as follows:
str_word_count(“string”,format);
In this expression, format can be 1, meaning return the words as a numeric
array; or 2, meaning return the words as an array where the key is the position
of the first character of the word. It may be confusing to you at this point, I will explain array in detail in the future of my php lesson.
To give you an example here:
$string = “Counting Words”;
$number of words = str_word_count($string);
$word1 = str_word_count($string,1);
$word2 = str_word_count($string,2);

if we echo $word1, we get:
$word1[0] = Counting
$word1[1] = Words
if we echo $word2, we get:
$word2[0] = Counting
$word2[9] = Words


View the original article here

PHP Learning-Work With Constants

Constants are similiar to variables, it is created by using "define" statement, such as:
define ("Yao Ming","Best NBA Center");
a number can also be defined
define("interest", .02);

To display constants, we can use "print_r" or "echo" statement
print_r(Yao Ming);
you will get "Best NBA Center"

Constants are really easy to understand, I will talk about Data in the next php learning session. (Emm, may be you are wondering why today's lesson is so short...... Take it easy, buddy, we will go through this step by step)


View the original article here

PHP Learning basics-PHP variables(1)

Basically, variables in php are containers that holds information. Variables starts with $, for example, you can name your variable $first_name, $last_name.... Be careful, variables can not start with a number, thus $4name would not be allowed in php as variable.
Some examples of variables:
$price=4;
$name="Jeff";
Something worth to notice here, when you assign a variable to a number, you don't need include "", however, when you assign variable to a string(I will explain later as we go through php in my blog), you either have to use "" or '', and a ; is always followed after you name the variable

To display a variable value, you can use "echo" or "print_r", for example:
$popsinger="Jeff Chang";
echo $popsinger ;(or print_r ($popsinger);)
For either of the command, you will get result "Jeff".
Of course, you can use "echo" command print more results once, looking at the example below:
$first_name="Jeff";
$last_name="Chang";
echo "Pop singer is", "$first_name," ", $last_name;
The result will be: Pop singer is Jeff Chang.

A little more complicated example:
$web="web";
echo "take a look at this $website";
What do we do now? we noticed that the variable "$website" does not even exist here, to tell php that we want to display website, we can use something like
echo "take a look at this {$web}site; Then you will get the desired result(It is just an example to show you something, of course i know i can just use $website="website", lol)

Look at this:
$name="NBA player";
$name="Yao Ming"
ok, why there is $ here?? if you look carefully, actually, we are creating a new variable here, which is $NBA player="Yao Ming"

To remove a variable, you can use: unset (variablename), such as unset($name), and you can unset more than one at a time, use "," to separate the variable name, such as unset($name, $NBAplayer)
Isn't that easy?? Emm... php language is not scary at all.... I think it is enough for today, we will have fun as we go through php.


View the original article here

PHP learning - Working With Data(1)

In previous lesson, we have learned variables. In php, variables can store data in different types. The different types of data are:
Interger: A whole number (no fractions), such as 10, 0, -43Floating point number: A number that include decimal places, such as 5.36, 4.232Character string: A series of single characters, such as "good", "hello"Boolean: a true or false valuePhp is smarter than you think, it evaluate the variable you stored and will assign an approriate data type to the variable. For example:
$number=2 ; # php store it as interger
$sports="football" #php store it as character string

You can also tell php the type of data you want to store, look at the following example:
$number=(int) $var1; #you are telling php to store $var1 as interger
$sports=(string)$var2; #you are telling php to store $var2 as character string

To find out the type of a variable, use var_dump:
var_dump($var1) #you are asking php what type of data is variable1

PHP provides a shortcut for adding 1 to a variable, for example:
$number=$number+1; can be rewrite as $number++;
$number=$number-1; can be rewrite as $number--;
Other forms of shortcut includes:
$number+=3;
$number-=2;
$number*=4;
$number/=5;


View the original article here

PHP Learning - Working with Data(2)

Use build-in function for simple math calculations:
To compute a square root: $number=sqrt(91);
Round up to the next interger $number=ceil(27.63);
There are a lot of function help you with calculations, I will not list them here

Usually, you will want to control how to display the number, then the statement number_format comes into play.
number_format(number,decimals,"decimalsep","thousandsep)
number is the number to be formatted. This must always be included.

decimals is the number of decimal places. If decimals is not included,
the number of decimal places is 0 by default.

decimalsep is the character used to separate the decimal places. The
default is a decimal point. If you include this, you must also include
thousandsep.

thousandsep is the character used to separate the number into thousands.
The default is a comma. If you include this parameter, you must
also include decimalsep.

For more complicated formatting, you can use the statement "printf" and "sprintf".


View the original article here

PHP Learning - View and Modify Arrays

In my last lesson, I talked about how to create arrays, and we are going to continue today by starting with the topic of how to view arrays in php, then we will look into how to modify arrays in php.
You can see the structure and values of any array by using one of two
statements — var_dump or print_r. The print_r() statement, however,
gives somewhat less information. To display the $studying_day array, use
the following statement:
print_r($studying_day); and you will get the output:
Array
(
[1] => english
[2] => french
[3] => math
[4] => business
)
To get a more detailed information other than the key and value, you can use:
var_dump($studying_day);

array(3) {
[1]=>
string(7) “english”
[2]=>
string(6) “french”
[3]=>
string(4) “math”
[4]=>
string(8)"business"
}
Arrays can be changed at any time in the script, just as variables can.
Let's say I am sick of english(actually I am), thus i decided not to study english anymore, then I can use the following statement to remove it from my previously made array:
$studying_day[1] = " "; # this set the value to empty, but it does not remove
unset(studying_day[1]); # this statement completely remove it from the group
Sort Array:
One of the most useful features of arrays is that PHP can sort them for you.
To sort my array, I can use the statement: sort($studying_day);
$studying_day[1]="english";
$studying_day[2]="french";
$study_day[3]="math";
$study_day[4]="business";
To sort array that contains word as key, we use asort($arrayname); for example, we have a new array:
$computer[cpu]="AMD";
$computer[mainboard]="ESC";
$computer[keyboard]="BENQ";
$computer[screen]="LCD";
when I use asort($computer), I get the following results:
$computer[cpu]="AMD";
$computer[keyboard]="BENQ";
$computer[mainboard]="ESC";
$computer[screen]="LCD";
Some other useful sorting include:
rsort($arrayname) #Sorts by value in reverse order; assigns new numbersas the keys.
ksort($arrayname) #Sorts by key.
krsort($arrayname) #Sorts by key in reverse order.
usort($arrayname, function) #Sorts by a function.


View the original article here