国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial PHP Session 變量的使用方法詳解與范例代碼(轉(zhuǎn))

PHP Session 變量的使用方法詳解與范例代碼(轉(zhuǎn))

Jun 13, 2016 pm 12:08 PM
gt login lt php session

PHP Session 變量的使用方法詳解與實(shí)例代碼(轉(zhuǎn))


? ? ? ? 在php中Session經(jīng)常用來(lái)驗(yàn)證用戶注冊(cè)或登錄之后的驗(yàn)證了,下面我來(lái)總結(jié)session變量的一些常用實(shí)例與用法介紹
?
?

當(dāng)您運(yùn)行一個(gè)應(yīng)用程序時(shí),您會(huì)打開(kāi)它,做些更改,然后關(guān)閉它。這很像一次會(huì)話。計(jì)算機(jī)清楚你是誰(shuí)。它知道你何時(shí)啟動(dòng)應(yīng)用程序,并在何時(shí)終止。但是在因特網(wǎng)上,存在一個(gè)問(wèn)題:服務(wù)器不知道你是誰(shuí)以及你做什么,這是由于 HTTP 地址不能維持狀態(tài)。
通過(guò)在服務(wù)器上存儲(chǔ)用戶信息以便隨后使用,PHP session 解決了這個(gè)問(wèn)題(比如用戶名稱、購(gòu)買(mǎi)商品等)。不過(guò),會(huì)話信息是臨時(shí)的,在用戶離開(kāi)網(wǎng)站后將被刪除。如果您需要永久儲(chǔ)存信息,可以把數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫(kù)中。

把手冊(cè)抄一下,然后每個(gè)都試試然后寫(xiě)出來(lái),方便自己查閱滴,誰(shuí)讓咱剛學(xué)呢。Session大概有12個(gè)函數(shù)分別是:

session_start: 初始 session。
session_destroy: 結(jié)束 session。
session_unset: 釋放session內(nèi)存。
session_name: 存取目前 session 名稱。
session_module_name: 存取目前 session 模塊。
session_save_path: 存取目前 session 路徑。
session_id: 存取目前 session 代號(hào)。
session_register: 注冊(cè)新的變量。
session_unregister: 刪除已注冊(cè)變量。
session_is_registered: 檢查變量是否注冊(cè)。
session_decode: Session 資料解碼。
session_encode: Session 資料編碼。

還有個(gè)全局變量就是:$_SESSION


在您把用戶信息存儲(chǔ)到 PHP session 中之前,首先必須啟動(dòng)會(huì)話。
注釋:session_start() 函數(shù)必須位于 標(biāo)簽之前:

?

復(fù)制代碼代碼如下:








?

?

存儲(chǔ) Session 變量

?

復(fù)制代碼代碼如下:

session_start();
// store session data
$_SESSION['views']=1;
?>?



//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>



?[html]

?

終結(jié) Session
unset() 函數(shù)用于釋放指定的 session 變量:

[code]
unset($_SESSION['views']);
?>
?

?

您也可以通過(guò) session_destroy() 函數(shù)徹底終結(jié) session:

?

復(fù)制代碼代碼如下:

session_destroy();
?>
?

?

實(shí)例:

?

復(fù)制代碼代碼如下:

session_start();?
switch ( $_GET['action'] ){?
case "loginif";?
//登陸驗(yàn)證,假定session儲(chǔ)存的秘密應(yīng)該等于123才為正確?
if ($_SESSION['pass']=="123"){echo "密碼正確 您可以執(zhí)行注銷(xiāo)";}else{echo "密碼錯(cuò)誤,您可以重新登陸";}?
break;?
case "logout";?
//注銷(xiāo)登陸?
session_unset();?
session_destroy();?
echo "注銷(xiāo)成功!可以判斷一下密碼是否正確來(lái)看看是不是成功注銷(xiāo)";?
break;?
case "login";?
//寫(xiě)入session以供驗(yàn)證,?
$pass="123";//密碼?
$_SESSION['pass']=$pass;?
echo "寫(xiě)入登陸密碼了 去判斷密碼成功與否吧。";?
break;?
}?
?>?

假定本頁(yè)名為temp.php

?

用戶進(jìn)行登陸post,程序處理寫(xiě)入session

?

判斷用戶密碼是否正確

?

登陸成功的用戶注銷(xiāo)登陸


?

?

我總結(jié)了一下php中session的用法。

(一)開(kāi)始session?
  在每一次使用session之前,都要加上這一句:“session_start();”。顧名思義,這個(gè)函數(shù)的作用就是開(kāi)始使用session。?
(二)注冊(cè)session?
  首先要建立一個(gè)global(注意,一定要定義為global,不然在其它頁(yè)面用不了)數(shù)組,如$login,其中$login['name']="Victor",$login['pwd']="111111",然后調(diào)用函數(shù)“session_register(login);”,session就成功注冊(cè)了。?
(三)使用session里面的變量?
  和注冊(cè)session類似,都要先建立一個(gè)global數(shù)組,然后就和使用一般數(shù)組一樣了。?
(四)判斷session是否注冊(cè)?
  很簡(jiǎn)單,用“if (session_is_registered(login))”判斷就可以了。?
(五)卸載session?
  也很簡(jiǎn)單,“session_unregister(login);”就可以了。?
  注意:在進(jìn)行(二)(三)(四)(五)之前一定要先進(jìn)行(一)。


下面給出一個(gè)例子:

index.htm

?

復(fù)制代碼代碼如下:

?
?
測(cè)試?
?
?
?
用戶名:
?
密碼:
?
?
?
?

?

?

login.php

?

復(fù)制代碼代碼如下:

global $login;?
if ($_POST['name']!="Victor" || $_POST['pwd']!="111111")?
{?
??????? echo "登陸失敗";?
??????? echo "請(qǐng)返回";?
??????? exit;?
}?
$login = array('name'=>$_POST['name'],?
?????????????????????????? 'pwd'=>$_POST['pwd']);?
session_start();?
session_register(login);?
echo "查看信息
";?
echo "退出登陸
";?
?>
?

?

info.php

?

復(fù)制代碼代碼如下:

session_start();?
if (session_is_registered(login))?
{?
??????? global $login;?
??????? echo "hello,".$login['name']."
";?
??????? echo "退出登陸
";?
}?
else?
{?
??????? echo "非法操作
";?
??????? exit;?
}?
?>

?


logout.php

?

復(fù)制代碼代碼如下:

session_start();?
session_unregister(login);?
header("location:index.htm");?
?>
?

?

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hello, PHP! Hello, PHP! Jul 18, 2025 am 04:35 AM

Of course it is useful. PHP still plays an important role in web development, especially for CMS and e-commerce platforms. 1. The threshold for PHP is low, the syntax is intuitive, and suitable for beginners to get started; 2. It is widely used in dynamic web development, such as user login, form submission and other functions; 3. It can build an API interface, which is more efficient with the Laravel framework; 4. It supports CMS customization, such as WordPress plug-in and theme development; 5. It can write automated task scripts, such as data crawling and report generation; 6. Learning suggestions include mastering basic syntax, combining database exercise projects, using mainstream frameworks, focusing on security defense and referring to open source projects. PHP is highly practical and has a mature ecological environment, and is still widely used in small and medium-sized projects.

What Not to Comment in PHP What Not to Comment in PHP Jul 18, 2025 am 04:37 AM

Annotations should be avoided in PHP development: 1. Do not comment sensitive information, such as database passwords or API keys, because these will still be exposed. It is recommended to use environment variables and add .gitignore; 2. Avoid outdated or incorrect comments. When modifying the code, you need to update the comments simultaneously to ensure the description is accurate; 3. Do not write meaningless comments. If you add redundant instructions to simple assignment statements, comments should be added in complex logic; 4. Do not replace PHPDoc document comments with ordinary comments. It is recommended to use standard formats to support IDE prompts and maintain structured information.

Install PHP and Apache Install PHP and Apache Jul 18, 2025 am 04:38 AM

To run the PHP website on your local computer, you need to install PHP and Apache. Windows users download Apache and configure httpd.conf, then download PHP and modify the configuration file to load the PHP module; macOS/Linux users can use Homebrew or apt command to install. Finally, create an info.php file in the root directory of the website and test whether it is successful.

Writing Effective PHP Comments Writing Effective PHP Comments Jul 18, 2025 am 04:44 AM

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

Simple PHP Setup Guide Simple PHP Setup Guide Jul 18, 2025 am 04:47 AM

PHP is suitable for beginners to quickly build local development environments. Use integrated tools such as XAMPP, WAMP or MAMP to install Apache, MySQL and PHP in one click. The project files can be accessed through localhost by putting them in the htdocs directory; 1. Download and install integrated environment tools; 2. Put the project files into the htdocs directory; 3. Browser access corresponding paths to test and run; you can also install PHP separately and configure environment variables, run php-Slocalhost:8000 through the command line to start the built-in server for quick debugging; create a new index.php and write an echo statement to output content, and add variables and condition judgment to experience logical processing capabilities. The key to getting started with PHP is to do it by hand.

PHP Control Structures: If/Else PHP Control Structures: If/Else Jul 18, 2025 am 04:02 AM

When using if/else control structure for conditional judgment in PHP, the following points should be followed: 1. Use if/else when different code blocks need to be executed according to the conditions; 2. Execute if branches if the condition is true, enter else or elseif if they are false; 3. When multi-conditional judgment, elseif should be arranged in logical order, and the range should be placed in front of the front; 4. Avoid too deep nesting, it is recommended to consider switch or reconstruction above three layers; 5. Always use curly braces {} to improve readability; 6. Pay attention to Boolean conversion issues to prevent type misjudgment; 7. Use ternary operators to simplify the code in simple conditions; 8. Merge and repeat judgments to reduce redundancy; 9. Test boundary values to ensure the complete logic. Mastering these techniques can help improve code quality and stability.

Getting Started with PHP Installation Getting Started with PHP Installation Jul 18, 2025 am 04:06 AM

The key to installing PHP is to clarify the usage scenario and system environment. 1. Determine the operating system and PHP version: Windows can use XAMPP or WAMP, macOS recommends Homebrew, Linux is installed through apt/yum, and it is recommended to choose version 8.1 or 8.2. 2. Local development environment construction: XAMPP, MAMP or Docker is recommended. You can also install it with Homebrew and verify it with php-v. 3. Server installation PHP: Taking Ubuntu as an example, execute aptupdate and installation commands and restart Apache/Nginx, and run through info.php test. 4. Frequently asked questions: Pay attention to extension activation, consistent path, permission settings and

what is undefined index in PHP what is undefined index in PHP Jul 18, 2025 am 04:07 AM

The reasons and ways to avoid the occurrence of "undefinedindex" error: 1. The reasons include accessing GET/POST parameters that are not passed, array logic errors, variable index not set, etc.; 2. Solutions include using isset() to check whether the key exists, using array_key_exists() to judge, and using the empty merge operator to provide default values; 3. Development suggestions include unified parameter verification, enabling debugging prompts, viewing data structures, and using IDE inspection functions.

See all articles