Ten days to learn PHP (3)

Source: Internet
Author: User


Seventh Day
Objective: To learn how to use a 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 what we mentioned above to see a login. Program To determine whether the user name and password are correct.
The login form is as follows: Login. php
<Table width = "100%" Height = "100%" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr>
<Form action = "checklogin. PHP "method =" Post "> <TD align =" center "valign =" Middle "> <Table width =" 400 "border =" 0 "cellpadding =" 5 "cellspacing = "1" class = "tablebg">
<Tr class = "tdbg">
<TD colspan = "2"> <Div align = "center"> administrators login </div> </TD>
</Tr>
<Tr class = "tdbg">
<TD> <Div align = "center"> username </div> </TD>
<TD> <Div align = "center">
<Input name = "username" type = "text" id = "username">
</Div> </TD>
</Tr>
<Tr class = "tdbg">
<TD> <Div align = "center"> password </div> </TD>
<TD> <Div align = "center">
<Input name = "password" type = "password" id = "password">
</Div> </TD>
</Tr>
<Tr class = "tdbg">
<TD colspan = "2"> <Div align = "center">
<Input type = "Submit" name = "Submit" value = "Submit">
<Input type = "reset" name = "submit2" value = "clear">
</Div> </TD>
</Tr>
</Table> </TD> </form>
</Tr>
</Table>

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.
Daily
Objective: To create a paging 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:
<A href = "XXX. php? Page = 0 "> firstpage </a>
<A href = "XXX. php? Page = <? = ($ Page-1)?> "> Prevpage </a>
<A href = "XXX. php? Page = <? = ($ Page + 1)?> "> Nextpage </a>
<A href = "XXX. php? Page = <? = Ceil ($ num/10)-1?> "> Lastpage </a>

This is a general idea. You can think about how to optimize it? Today, let's talk about some important issues tomorrow.
Day 9
Objective: To learn more

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 <input type = "file" name = "file">
Next let's take a look.Code:

$ 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 learned PHP in ten days. My three seriesArticleI use "ten days to learn" as the name. I want to tell you that ASP, PHP, and ASP. NET can all be used for ten days, but it is never a ten-day master. You still 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 <input type = "file" name = "file">
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.

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.