PHP section--session three ways to use

Source: Internet
Author: User

I. Displaying the user's information between different pages

Two. Control Login

1. Login Page

<body>
<form action= "loginchuli.php" method= "POST" >
<div> Username: <input type= "text" name= "UID"/></div>
<div> Password: <input type= "password" name= "pwd"/></div>
<input type= "Submit" value= "Login"/>
</form>
</body>

2. Login Processing page

<?php
Session_Start ();
Include ("DBDA.class.php");
$db = new Dbda ();

$uid = $_post["UID"];
$pwd = $_post["pwd"];

$sql = "Select pwd from users where uid= ' {$uid} '"; Password is determined by user name
$attr = $db->query ($sql);

$mm = $attr [0][0]; Remove password

if (!empty ($pwd) && $pwd = = $mm)
{
$_session["UID"] = $uid; If the login is successful, put the username in the session to save, jump to the main page
Header ("location:main.php");
}
Else
{
echo "Login failed!";
}

3. Main Page

<?php
Session_Start ();


if (Empty ($_session["UID"]))//Determine if the SESSION is stored in the value, if there is no storage, let it jump to the login interface
{
Header ("location:login.php");
Exit
}

$uid = $_session["UID"]; If you can fetch the user name from the session, the following information is displayed
Include ("DBDA.class.php");
$db = new Dbda ();
$sql = "Select name from users where uid= ' {$uid} '";
$attr = $db->query ($sql);

?>
<body>
<?php
echo "
?>

Third, do shopping cart

1. Information on fruit table

<body>
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> name </td>
<td> Price </td>
<td> Origin </td>
<td> Inventory </td>
<td> Operations </td>
</tr>
<?php
Include ("DBDA.class.php");
$db = new Dbda ();

$sql = "SELECT * from Fruit";
$attr = $db->query ($sql);

foreach ($attr as $v)
{
echo "<tr>
<td>{$v [1]}</td>
<td>{$v [2]}</td>
<td>{$v [3]}</td>
<td>{$v [4]}</td>
<td><a href= ' jia.php?code={$v [0]} ' > Plus shopping cart </a></td>//Add Shopping cart options
</tr> ";
}


?>
</table>

2. Add the fruit to your cart

Three lines of thought:

1. If it is the first time to click on the shopping cart, do a two-dimensional array thrown into the session

2. If it is not the first time you click Add Shopping cart and the fruit is first clicked, make a one-dimensional array of the fruit and throw it into the two-dimensional array of the session

3. If it is not the first time you click on the cart and the fruit is not the first click, add 1 to the number of the fruit in the session array




<?php
Session_Start ();

$code = $_get["code"];

if (Empty ($_session["GWC"]))
{
1. If it is the first time to click on the shopping cart, do a two-dimensional array thrown into the session
$attr = Array (
Array ($code, 1)//Create a two-dimensional array and add the codename and number 1 in an array inside.
);

$_session["GWC"] = $attr; Add the newly created two-dimensional array to the session
}
Else
{
Determine if the fruit designator appears in the session array
$attr = $_session["GWC"]; If there is content in the session, give the content to a two-dimensional array
if (Panduan ($code, $attr))//See Below a Panduan method to determine if the code name is already present in the session
{
3. If it is not the first time you click on the cart and the fruit is not the first click, add 1 to the number of the fruit in the session array
/*foreach ($attr as $v)
{
if ($code = = $v [0])
{
$v [1] = $v [1]+1;
}
}*/
for ($i =0; $i <count ($attr); $i + +)//Take the length of the array with count ()
{
if ($code = = $attr [$i][0])
{
$attr [$i][1]++;
}
}
$_session["GWC"]= $attr;
}
Else
{
2. If it is not the first time you click Add Shopping cart and the fruit is first clicked, make a one-dimensional array of the fruit and throw it into the two-dimensional array of the session
$arr = Array ($code, 1);
$attr [] = $arr;
$_session["GWC"]= $attr;
}

}


Var_dump ($_session["GWC"]);

Determine if V is present in arr
function Panduan ($v, $arr)//Judging method
{
$n = 0;
foreach ($arr as $a)
{
if ($v = = $a [0])
{
$n + +;
}
}

if ($n ==0)
{
return false;
}
Else
{
return true;
}
}

PHP section--session three ways to use

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.