Ten days to learn php (3) _ PHP Tutorial

Source: Internet
Author: User
Ten days to learn php (3 ). The seventh day of learning objective: to learn how to use a SESSION has many functions, most of which is to pass variables between pages on the site. At the beginning of the page, we want to start session_start (); enable the SESSION; and the seventh day of learning objective: to learn how to use the SESSION

SESSION has many functions, most of which are the variable transfer between pages on the site. At the beginning of the page, we want session_start (); to enable SESSION;
Then you can use the SESSION variable. for example, the value to be assigned is $ _ SESSION ['item'] = "item1 "; the value to be obtained is $ item1 = $ _ SESSION ['item'];, which is simple. Here we may use some functions. for example, to determine whether a SESSION variable is null, we can write: empty ($ _ SESSION ['inum']) and return true or false.

Next, let's take a look at the above description to check whether the user name and password are correct.
The login form is as follows: login. php






This is the case when processing files.
Require_once ('Conn. php ');
Session_start ();
$ Username = $ _ POST ['username'];
$ Password = $ _ POST ['password'];
$ Exec = "select * from admin where username = '". $ username ."'";
If ($ result = mysql_query ($ exec ))
{
If ($ rs = mysql_fetch_object ($ result ))
{
If ($ rs-> password = $ password)
{
$ _ SESSION ['adminname'] = $ username;
Header ("location: index. php ");
}
Else
{
Echo "script alert ('password Check Error! '); Location. href = 'login. php'; script ";
}
}
Else
{
Echo "script alert ('username Check Error! '); Location. href = 'login. php'; script ";
}
}
Else
{
Echo "script" alert ('database Connection Error! '); Location. href = 'login. php'; script ";
}?>

Conn. php is like this:
$ Conn = mysql_connect ("127.0.0.1 ","","");
Mysql_select_db ("shop ");
?>

Because $ _ SESSION ['adminname'] = $ username; we can write a file to verify whether the statement is logged on: checkadmin. asp.
Session_start ();
If ($ _ SESSION ['adminname'] = '')
{
Echo "script" alert ('Please Login First '); location. href = 'login. php'; script ";
}
?>

Let's talk about how to create a page tomorrow. Objective: to create a page display

The key is to use the limit in the SQL statement to limit the number of records displayed from several to several. We need a variable $ page for recording the current page, and the total number of records $ num

For $ page, if it does not exist, let it = 0. if there is <0, let it ALSO = 0. if it exceeds the total number of pages, let it = the total number of pages.

$ Execc = "select count (*) from tablename ";
$ Resultc = mysql_query ($ execc );
$ Rsc = mysql_fetch_array ($ resultc );
$ Num = $ rsc [0];

In this way, the total number of records can be obtained.
Ceil ($ num/10) if a page contains 10 records, this is the total number of pages.

So you can write it like this.
If (empty ($ _ GET ['Page'])
{
$ Page = 0;
}
Else
{
$ Page = $ _ GET ['Page'];
If ($ page <0) $ page = 0;
If ($ page> = ceil ($ num/10) $ page = ceil ($ num/10)-1; // because page starts from 0, so-1
}

In this way, $ exec can write $ exec = "select * from tablename limit". ($ page * 10). ", 10 ";
// One page is 10 records

What we need to do at last is several connections:
FirstPage
"> PrevPage
"> NextPage
"> LastPage

This is a general idea. you can think about how to optimize it? Today, let's talk about some important issues tomorrow.
Things to note

Because I learned ASP first, I will find many places to adapt when I do PHP again.

1. do not miss a semicolon
2. do not miss the $
3. do not omit session_start () when using SESSION ();

If an error occurs, you can use the following methods:
1. if an error occurs in an SQL statement, comment out and output the SQL statement. Note that you must also annotate the subsequent execution of the SQL statement.
2. if the variable is empty, most of them are not passed in place. check the output variable and check the form id and name.
3. if a database connection error occurs, check whether my SQL is correctly opened and whether the connection statements are omitted.
4. pay attention to indentation and eliminate the error of unpartitioned brackets.

When creating a large website, my idea is to first build a database to determine the role of each field and the relationship between the table. Then design the background interface, starting from adding data, because the addition is successful or not, it can be verified directly in the database, the page for adding and displaying is completed, and finally the combination of the two. In general, the background includes adding, deleting, modifying, and displaying. there is no problem in the background, and there is no major problem in the foreground. The front-end also needs to pay attention to security and fault tolerance, as well as the output format.

Now, let's talk about uploading files and sending emails in PHP tomorrow.
Day 10 objective: to learn how to use PHP to upload files and send emails

Enctype = "multipart/form-data" must be added to the file upload form"
And
Let's take a look at the code below:

$ F = & $ HTTP_POST_FILES ['file'];
$ Dest_dir = 'uploads'; // sets the upload directory.
$ Dest = $ dest_dir. '/'. date ("ymd "). "_". $ f ['name']; // Here I set the file name to date plus the file name to avoid duplication.
$ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );
Chmod ($ dest, 0755); // sets the attributes of the uploaded file

The uploaded file name is date ("ymd "). "_". $ f ['name'] can be used later when it is inserted into the database. PHP actually moves the files you uploaded from the temporary directory to the specified directory. Move_uploaded_file ($ f ['tmp _ name'], $ dest );

Mail is simpler. you can use the mail () function.

Mail ("recipient address", "topic", "body", "From: sender \ r \ nReply-to: sender address ");

However, mail () requires the support of the server. in WINDOWS, you also need to configure the SMTP server. Generally, the LINUX space outside will work.
It seems that uploading files and sending emails are much simpler than ASP. you only need to call the function. ASP also needs to use different server components such as FSO and JMAIL.

I have learned PHP for ten days. my three series of articles use "ten days of study" as their names. I want to tell you ASP, PHP, and ASP. NET can be started for ten days, but proficient is by no means ten days. you need to study it on your own.
Objective: to learn how to use PHP to upload files and send emails

Enctype = "multipart/form-data" must be added to the file upload form"
And
Let's take a look at the code below:

$ F = & $ HTTP_POST_FILES ['file'];
$ Dest_dir = 'uploads'; // sets the upload directory.
$ Dest = $ dest_dir. '/'. date ("ymd "). "_". $ f ['name']; // Here I set the file name to date plus the file name to avoid duplication.
$ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );
Chmod ($ dest, 0755); // sets the attributes of the uploaded file

The uploaded file name is date ("ymd "). "_". $ f ['name'] can be used later when it is inserted into the database. PHP actually moves the files you uploaded from the temporary directory to the specified directory. Move_uploaded_file ($ f ['tmp _ name'], $ dest );

Mail is simpler. you can use the mail () function.

Mail ("recipient address", "topic", "body", "From: sender \ r \ nReply-to: sender address ");

However, mail () requires the support of the server. in WINDOWS, you also need to configure the SMTP server. Generally, the LINUX space outside will work.
It seems that uploading files and sending emails are much simpler than ASP. you only need to call the function. ASP also needs to use different server components such as FSO and JMAIL.

I have learned PHP for ten days. my three series of articles use "ten days of study" as their names. I want to tell you ASP, PHP, and ASP. NET can be started for ten days, but proficient is by no means ten days. you need to study it on your own.

The reset SESSION has many functions, most of which are the variable transfer between pages on the site. Start session_start () on the page...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.