Php logon and logout instance code

Source: Internet
Author: User

Here we have pm_user, which is a data table without creating tables. You can create a table by yourself. Next we will only introduce the program code for logging on with php and then logging out. If you need it, please refer to it.

Login.htm

The Code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>

<Body>
<Form id = "form1" name = "form1" method = "post" action = "">
<P>
<Label for = "user"> </label>
<Input type = "text" name = "user" id = "user"/>
</P>
<P>
<Label for = "pwd"> </label>
<Input type = "text" name = "pwd" id = "pwd"/>
</P>
<P>
<Input type = "submit" name = "button" id = "button" value = "submit"/>
</P>
</Form>
</Body>
</Html>


Login. php

The Code is as follows: Copy code

Function showPage () // determine whether to log on without logon. directly jump to the logon page.
{
If (! Isset ($ _ SESSION ["user"]) &! Isset ($ _ SESSION ["pwd"])
{
If (isset ($ _ COOKIE ["user"]) & isset ($ _ COOKIE ["pwd"])
{
$ SQL = "select * from pm_user where u_user = '$ _ COOKIE [user]'";
$ Query = mysql_query ($ SQL );
$ IsUser = is_array ($ row = mysql_fetch_array ($ query); // judge the user name
$ IsPwd = $ isUser? $ Row ["u_pwd"] === _ COOKIE ["pwd"]: false; // determine the password
If ($ isPwd)
{
$ _ SESSION ["user"] = $ _ COOKIE ["user"];
$ _ SESSION ["pwd"] = $ _ COOKIE ["pwd"];
$ _ SESSION ["id"] = $ _ COOKIE ["id"];
$ _ SESSION ["name"] = $ _ COOKIE ["name"];
}
}
}
 
If (! Isset ($ _ SESSION ["user"]) &! Isset ($ _ SESSION ["pwd"])
{
Echo '<script> alert ("You have not logged on! Returning to the login page... "); location. href =" index. php "; </script> ';
Exit ();
}
}

Log out

Out. php

The Code is as follows: Copy code

Function loginOut () // logout function
{
If (isset ($ _ GET ["login"]) = 'out ')
{
$ _ SESSION ["user"] = '';
$ _ SESSION ["pwd"] = '';
$ _ SESSION ["id"] = '';
$ _ SESSION ["name"] = '';
Session_destroy ();
Echo '<script> alert ("exited successfully! "); Location. href =" index. php "; </script> ';
}
}

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.