长沙发上的对话(三)
市场游戏
现在你已经知道了基本的东西,再来点复杂的吧,演示一个现实生活中的session 应用。让我们假设一下,你有一个金融门户,它允许它的用户选择四支股票,然后显示每支股票的当前市场价格,并且在每一页上显示用户在这个站点上的过程。
在下面的例子中,我们假定用户已经被验证过身份并且登录到站点上来了。我们使用了MySQL 数据库,里面有一个名为user_info的表,用于存储用户的四支股票和唯一用户名。一旦一个session被初始化了,我们注册变量保存用户名和四支股票,然后同数据库进行连接,取回值并显示在页面上。
代码如下:
$#@60;?php
//初始化一个session session_start();
//注册session变量
//用户名
session_register(username);
//选择的股票变量
session_register(stock1);
session_register(stock2);
session_register(stock3);
session_register(stock4);
//连接MySQL
$db = mysql_connect("someserver.com", "tom", "jones");
//选择数据库
mysql_select_db("stock_db", $db);
//使用SQL查询数据库
$query = "select stock_pref1,stock_pref2,stock_pref3,stock_pref4
from user_info where username=$username";
$result = mysql_query($query,$db);
//从数据库中取股票代码,并赋值给session变量
list($stock1,$stock2,$stock3,$stock4) = mysql_fetch_row($result);
echo "Hi $username!$#@60;br$#@62;";
echo "Your selected stocks are:$#@60;br$#@62;";
echo "$stock1$#@60;br$#@62;";
echo "$stock2$#@60;br$#@62;";
echo "$stock3$#@60;br$#@62;";
echo "$stock4$#@60;br$#@62;";
// code to generate rest of page
?$#@62;
PHP4有许多与session相关的函数 -- 大部分都不需要解释,把他们列在下面。
session_destroy(): 释放所有的session数据(当一个用户从一个站点注销时非常有用,你需要释放掉在他访问期间的所创建的所有变量)。
session_name():设置或读取当前session的名字。
session_id():设置或读取当前session的id值。
session_unregister(session_variable_name):从一个特别的session注销一个变量。
session_is_registered():检查是否一个session变量已经注册了。
例如:
$#@60;?php
session_start();
if(session_is_registered(username))
{
echo "A session variable by the name "username"
already exists";
}
else
{
echo "No variable named "username" registered yet.
Registering...";
session_register(username);
}
?$#@62;
session_encode() and session_decode():将session数据编码成字符串或将字符串解码成session数据。
在这里你可能使用他们:
$#@60;?php
session_start();
session_register(someString);
$someString="I hate cats!";
//将所有的session变量编成一个字符串
$sessStr = session_encode();
//可以在这里看到
echo $sessStr;
echo "$#@60;br$#@62;$#@60;br$#@62;";
//在出现cats的地方用dogs进行替换
$sessStr = ereg_replace("cats","dogs",$sessStr);
//解码后更新了session变量
session_decode($sessStr);
//再显示一次$sessstr
echo $someString;
?$#@62;
最后,在开始PHPLIB之前,有一个技术性问题你应该知道的 -- 上面所有的例子都使用了cookies在客户端来保存session id值。但是如果用户的浏览器被设成拒绝cookies会怎么样呢?
如果发生这种情况,那就需要将一个session id通过嵌在url中从一页传递到另一页上去。例如: $#@60;a href="http://www.someserver.com/admin/preferences.php3?PHPSESSID=$#@60;? echo "$PHPSESSID"; ?$#@62;"$#@62;Edit Your Portfolio! $#@60;/a$#@62;
这个就可以保证在后面的页面中session变量是有效的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



If Nvgpucomp64.dll is causing your game to crash frequently, the solutions provided here may help you. This problem is usually caused by outdated or corrupted graphics card drivers, corrupted game files, etc. Fixing these issues can help you deal with game crashes. The Nvgpucomp64.dll file is associated with NVIDIA graphics cards. When this file crashes, your game will crash too. This usually happens in games like LordsoftheFallen, LiesofP, RocketLeague, and ApexLegends. Nvgpucomp64.dll crashes games on Windows PC if N

In today's situation where almost all games are online, it is not advisable to ignore the optimization of home network. Almost all routers are equipped with NATBoost and QoS features designed to enhance users' gaming experience. This article will explore the definition, advantages and disadvantages of NATBoost and QoS. NATBoost vs. Qos for games; which one is better? NATBoost, also known as Network Address Translation Boost, is a feature built into routers that improves their performance. It's especially important for gaming because it helps reduce network latency, which is the time it takes for data to be transferred between the gaming device and the server. By optimizing the data processing method within the router, NATBoost achieves faster data processing speed and lower latency, thus changing the

According to news from this website on February 23, NVIDIA updated and launched the NVIDIA application last night, providing players with a new unified GPU control center, allowing players to capture wonderful moments through the powerful recording tool provided by the in-game floating window. In this update, NVIDIA also introduced the RTXHDR function. The official introduction is attached to this site: RTXHDR is a new AI-empowered Freestyle filter that can seamlessly introduce the gorgeous visual effects of high dynamic range (HDR) into In games that do not originally support HDR. All you need is an HDR-compatible monitor to use this feature with a wide range of DirectX and Vulkan-based games. After the player enables the RTXHDR function, the game will run even if it does not support HD

The superpeople game can be downloaded through the steam client. The size of this game is about 28G. It usually takes one and a half hours to download and install. Here is a specific download and installation tutorial for you! New method to apply for global closed testing 1) Search for "SUPERPEOPLE" in the Steam store (steam client download) 2) Click "Request access to SUPERPEOPLE closed testing" at the bottom of the "SUPERPEOPLE" store page 3) After clicking the request access button, The "SUPERPEOPLECBT" game can be confirmed in the Steam library 4) Click the install button in "SUPERPEOPLECBT" and download

Friends who have played enough AAA masterpieces and mobile games, do you want to relive the computer games of your childhood? Then let’s look for Spider Solitaire in Windows 11 together! Click the Start menu on the interface, click the "All Apps" button; click "All Apps". Find and select "MicrosoftSolitaireCollection", which is Microsoft's Solitaire series game application; Solitaire series game selection. After loading is complete, enter the selection interface and find "Spider Solitaire"; select "Spider Solitaire". Although the interface has changed slightly, it is still the same as before

According to news from this site on April 20, ASUS recently released a BIOS update, which improves instability such as crashes when running games on Intel's 13th/14th generation processors. This site previously reported that players reported problems including that when running the PC demo version of Bandai Namco's fighting game "Tekken 8", even if the computer has sufficient memory and video memory, the system will crash and prompt an error message indicating insufficient memory. Similar crashing issues have also appeared in many games such as "Battlefield 2042", "Remnant 2", "Fortnite", "Lord of the Fallen", "Hogwarts Legacy" and "The Finals". RAD published a long article in February this year, explaining that the game crash problem is a combination of BIOS settings, high clock frequency and high power consumption of Intel processors.

Various games can be purchased in the small black box, so how do you add the purchased games to Steam? Users need to activate the product on Steam in Steam, and then copy the activation code in the small black box to activate. This introduction to the method of storing purchased games into Steam will tell you the specific method. The following is a detailed introduction, hurry up Come and take a look! How to add games purchased from Little Black Box to Steam Answer: Activate the product on Steam in Steam to add it to the warehouse. Specific methods: 1. First, click the game button on Steam. 2. Click Activate product on Steam. 3. Then click Next in the window that appears. 4. Paste the purchase code in the small black box in the product activation. 5. Then click Next to add

There is no sound when playing games on iPad. Possible reasons. When using iPad for game entertainment, it is very frustrating to suddenly find that there is no sound. A silent gaming experience will undoubtedly reduce the fun significantly and make people feel confused and dissatisfied. So, when we encounter the problem of no sound when playing games on iPad, what are the possible reasons? This article will analyze and solve the possible causes of such problems. Volume settings are incorrect First, we need to check the iPad’s volume settings. Physical buttons on the side or top of the iPad can
