Home >Backend Development >PHP Tutorial >The seventh day of learning PHP in ten days_PHP tutorial

The seventh day of learning PHP in ten days_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:08:13737browse

Learning purpose: Learn how to use SESSION

SESSION has many functions, the most commonly used one is the transfer of variables between pages within the site. At the beginning of the page, we need session_start(); to turn on SESSION;
Then you can use the SESSION variable. For example, to assign a value: $_SESSION['item']="item1"; to get the value, $item1=$_SESSION ['item'];, very simple. Here we may use some functions, for example, to determine whether a SESSION variable is empty, you can write: empty($_SESSION['inum']) returns true or false.

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


















< ;div align="center">Administrators Login
< div align="center">Username


< ;div align="center">Password





This is how the file is processed
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" );
?>

Since $_SESSION['adminname']=$username; we can write the file to verify the login statement like this: checkadmin.asp
session_start ();
if($_SESSION['adminname']=='')
{
echo "<script>alert('Please Login First');location.href='login.php ';</script>";
}
?>

Haha, that’s it for today, let’s talk about how to make a paging tomorrow.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314888.htmlTechArticleLearning purpose: Learn how to use SESSION. SESSION has many functions. The most commonly used one is the transfer of variables between pages within the site. At the beginning of the page we need session_start(); to open SESSION; and then you can...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn