Pengekodan dan penyahkodan sesi PHP

WBOY
Lepaskan: 2024-02-28 08:56:01
ke hadapan
532 orang telah melayarinya

Editor PHP Zimo membawakan anda analisis terperinci pengekodan dan penyahkodan sesi PHP. Dalam pembangunan web, pengekodan dan penyahkodan sesi adalah bahagian yang sangat penting, yang melibatkan penghantaran dan penyimpanan data yang selamat. Pemahaman mendalam tentang mekanisme pengekodan dan penyahkodan sesi PHP boleh membantu pembangun melindungi data pengguna dengan lebih baik dan meningkatkan keselamatan tapak web. Artikel ini akan memperkenalkan prinsip, kaedah biasa dan kemahiran aplikasi praktikal pengekodan dan penyahkodan sesi untuk membantu pembaca memahami perkara teknikal pemprosesan sesi PHP dengan lebih baik.


Demo menggunakan sess<strong class="keylink">io</strong>n_encode() dan session_decodesess<strong class="keylink">io</strong>n_encode()session_decode dalam

php

session_encode()$_SESSION 数组数据序列化为字符串,然后 session_decode() Fungsi terbina dalam session_encode() menyerikan $_SESSION

array

data ke dalam

string

, dan kemudian session_decode() menukar sesi data sekali lagi untuk format sebenar. test.php

Kami memasukkan data melalui borang dan kemudian menyimpannya ke dalam sesi:

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><</span><strong class="keylink">html</strong><span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"><</span>head<span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"><</span>title<span style="color:#666">></span> Demonstration of Session <span style="color:#666"></</span>title<span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"></</span>head<span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"><</span>body<span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"><</span>f<strong class="keylink">ORM</strong> action<span style="color:#666">=</span><span style="color:#ba2121">"action.php"</span> method<span style="color:#666">=</span><span style="color:#ba2121">"post"</span> <span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"><</span>div style<span style="color:#666">=</span><span style="color:#ba2121">"border: 4px solid;padding:10px; width:40%"</span><span style="color:#666">></span>
</span></span><span style="display:flex;"><span>Employee Name<span style="color:#666">:<</span>input type<span style="color:#666">=</span><span style="color:#ba2121">"text"</span> name<span style="color:#666">=</span><span style="color:#ba2121">"employee"</span><span style="color:#666">></span>
</span></span><span style="display:flex;"><span>ID<span style="color:#666">:<</span>input type<span style="color:#666">=</span><span style="color:#ba2121">"text"</span>name<span style="color:#666">=</span><span style="color:#ba2121">"id"</span><span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"><</span>input type<span style="color:#666">=</span><span style="color:#ba2121">"submit"</span> value<span style="color:#666">=</span><span style="color:#ba2121">"SUBMIT"</span> name<span style="color:#666">=</span><span style="color:#ba2121">"submit"</span><span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"></</span>div<span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"></</span>form<span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"></</span>body<span style="color:#666">></span>
</span></span><span style="display:flex;"><span><span style="color:#666"></</span>html<span style="color:#666">></span>
</span></span></code></code>
Salin selepas log masuk

Sesi berfungsi seperti kuki apabila kami memulakan sesi dan mula menyimpan data, kami boleh menggunakannya sehingga sesi dimusnahkan. PHP 会话编码解码

Keluaran:

; 分隔的所有会话元素。这里应该提到的是,这种序列化与 PHP serialize()

action.php Rentetan sesi yang dikodkan mengandungi berbeza dengan

.

serialize()

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span> (isset(<span style="color:#19177c">$_POST</span>[<span style="color:#ba2121">&#39;submit&#39;</span>]))
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Start the Session
</span></span></span><span style="display:flex;"><span>session_start();
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//Form Data
</span></span></span><span style="display:flex;"><span><span style="color:#19177c">$employee</span><span style="color:#666">=</span><span style="color:#19177c">$_POST</span>[<span style="color:#ba2121">&#39;employee&#39;</span>];
</span></span><span style="display:flex;"><span><span style="color:#19177c">$id</span><span style="color:#666">=</span><span style="color:#19177c">$_POST</span>[<span style="color:#ba2121">&#39;id&#39;</span>];
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//store the form data into session
</span></span></span><span style="display:flex;"><span><span style="color:#19177c">$_SESSION</span>[<span style="color:#ba2121">&#39;employee&#39;</span>]<span style="color:#666">=</span><span style="color:#19177c">$employee</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$_SESSION</span>[<span style="color:#ba2121">&#39;id&#39;</span>]<span style="color:#666">=</span><span style="color:#19177c">$id</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"According to the data from session: <br>"</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Hello "</span><span style="color:#666">.</span> <span style="color:#19177c">$employee</span><span style="color:#666">.</span> <span style="color:#ba2121">"! your ID is "</span><span style="color:#666">.</span><span style="color:#19177c">$id</span><span style="color:#666">.</span><span style="color:#ba2121">"<br><br>"</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span><span style="color:#ba2121">"The encoded Session Data is: <br>"</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#408080;font-style:italic">//encode the session
</span></span></span><span style="display:flex;"><span><span style="color:#19177c">$session_econded</span><span style="color:#666">=</span> session_encode();
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$session_econded</span><span style="color:#666">.</span><span style="color:#ba2121">"<br><br>"</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#408080;font-style:italic">//decode session
</span></span></span><span style="display:flex;"><span>session_decode(<span style="color:#19177c">$session_econded</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Session data after decode: "</span>;
</span></span><span style="display:flex;"><span>print_r( <span style="color:#19177c">$_SESSION</span>);
</span></span><span style="display:flex;"><span>	<span style="color:#408080;font-style:italic">//Destroy the Session
</span></span></span><span style="display:flex;"><span>session_destroy();
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
Salin selepas log masuk
Keluaran: 🎜
<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>According to the data from session:
</span></span><span style="display:flex;"><span>Hello Jack! your ID is 1234
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>The encoded Session Data is:
</span></span><span style="display:flex;"><span>employee|s:4:"Jack";id|s:4:"1234";
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Session data after decode: Array ( [employee] => Jack [id] => 1234 ) 
</span></span></code></code>
Salin selepas log masuk
🎜 Data yang dikodkan adalah dalam bentuk rentetan yang serupa dengan PHP 🎜. 🎜

Atas ialah kandungan terperinci Pengekodan dan penyahkodan sesi PHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:lsjlt.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan