10 Day Learning PHP (3) _php Foundation

Source: Internet
Author: User
Tags chmod learn php

The Seventh day study goal: learns the session the use

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

Here is a comprehensive look at a login program to determine whether the user name password is correct.
The landing form is this: 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" >
&LT;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" >
&LT;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>

Working with 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 "<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 this to verify that the file is logged: checkadmin.asp
?
Session_Start ();
if ($_session[' adminname ']== ')
{
echo "<script>alert (' please Login a '); location.href= ' login.php ';</script>";
}
?>

Oh, today said here, tomorrow say how to get a pagination. The eighth day study purpose: Make a paging display

The key is to use the limit in the SQL statement to qualify the displayed records from several to several. 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 get the total number of records
Ceil ($num/10) If a page 10 records, this is the total number of pages

So you can write that.
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 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:
<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, we can think how to optimize? Today, let's talk about some of the issues of attention tomorrow.
The Nineth Day study goal: attention matters

Because I was first to learn ASP, so do PHP when you will find that many places need to adapt.

1, pay attention not to miss the semicolon
2, pay attention not to miss the variable before the $
3, use the time of session to pay attention not to omit session_start ();

If an error occurs, you can use the following methods:
1, if it is an error in the SQL statement, then the output of the SQL statement, note that you also want to comment on the subsequent execution of the SQL statement
2, if the variable is empty, most of it is not passed in place, output variables check, check the ID and name of the form
3, if the database connection error, check to open my SQL correctly and whether the connection statement is missing
4, pay attention to indentation, excluding the parentheses do not match the error

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

All right, here we are today. If you use PHP to upload files and send emails.
Learning Goals: Learn how to use PHP to upload files and send emails

Upload file form must add Enctype= "Multipart/form-data"
and <input type= "file" name= "file" >
Let's take 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 filename 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 the database, PHP actually moves the file you uploaded from the temporary directory to the specified directory. Move_uploaded_file ($f [' Tmp_name '], $dest); That's the key.

It's easier to send mail, you can 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, in 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 you call a function. ASP also needs to use different components of the server such as FSO, JMail and so on.

10 days Learn PHP said here, my three series of articles are used "ten days" as the name, want to tell everyone is asp,php,asp. NET entry can be 10 days, but mastery is by no means 10 days, but also need everyone to study themselves.
Learning Purpose: Learn to upload files and email in PHP

Upload file form must add Enctype= "Multipart/form-data"
and <input type= "file" name= "file" >
Let's take 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 filename 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 the database, PHP actually moves the file you uploaded from the temporary directory to the specified directory. Move_uploaded_file ($f [' Tmp_name '], $dest); That's the key.

It's easier to send mail, you can 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, in 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 you call a function. ASP also needs to use different components of the server such as FSO, JMail and so on.

10 days Learn PHP said here, my three series of articles are used "ten days" as the name, want to tell everyone is asp,php,asp. NET entry can be 10 days, but mastery is by no means 10 days, but also need everyone to study themselves.

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.