10-Day Learning PHP (3) _php tutorial

Source: Internet
Author: User
Seventh Day Study Purpose: Learn the use of the session

The role of the session is many, the most used is the site within the variable transfer of the page. At the beginning of the page we want to session_start (); open session;
Then you can use the SESSION variable, for example to assign a value is: $_session[' item ']= "Item1", to get the value is $item1=$_session[' item '; Here we may use some functions, such as determining if a SESSION variable is empty, so write: Empty ($_session[' Inum ') returns TRUE or false.

Let's take a look at a login procedure to determine if the username and password are correct.
The login form is like this: login.php






Processing files is like this
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 "";
}
}
Else
{
echo "";
}
}
Else
{
echo "";
}?>

Conn.php is like this:
$conn =mysql_connect ("127.0.0.1", "", "" ");
mysql_select_db ("Shop");
?>

Since $_session[' AdminName ']= $username; we can write the file that verifies whether the login statement: checkadmin.asp
Session_Start ();
if ($_session[' adminname ']== ')
{
echo "";
}
?>

Oh, today said here, tomorrow to say how to get a paging. Eighth Day Study Purpose: do a pagination display

The key is to use the limit in the SQL statement to limit the number of records displayed to a few. We need a variable $page that records the current page and a total number of records $num

For $page if not we let it = 0, if there is <0 let it also = 0, if more than the total number of pages let him = total number of pages.

$EXECC = "SELECT count (*) from TableName";
$RESULTC =mysql_query ($EXECC);
$RSC =mysql_fetch_array ($RESULTC);
$num = $RSC [0];

This will give you the total number of records
Ceil ($num/10)) If a page of 10 records, this is the total number of pages

So I can write 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 the page is starting from 0, so to 1
}

So $exec can write $exec = "SELECT * FROM tablename limit". ($page *10). ", 10";
One page is 10 records.

The last thing we need to do is a couple of connections:
FirstPage
">prevpage
">nextpage
">lastpage

This is a general idea, you can think how to optimize? Speaking here today, let's talk about some of the issues of attention tomorrow.
Nineth Day Study Purpose: Precautions

Because I learned ASP first, so when I do PHP will find a lot of places need to adapt.

1, attention do not miss the semicolon
2. Be careful not to miss the $ before the variable
3, use the session when the attention do not miss Session_Start ();

If an error occurs, you can use the following methods:
1, if the SQL statement error, comments and then output the SQL statement, note also to annotate the subsequent execution of the SQL statement
2, if the variable is empty, most of it is not passed in place, the output variable check, check the form ID and name
3. If there is a database connection error, check if my SQL is open correctly and if the connection statement is missing
4, pay attention to indentation, excluding brackets mismatch error

In the big Web site, my idea is to first build a database, to determine the role of each field, and the relationship between tables. Then design the background interface, starting from the addition of data, because the success of the add can be directly into the database validation, do a good job of adding and displaying the page, and finally the combination of the two. In general, the background will include the addition of delete changes and display, backstage no problem, the front desk is not a big problem. The front desk also needs to be aware of security and fault tolerance and the output format.

All right, today I'll talk about it. If you are uploading files and sending emails in PHP.
Learning Purpose: Learn to upload files and send emails in PHP

Upload file form must be added enctype= "Multipart/form-data"
And
Here's a look at the code:

$f =& $HTTP _post_files[' file ';
$dest _dir= ' uploads ';//Set upload directory
$dest = $dest _dir. '/'. Date ("Ymd"). " _ ". $f [' name '];//me here Set file name to date plus file name to avoid duplication
$r =move_uploaded_file ($f [' Tmp_name '], $dest);
chmod ($dest, 0755);//Set the properties of the uploaded file

The uploaded file name is date ("Ymd"). " _ ". $f [' name '], which can be used later when inserting into a database, PHP is actually moving your uploaded files from the temporary directory to the specified directory. Move_uploaded_file ($f [' Tmp_name '], $dest); This is the key.

As for sending e-mail, it is easier to use the mail () function

Mail ("Recipient address", "subject", "Body", "From: Sender \r\nreply-to: Sender's address");

However, mail () requires the support of the server, under Windows also need to configure the SMTP server, generally outside the Linux space is OK.
It seems that uploading files and sending emails is much simpler than ASP, as long as the functions are called. The ASP also needs to use the different components of the server, such as FSO, JMail, etc.

10 days to learn PHP said here, my three series of articles are used "Ten days to learn" as the name, want to tell everyone is asp,php,asp. NET entry can be 10 days, but mastery is never 10 days ah, but also needs to be researched by everyone.
Learning Purpose: Learn to upload files and send emails in PHP

Upload file form must be added enctype= "Multipart/form-data"
And
Here's a look at the code:

$f =& $HTTP _post_files[' file ';
$dest _dir= ' uploads ';//Set upload directory
$dest = $dest _dir. '/'. Date ("Ymd"). " _ ". $f [' name '];//me here Set file name to date plus file name to avoid duplication
$r =move_uploaded_file ($f [' Tmp_name '], $dest);
chmod ($dest, 0755);//Set the properties of the uploaded file

The uploaded file name is date ("Ymd"). " _ ". $f [' name '], which can be used later when inserting into a database, PHP is actually moving your uploaded files from the temporary directory to the specified directory. Move_uploaded_file ($f [' Tmp_name '], $dest); This is the key.

As for sending e-mail, it is easier to use the mail () function

Mail ("Recipient address", "subject", "Body", "From: Sender \r\nreply-to: Sender's address");

However, mail () requires the support of the server, under Windows also need to configure the SMTP server, generally outside the Linux space is OK.
It seems that uploading files and sending emails is much simpler than ASP, as long as the functions are called. The ASP also needs to use the different components of the server, such as FSO, JMail, etc.

10 days to learn PHP said here, my three series of articles are used "Ten days to learn" as the name, want to tell everyone is asp,php,asp. NET entry can be 10 days, but mastery is never 10 days ah, but also needs to be researched by everyone.

http://www.bkjia.com/PHPjc/314611.html www.bkjia.com true http://www.bkjia.com/PHPjc/314611.html techarticle The seventh day of learning purposes: the session of the use of the session of the role of many, the most used is the site of the variable transfer between pages. At the beginning of the page we want to session_start (), open the session;

  • 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.