search
HomeBackend DevelopmentPHP TutorialConversation on the couch (3)_PHP tutorial
Conversation on the couch (3)_PHP tutorialJul 13, 2016 pm 05:19 PM
threesuperiorthingBasiccomplexdialoguemarketgameDemopointNowRealityofKnow

Market Game

Now that you know the basics, let’s get a little more complex and demonstrate a real-life session application. Let's assume for a moment that you have a financial portal that allows its user to select four stocks and then displays the current market price of each stock and displays on each page the user's progress on the site.

In the following example, we assume that the user has been authenticated and logged in to the site. We used a MySQL database, which has a table named user_info, which is used to store the user's four stocks and unique user name. Once a session is initialized, we register variables to hold the username and four stocks, then connect to the database to retrieve the values ​​and display them on the page.

The code is as follows:

$#@60;?php

//Initialize a session session_start();

//Register session variable

//User name
session_register (username);

//Selected stock variables
session_register(stock1);
session_register(stock2);
session_register(stock3);
session_register(stock4);

//Connect to MySQL
$db = mysql_connect("someserver.com", "tom", "jones");

//Select database
mysql_select_db("stock_db", $db);

//Use SQL to query the database
$query = "select stock_pref1,stock_pref2,stock_pref3,stock_pref4
from user_info where username=$username";

$ result = mysql_query($query,$db);

//Get the stock code from the database and assign it to the session variable
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 has many session-related functions - most of which require no explanation, so they are listed below.

session_destroy(): Release all session data (very useful when a user logs out of a site, you need to release all variables created during his visit).

session_name():Set or read the name of the current session.

session_id():Set or read the id value of the current session.
session_unregister(session_variable_name):Unregister a variable from a particular session.

session_is_registered(): Check whether a session variable has been registered.

For example:

$#@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():Encode session data into a string or decode a string into session data.

Here you may use them:

$#@60;?php

session_start();

session_register(someString);
$someString="I hate cats!";

//Compile all session variables into a string
$sessStr = session_encode();

//You can see it here
echo $sessStr;

echo " $#@60;br$#@62;$#@60;br$#@62;";

//Replace with dogs where cats appear
$sessStr = ereg_replace(" cats","dogs",$sessStr);

//The session variable is updated after decoding
session_decode($sessStr);

//Display $sessstr again
echo $someString;

?$#@62;

Finally, before starting PHPLIB, there is a technical issue you should be aware of - all the examples above use cookies to save the session id value on the client side. But what happens if the user's browser is set to reject cookies?

If this happens, a session id needs to be passed from one page to another by embedding it in the URL. For example: $#@60;a href="http://www.someserver.com/admin/preferences.php3?PHPSESSID=$#@60;? echo "$PHPSESSID"; ?$#@62;"$# @62;Edit Your Portfolio! $#@60;/a$#@62;

This can ensure that the session variable is valid in subsequent pages.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532662.htmlTechArticleMarket Game Now that you know the basics, let’s get a little more complicated and demonstrate a real-life session application. Let's say you have a financial portal that allows...
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
如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)Dec 31, 2023 pm 05:15 PM

技嘉的主板怎么设置键盘开机首先,要支持键盘开机,一定是PS2键盘!!设置步骤如下:第一步:开机按Del或者F2进入bios,到bios的Advanced(高级)模式普通主板默认进入主板的EZ(简易)模式,需要按F7切换到高级模式,ROG系列主板默认进入bios的高级模式(我们用简体中文来示范)第二步:选择到——【高级】——【高级电源管理(APM)】第三步:找到选项【由PS2键盘唤醒】第四步:这个选项默认是Disabled(关闭)的,下拉之后可以看到三种不同的设置选择,分别是按【空格键】开机、按组

CS玩家的首选:推荐的电脑配置CS玩家的首选:推荐的电脑配置Jan 02, 2024 pm 04:26 PM

1.处理器在选择电脑配置时,处理器是至关重要的组件之一。对于玩CS这样的游戏来说,处理器的性能直接影响游戏的流畅度和反应速度。推荐选择IntelCorei5或i7系列的处理器,因为它们具有强大的多核处理能力和高频率,可以轻松应对CS的高要求。2.显卡显卡是游戏性能的重要因素之一。对于射击游戏如CS而言,显卡的性能直接影响游戏画面的清晰度和流畅度。建议选择NVIDIAGeForceGTX系列或AMDRadeonRX系列的显卡,它们具备出色的图形处理能力和高帧率输出,能够提供更好的游戏体验3.内存电

主板上的数字音频输出接口-SPDIF OUT主板上的数字音频输出接口-SPDIF OUTJan 14, 2024 pm 04:42 PM

主板上SPDIFOUT连接线序最近我遇到了一个问题,就是关于电线的接线顺序。我上网查了一下,有些资料说1、2、4对应的是out、+5V、接地;而另一些资料则说1、2、4对应的是out、接地、+5V。最好的办法是查看你的主板说明书,如果找不到说明书,你可以使用万用表进行测量。首先找到接地,然后就可以确定其他的接线顺序了。主板vdg怎么接线连接主板的VDG接线时,您需要将VGA连接线的一端插入显示器的VGA接口,另一端插入电脑的显卡VGA接口。请注意,不要将其插入主板的VGA接口。完成连接后,您可以

广联达软件电脑配置推荐;广联达软件对电脑的配置要求广联达软件电脑配置推荐;广联达软件对电脑的配置要求Jan 01, 2024 pm 12:52 PM

广联达软件是一家专注于建筑信息化领域的软件公司,其产品被广泛应用于建筑设计、施工、运营等各个环节。由于广联达软件功能复杂、数据量大,对电脑的配置要求较高。本文将从多个方面详细阐述广联达软件的电脑配置推荐,以帮助读者选择适合的电脑配置处理器广联达软件在进行建筑设计、模拟等操作时,需要进行大量的数据计算和处理,因此对处理器的要求较高。推荐选择多核心、高主频的处理器,如英特尔i7系列或AMDRyzen系列。这些处理器具有较强的计算能力和多线程处理能力,能够更好地满足广联达软件的需求。内存内存是影响计算

电脑游戏下载到d盘还是c盘电脑游戏下载到d盘还是c盘Mar 16, 2023 pm 03:02 PM

电脑游戏下载到d盘。C盘是系统盘,是专门为安装系统而设置的磁盘空间,里面安装的东西越少越好;C盘安装的东西多,电脑就会很卡。C盘系统运行会产生很多缓存与磁盘碎片,这些都会影响系统的运行及速度;如果再安装游戏或者软件,会更加加速缓存与碎片产生的数量与速度。

我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次Jan 07, 2024 am 10:06 AM

我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次去徒步多就60升或以上的徒步少就60升以下的全程都坐车就不用背包,旅行箱更方便,真要随身带东西,弄个25~40升的就绰绰有馀西藏旅游必备用品:太阳镜、太阳帽、防晒霜、护肤霜、润唇膏、长袖上衣、毛衣;对于特殊旅游或去阿里、藏北、川藏线旅游,建议带:睡袋(防寒)、床单(防脏)、羽绒服、旅游鞋或登山鞋、拖鞋、牙刷、牙膏、毛巾、卷筒纸、纸内裤、消毒湿巾、手电筒、防水火柴、刀具、绳子。前运包能装电脑吗能装电脑,有些背包有

请推荐一款性价比较高的1155针CPU请推荐一款性价比较高的1155针CPUJan 14, 2024 pm 01:30 PM

求推荐1155针的cpu哪个最好当前性能最高的1155针CPU是IntelCorei7-3770K。它拥有4个核心和8个线程,基础频率为3.5GHz,并支持TurboBoost2.0技术,最高可达到3.9GHz。此外,它还搭载了8MB的三级缓存,是一款非常出色的处理器LGA1155针最强的CPUIntel酷睿i73770K。LGA1155接口为二三代酷睿处理器所使用的接口类型,性能最好的为Intel酷睿i73770K,这款处理器参数如下:1.适用类型:台式机;2.CPU系列:酷睿i7;3.CPU

如何把电脑的百兆网口升级为千兆网口?如何把电脑的百兆网口升级为千兆网口?Dec 31, 2023 pm 05:32 PM

电脑100兆网口怎么变成千兆的要将电脑的100兆网口升级为千兆网口,一般需要按照以下步骤进行操作:1.确认网卡是否支持千兆网速:首先需要确认电脑上的网卡是否支持千兆以太网,如果不支持的话就无法实现将100兆网口提速至千兆。2.更换网线:要实现千兆网速,需要使用Cat5e或更高规格的网线,因为Cat5e网线可以支持千兆以太网,而对于100兆以太网来说则只需使用Cat5网线即可。3.更改网卡驱动程序:如果您的网卡支持千兆以太网,则需要更新网卡的驱动程序。可以前往网卡制造商的官方网站,下载并安装最新的

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft