Chapterبەش 10 · Lectureوانە 10.2
Sessionsسێشنەکان
How PHP remembers one visitor across many pages while keeping the data on the server: start a session, store and read $_SESSION variables, end a session completely, compare sessions with cookies, and keep the session ID safe.
چۆن PHP یەک سەردانکەر لە چەندین لاپەڕەدا لە بیر دەگرێت و لەسەر دەهێڵێتەوە: دەست پێبکە، $_SESSION خەزن بکە و بیانخوێنەوە، بە تەواوی کۆتایی بە بهێنە، لەگەڵ بەراورد بکە و پارێزراو ڕابگرە.
What you will learnچی فێر دەبیت
- Explain why HTTP needs sessions and how the session ID cookie links a browser to its data on the server.
- Start a session correctly, before any output, on every page that needs it.
- Store, read and check session variables with
$_SESSION, including arrays. - Remove one variable, all variables, or the whole session, and log a user out completely.
- Compare cookies and sessions and choose the right one.
- Protect sessions against hijacking and fixation.
- ڕوونی بکەیتەوە بۆچی HTTP پێویستی بە هەیە و چۆن بە سەر دەبەستێتەوە.
- بە دروستی دەست پێبکەیت، پێش هەر ، لە هەموو ئەو لاپەڕانەی پێویستیان پێیەتی.
- بە
$_SESSIONخەزن بکەیت، بیانخوێنیتەوە و بیانپشکنیت، لەوانە . - یەک ، هەموو ، یان هەموو لاببەیت و بە تەواوی بچێتە دەرەوە.
- و بەراورد بکەیت و گونجاوەکە هەڵبژێریت.
- لە و بپارێزیت.
The problem: HTTP forgets youکێشەکە: HTTP تۆی لە بیر دەچێتەوە
When you work with an application on your computer, you open it, make some changes and close it. That is much like a session. The computer knows who you are: it knows when you start the application and when you finish. On the internet there is a problem. The web server does not know who you are or what you did, because HTTP is : every request is handled on its own, with no memory of the requests before it.
کاتێک لەسەر کۆمپیوتەرەکەت کار بە دەکەیت، دەیکەیتەوە، چەند گۆڕانکارییەک دەکەیت و دایدەخەیت. ئەمە زۆر لە دەچێت. دەزانێت تۆ کێیت: دەزانێت کەی دەست پێدەکەیت و کەی تەواوی دەکەیت. لەسەر کێشەیەک هەیە. نازانێت تۆ کێیت یان چیت کردووە، چونکە HTTP : هەر بە جیا مامەڵەی لەگەڵ دەکرێت، بەبێ هیچ بیرەوەرییەک لە پێشووی.
Session variables solve this problem. They store information about one user, such as the username or a favourite colour, so that every page of the website can use it. That is why a session is often used to identify a user, for example after login. As the course deck puts it: HTTP forgets everything, and sessions are how a site remembers you.
ئەم کێشەیە چارەسەر دەکەن. زانیاری دەربارەی یەک هەڵدەگرن، وەک ناوی یان ڕەنگی دڵخواز، بۆ ئەوەی هەموو لاپەڕەکانی بتوانن بەکاری بهێنن. بۆیە زۆرجار بۆ ناسینەوەی بەکار دێت، بۆ نموونە دوای . وەک سلایدەکانی کۆرسەکە دەڵێن: HTTP هەموو شتێک لە بیر دەکات و ئەو ڕێگایەن کە پێی لە بیرت دەگرێت.
You take a piece of cloth to a tailor in the bazaar. The tailor keeps the cloth and your measurements in his shop and gives you a small paper with a number on it: 57. A week later you show the paper, and he finds your order at once. You carry only the number; everything else stays in the shop. A session works the same way: the server keeps the data, and the browser carries only a number, the , in a cookie.
پارچە قوماشێک دەبەیت بۆ لای بەرگدرووێک لە بازاڕ. بەرگدروو قوماشەکە و پێوانەکانت لە دوکانەکەیدا هەڵدەگرێت و پارچە کاغەزێکت دەداتێ کە ژمارەیەکی لەسەرە: ٥٧. هەفتەیەک دواتر کاغەزەکە نیشان دەدەیت و دەستبەجێ داواکەت دەدۆزێتەوە. تۆ تەنها ژمارەکە هەڵدەگریت؛ هەموو شتێکی تر لە دوکانەکەدا دەمێنێتەوە. بە هەمان شێوە کار دەکات: هەڵدەگرێت و تەنها ژمارەیەک، واتە ، لە هەڵدەگرێت.
Where the analogy stops workingسنووری ئەم نموونەیە
If someone steals your paper, they can collect your clothes. In the same way, anyone who steals a session ID can act as you. That is why PHP uses long random IDs, and why a login page should give the user a new ID.
ئەگەر کەسێک کاغەزەکەت بدزێت، دەتوانێت جلەکانت وەربگرێت. بە هەمان شێوە، هەر کەسێک بدزێت دەتوانێت خۆی بکات بە تۆ. بۆیە PHP ناسێنەری هەڕەمەکیی درێژ بەکار دەهێنێت و بۆیە لاپەڕەی دەبێت ناسێنەرێکی نوێ بە بدات.
What a session isسێشن چییە
A way to recognise one user across many pages, with the data kept on the . PHP gives the browser a in a cookie and keeps the user's data in $_SESSION. Call session_start() at the top of every page that uses it.
ڕێگایەکە بۆ ناسینەوەی بەکارهێنەرێک لە چەندین لاپەڕەدا و دراوەکانی لەسەر هەڵدەگیرێن. PHP لە کووکییەکدا دەدات بە تۆڕگەڕەکە و زانیارییەکانی بەکارهێنەر لە $_SESSIONدا هەڵدەگرێت. لە سەرەتای هەر لاپەڕەیەک کە بەکاری دەهێنێت session_start() بانگ بکە.
The course defines it like this: a session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the user's computer. Session variables hold information about one single user, and they are available to all the pages of one application.
کۆرسەکە بەم شێوەیە دەیناسێنێت: ڕێگایەکە بۆ هەڵگرتنی زانیاری (لە ) بۆ ئەوەی لە چەندین لاپەڕەدا بەکار بهێنرێت. بە پێچەوانەی ، زانیارییەکە لەسەر خەزن ناکرێت. زانیاری دەربارەی تەنها یەک هەڵدەگرن و بۆ هەموو لاپەڕەکانی یەک بەردەستن.
How can the server know which data belongs to which visitor? With a small trick:
چۆن دەزانێت کام هی کام سەردانکەرە؟ بە فێڵێکی بچووک:
session_start()gives each new visitor a long random , for examplea1b2c3….- PHP sends this ID to the browser in a cookie named
PHPSESSID. This is the only thing the browser stores. - The data itself stays on the server, in a file named after the ID (
sess_a1b2c3…). - On every later request the browser sends the cookie back.
session_start()reads the ID, opens the matching file and fills$_SESSIONwith its data.
session_start()هەڕەمەکیی درێژ دەدات بە هەر سەردانکەرێکی نوێ، بۆ نموونەa1b2c3….- PHP ئەم ناسێنەرە لە بە ناوی
PHPSESSIDدەنێرێت بۆ . ئەمە تاکە شتە کە هەڵیدەگرێت. - خۆیان لەسەر دەمێننەوە، لە فایلێکدا کە ناوەکەی لە ناسێنەرەکەوە وەرگیراوە (
sess_a1b2c3…). - لە هەموو دواتردا دەنێرێتەوە.
session_start()ناسێنەرەکە دەخوێنێتەوە، فایلە هاوتاکە دەکاتەوە و$_SESSIONبە پڕ دەکات.
What a session isسێشن چییە
How long does a session last?سێشن چەند دەخایەنێت؟
The course says that by default, session variables last until the user closes the browser. That is half of the story. Two separate clocks are at work:
کۆرسەکە دەڵێت بە شێوەی بنەڕەتی، تا ئەو کاتە دەمێننەوە کە دادەخات. ئەمە نیوەی چیرۆکەکەیە. دوو کاتژمێری جیاواز کار دەکەن:
- In the browser: the
PHPSESSIDcookie has the lifetime0(settingsession.cookie_lifetime), so the browser deletes it when it closes. Without the ID, the old data cannot be found any more. - On the server: PHP cleans up session files that nobody has used for
session.gc_maxlifetimeseconds: 1440 seconds = 24 minutes by default. So a session also ends if the visitor does nothing for a while, even with the browser open.
- لە :
PHPSESSIDماوەی0ـی هەیە (ڕێکخستنیsession.cookie_lifetime)، بۆیە کاتێک دادەخرێت دەیسڕێتەوە. بەبێ ناسێنەرەکە، کۆنەکان ئیتر نادۆزرێنەوە. - لەسەر : PHP ئەو فایلانەی پاک دەکاتەوە کە ماوەی
session.gc_maxlifetimeچرکە کەس بەکاری نەهێناون: بە شێوەی بنەڕەتی 1440 چرکە = ٢٤ خولەک. بۆیە ئەگەر سەردانکەر ماوەیەک هیچ نەکات، کۆتایی دێت، تەنانەت ئەگەر کراوە بێت.
<?php
echo "session.name = " . ini_get('session.name') . "<br>";
echo "session.cookie_lifetime = " . ini_get('session.cookie_lifetime') . "<br>";
echo "session.gc_maxlifetime = " . ini_get('session.gc_maxlifetime');session.name = PHPSESSID session.cookie_lifetime = 0 session.gc_maxlifetime = 1440
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
session.name = PHPSESSID<br>session.cookie_lifetime = 0<br>session.gc_maxlifetime = 1440The default values of PHP 8.5. Your server's php.ini may change them.
PHP 8.5. php.iniـی لەوانەیە بیانگۆڕێت.
Starting a session: session_start()دەستپێکردنی سێشن: session_start()
To use a session, the first thing to do is to call session_start(). It checks whether this visitor already has a session. If not, it creates one. If yes, it resumes it, using the session ID the browser sent, and fills $_SESSION with the saved data.
بۆ بەکارهێنانی ، یەکەم کار بانگکردنی session_start()ـە. دەپشکنێت ئایا ئەم سەردانکەرە پێشتر هەیە. ئەگەر نا، یەکێک دروست دەکات. ئەگەر بەڵێ، درێژەی پێدەداتەوە، بە بەکارهێنانی ئەو ناردوویەتی و $_SESSION بە خەزنکراوەکان پڕ دەکات.
<?PHP
session_start();
?>
<html>
<head> </head>
<body> </body>Set-Cookie: PHPSESSID=340be8cdf16d391b54c4e61a96a1eef0; path=/ <html> <head> </head> <body> </body>
On the second visit the browser sends Cookie: PHPSESSID=340be8cd…, and the server sends no new Set-Cookie header: the session is resumed. Your ID will be different.
لە سەردانی دووەمدا Cookie: PHPSESSID=340be8cd… دەنێرێت و هیچ سەردێڕێکی Set-Cookieـی نوێ نانێرێت: درێژەی پێدەدرێتەوە. ناسێنەرەکەی تۆ جیاواز دەبێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
<?PHPin capitals is valid: the opening tag is not case-sensitive. Lower case<?phpis the usual style.<?PHPبە پیتی گەورە دروستە: کردنەوە نییە.<?phpی بچووک شێوازی ئاساییە.- Lineدێڕی 2
Before any HTML: the session cookie can still be sent.
پێش هەموو HTMLێک: هێشتا دەتوانرێت بنێردرێت.
Starting a session: session_start()دەستپێکردنی سێشن: session_start()
session_start() must come before any outputsession_start() دەبێت پێش هەر دەرخستەیەک بێت
The course says: “The session_start() function must appear BEFORE the <html> tag.” The exact rule is stricter: it must run before any output at all. session_start() sends the PHPSESSID cookie in a header, and headers can only be sent before the first byte of the page: before any HTML, any echo, even a space or an empty line before <?php.
کۆرسەکە دەڵێت: « session_start() دەبێت پێش <html> بێت.» یاسا وردەکە توندترە: دەبێت پێش هەموو کار بکات. session_start() PHPSESSID لە دەنێرێت و سەردێڕەکان تەنها پێش یەکەم لاپەڕەکە دەنێردرێن: پێش هەموو HTMLێک، هەموو echoیەک، تەنانەت یان دێڕێکی بەتاڵ پێش <?php.
<html>
<body>
<?php
session_start();
$_SESSION['views'] = 1;
echo "Pageviews=" . $_SESSION['views'];
?>
</body>
</html>Warning: session_start(): Session cannot be started after headers have already been sent (sent from /www/late.php on line 1) in /www/late.php on line 4 Pageviews=1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
<br />
<b>Warning</b>: session_start(): Session cannot be started after headers have already been sent (sent from /www/late.php on line 1) in <b>/www/late.php</b> on line <b>4</b><br />
Pageviews=1</body>
</html>Pageviews=1 still appears, because $_SESSION is an ordinary array during this request. But no cookie is sent and nothing is saved, so the next page knows nothing.
Pageviews=1 هێشتا دەردەکەوێت، چونکە $_SESSION لەم داواکارییەدا ئاساییە. بەڵام هیچ نانێردرێت و هیچ شتێک خەزن ناکرێت، بۆیە لاپەڕەی داهاتوو هیچ نازانێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
Output: PHP sends the headers here.
: PHP لێرەدا سەردێڕەکان دەنێرێت.
- Lineدێڕی 4
Too late: the session cannot be started, and nothing is saved.
زۆر درەنگە: دەست پێناکات و هیچ شتێک خەزن ناکرێت.
session_start(), setcookie() and header() all send headers, so they must run before the first output.سەرەتا ، پاشان ناوەڕۆک. session_start()، setcookie() و header() هەموویان سەردێڕ دەنێرن، بۆیە دەبێت پێش یەکەم کار بکەن.CêITPHP 8 code: why session_start() and setcookie() must come before any output ('headers already sent'), and how to set, read and delete cookies.
کۆدی 8: بۆچی session_start() و setcookie() دەبێت پێش هەر بێن ( headers already sent) و چۆن دابنێیت، بیخوێنیتەوە و بیسڕیتەوە.
Watch 0:36–6:09: session_start(), output buffering and 'headers already sent'.
لە 0:36 تا 6:09 سەیر بکە: session_start()، و headers already sent.
Storing and reading session variablesخەزنکردن و خوێندنەوەی گۆڕاوەکانی سێشن
The correct way to store and read session variables is the $_SESSION. It is an : you choose a key and give it a value, and you read it back with the same key.
ڕێگای دروست بۆ خەزنکردن و خوێندنەوەی ، $_SESSIONـە. : هەڵدەبژێریت و پێ دەدەیت و بە هەمان دەیخوێنیتەوە.
<?php
session_start(); // store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php //retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body> </html>Pageviews=1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
Pageviews=1</body> </html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The comment “store session data” sits on this line, but it describes line 3.
«store session data» لەسەر ئەم دێڕەیە، بەڵام باسی دێڕی 3 دەکات.
- Lineدێڕی 3
Store: the key
viewsgets the value 1.خەزنکردن:
views1 وەردەگرێت.- Lineدێڕی 8
Read: the same key gives the value back.
خوێندنەوە: هەمان دەگەڕێنێتەوە.
The course deck shows the shortest version: session_start(); $_SESSION["user"] = "Aram";. Unlike a cookie, a session variable is visible at once, in the same request, because it is only an array element on the server.
سلایدەکانی کۆرسەکە کورتترین وەشان پیشان دەدەن: session_start(); $_SESSION["user"] = "Aram";. بە پێچەوانەی ، دەستبەجێ دەبینرێت، لە هەمان ، چونکە تەنها دانەیەکە لە لەسەر .
Storing and reading session variablesخەزنکردن و خوێندنەوەی گۆڕاوەکانی سێشن
Using session variables on other pagesبەکارهێنانی گۆڕاوەکانی سێشن لە لاپەڕەکانی تردا
The real power of a session: one page stores a value, and any other page of the website reads it. Every page that uses the session must call session_start() first. The course shows only the reading page; here is the page that sets the values, too:
هێزی ڕاستەقینەی : لاپەڕەیەک خەزن دەکات و هەر لاپەڕەیەکی تری دەیخوێنێتەوە. هەموو لاپەڕەیەک کە بەکار دەهێنێت دەبێت سەرەتا session_start() بانگ بکات. کۆرسەکە تەنها لاپەڕەی خوێندنەوە پیشان دەدات؛ ئەمە لاپەڕەی دانانی :
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>Session variables are set.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
Session variables are set.
</body>
</html><?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>Favorite color is green. Favorite animal is cat.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
Favorite color is green.<br>Favorite animal is cat.
</body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
This page needs
session_start()too. Without it,$_SESSIONdoes not exist on this page, and PHP warns “Undefined global variable $_SESSION”.ئەم لاپەڕەیەش پێویستی بە
session_start()ـە. بەبێ ئەو،$_SESSIONلەم لاپەڕەیەدا بوونی نییە و PHP ئاگادار دەکاتەوە: «Undefined global variable $_SESSION».
What if a visitor opens page2.php first, before page1.php? The keys do not exist yet, and PHP 8 warns:
ئەگەر سەردانکەرێک سەرەتا page2.php بکاتەوە، پێش page1.php؟ هێشتا بوونیان نییە و PHP 8 ئاگادار دەکاتەوە:
Warning: Undefined array key "favcolor" in /www/page2.php on line 10 Favorite color is . Warning: Undefined array key "favanimal" in /www/page2.php on line 11 Favorite animal is .
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// ?? gives a default when page 1 has not been visited yet
$color = $_SESSION["favcolor"] ?? "unknown";
$animal = $_SESSION["favanimal"] ?? "unknown";
echo "Favorite color is " . htmlspecialchars($color) . ".<br>";
echo "Favorite animal is " . htmlspecialchars($animal) . ".";
?>
</body>
</html>Favorite color is unknown. Favorite animal is unknown.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
Favorite color is unknown.<br>Favorite animal is unknown.
</body>
</html>Storing and reading session variablesخەزنکردن و خوێندنەوەی گۆڕاوەکانی سێشن
What can a session hold?سێشن دەتوانێت چی هەڵبگرێت؟
A cookie can hold only a . A session variable can hold any PHP value: numbers, strings, booleans, arrays, even objects. PHP saves it on the server and gives it back unchanged on the next page. Here a shopping cart is an array inside the session:
تەنها دەتوانێت هەڵبگرێت. دەتوانێت هەر نرخێکی PHP هەڵبگرێت: ژمارە، ، ، ، تەنانەت . PHP لەسەر خەزنی دەکات و لە لاپەڕەی داهاتوودا بێ گۆڕان دەیگەڕێنێتەوە. لێرەدا سەبەتەی کڕین لە ناو :
<?php
session_start();
// A session variable can hold an array (a cookie can only hold a string)
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
$_SESSION['cart'][] = 'tea';
echo "Items in your cart: " . count($_SESSION['cart']) . "<br>";
echo implode(', ', $_SESSION['cart']);Items in your cart: 2 tea, tea
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Items in your cart: 2<br>tea, teaOn the first visit: Items in your cart: 1 — tea.
لە یەکەم سەرداندا: Items in your cart: 1 — tea.
Try the same with a cookie and PHP 8 refuses: setcookie('cart', ['tea', 'sugar']) stops with TypeError: setcookie(): Argument #2 ($value) must be of type string, array given (see Cookies).
هەمان شت بە تاقی بکەرەوە و PHP 8 ڕەتی دەکاتەوە: setcookie('cart', ['tea', 'sugar']) بە TypeError: setcookie(): Argument #2 ($value) must be of type string, array given دەوەستێت (بڕوانە کووکییەکان).
Storing and reading session variablesخەزنکردن و خوێندنەوەی گۆڕاوەکانی سێشن
A session value or a cookie value?نرخێکی سێشن یان نرخێکی کووکی؟
A student's registration page shows the choice between the two. After the account is saved, it greets the user with the name from the session; the cookie version is commented out. This is the fragment from the course material:
لاپەڕەیەکی خوێندکارێک هەڵبژاردنی نێوان ئەم دووانە پیشان دەدات. دوای خەزنکردنی هەژمارەکە، بە ناوەکەی ناو بەخێرهاتنی دەکات؛ وەشانی کراوە بە . ئەمە پارچەکەیە لە کەرەستەی کۆرسەکەوە:
//echo "An account has been successfully created by ".$_COOKIE['username']."!";
echo "An account has been successfully created by ".$_SESSION['username']."!";
} else {
die(mysqli_error($connect));
}The fragment does not show where $_SESSION['username'] is set. Here it is completed and made runnable. The session value is visible at once. A cookie set in the same request would still be empty, because a new cookie only arrives with the next request:
پارچەکە پیشان نادات $_SESSION['username'] لە کوێ دادەنرێت. لێرەدا تەواو کراوە و ئامادەیە بۆ کارپێکردن. دەستبەجێ دەبینرێت. کە لە هەمان داواکاریدا دابنرێت هێشتا بەتاڵ دەبێت، چونکە نوێ تەنها لەگەڵ داهاتوودا دەگات:
<?php
session_start();
$username = 'Aram'; // in the real page: the name from the registration form
$insertWorked = true; // in the real page: the result of the INSERT query
$_SESSION['username'] = $username;
if ($insertWorked) {
//echo "An account has been successfully created by ".$_COOKIE['username']."!";
echo "An account has been successfully created by " . htmlspecialchars($_SESSION['username']) . "!";
} else {
die("An error occurred. Please try again.");
}An account has been successfully created by Aram!
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
Set the session value; it is readable on this page and on every later page.
دابنێ؛ لەم لاپەڕەیە و لە هەموو لاپەڕەیەکی دواتردا دەخوێندرێتەوە.
- Lineدێڕی 9
Escape the name with
htmlspecialchars(): it came from a form.ناوەکە بە
htmlspecialchars()بپارێزە: لە هاتووە.
<?php
session_start();
$username = 'Aram'; // in the real page this comes from the form
// ... the INSERT query succeeded ...
$_SESSION['username'] = $username; // visible immediately, in this same request
setcookie('username', $username, time() + 3600); // NOT visible in $_COOKIE until the next request
echo "From the session: " . htmlspecialchars($_SESSION['username']) . "<br>";
echo "From the cookie: " . htmlspecialchars($_COOKIE['username'] ?? '(empty in this request)');From the session: Aram From the cookie: (empty in this request)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
From the session: Aram<br>From the cookie: (empty in this request)On the next request both lines say Aram.
لە داهاتوودا هەردوو دێڕەکە دەڵێن Aram.
Example: a page-visit counterنموونە: ژمێرەرێکی سەردانی لاپەڕە
This course example counts how many times the visitor opened the page during the session. The first visit sets intCount to 1, and each later visit adds 1. Here is the slide code as it is:
ئەم نموونەیەی کۆرسەکە دەژمێرێت سەردانکەر لە ماوەی چەند جار لاپەڕەکەی کردووەتەوە. یەکەم سەردان intCount دەکات بە 1 و هەر سەردانێکی دواتر 1 زیاد دەکات. ئەمە کۆدی سلایدەکەیە وەک خۆی:
<?php
// Session: example1.php
session_start();
if (!$_SESSION["intCount"])
$_SESSION["intCount"] = 1; else
$_SESSION["intCount"]++;
?>
<html >
<head><title>PHP Script</title></head>
<body>
<?php
echo "<p>You have accessed this page " . $_SESSION["intCount"]
. " times.</p>";
?>
</body>
</html>Warning: Undefined array key "intCount" in /www/example1.php on line 4 You have accessed this page 1 times.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Warning</b>: Undefined array key "intCount" in <b>/www/example1.php</b> on line <b>4</b><br />
<html >
<head><title>PHP Script</title></head>
<body>
<p>You have accessed this page 1 times.</p></body>
</html>The second visit prints “You have accessed this page 2 times.” without a warning.
سەردانی دووەم بەبێ «You have accessed this page 2 times.» چاپ دەکات.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
On the first visit the key
intCountdoes not exist yet, so reading it gives a warning.لە یەکەم سەرداندا
intCountهێشتا بوونی نییە، بۆیە خوێندنەوەی دەدات.- Lineدێڕی 4-6
An
if/elsewithout braces, withelseon the same line as the first statement: legal, but hard to read.if/elseێک بەبێ کەوانەی لوولپێچ وelseلەسەر هەمان دێڕی یەکەم ڕستەدا: یاساییە، بەڵام خوێندنەوەی قورسە.
<?php
// Session: example1.php (corrected)
session_start();
if (!isset($_SESSION["intCount"])) {
$_SESSION["intCount"] = 1; // first visit in this session
} else {
$_SESSION["intCount"]++; // every later visit
}
?>
<html>
<head><title>PHP Script</title></head>
<body>
<?php
echo "<p>You have accessed this page " . $_SESSION["intCount"] . " times.</p>";
?>
</body>
</html>You have accessed this page 1 times. You have accessed this page 2 times. You have accessed this page 3 times.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
isset(): true only when the key exists. No warning.isset(): تەنها کاتێک هەبێت ڕاستە. هیچ نییە.- Lineدێڕی 7
++adds 1 to the stored number.++1 دەخاتە سەر ژمارە خەزنکراوەکە.
A shorter way to write lines 4 to 8 is one line with the : $_SESSION["intCount"] = ($_SESSION["intCount"] ?? 0) + 1;. Compare this counter with the cookie counter: there the count lives in the visitor's browser and can be edited; here it lives on the server, and the visitor cannot change it.
ڕێگایەکی کورتتر بۆ نووسینی دێڕەکانی 4 تا 8، یەک دێڕە بە : $_SESSION["intCount"] = ($_SESSION["intCount"] ?? 0) + 1;. ئەم ژمێرەرە لەگەڵ ژمێرەری کووکی بەراورد بکە: لەوێ ژمارەکە لە سەردانکەردا دەژی و دەتوانرێت دەستکاری بکرێت؛ لێرە لەسەر دەژی و سەردانکەر ناتوانێت بیگۆڕێت.
Ending a sessionکۆتاییهێنان بە سێشن
To remove all session variables and destroy the session, the course uses session_unset() and session_destroy():
بۆ لابردنی هەموو و لەناوبردنی ، کۆرسەکە session_unset() و session_destroy() بەکار دەهێنێت:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
echo "The session is destroyed.";
?>
</body>
</html>The session is destroyed.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
The session is destroyed.
</body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
session_start()first: you can only destroy a session that is open.سەرەتا
session_start(): تەنها دەتوانیت لەناو ببەیت کە کراوەتەوە.- Lineدێڕی 10
Remove all session variables (
$_SESSIONbecomes empty).هەموو لاببە (
$_SESSIONبەتاڵ دەبێت).- Lineدێڕی 13
Delete the session data on the server.
لەسەر بسڕەوە.
- Lineدێڕی 14
We added this message so that the page shows something.
ئەم پەیامەمان زیاد کرد بۆ ئەوەی لاپەڕەکە شتێک پیشان بدات.
There are several tools, and each does a different job. This test shows them one after another:
چەند ئامرازێک هەن و هەریەکەیان کارێکی جیاواز دەکات. ئەم تاقیکردنەوەیە یەک لە دوای یەک پیشانیان دەدات:
<?php
session_start();
$_SESSION['user'] = 'Aram';
$_SESSION['color'] = 'green';
unset($_SESSION['color']); // remove ONE variable
echo "After unset: "; print_r($_SESSION); echo "\n";
session_unset(); // remove ALL variables (the session still exists)
echo "After session_unset(): "; print_r($_SESSION); echo "\n";
$_SESSION['user'] = 'Aram';
var_dump(session_destroy()); // delete the data on the server
echo "After session_destroy(): "; print_r($_SESSION); echo "\n";
echo "Session status: " . session_status() . " (1 = none, 2 = active)";After unset: Array
(
[user] => Aram
)
After session_unset(): Array
(
)
bool(true)
After session_destroy(): Array
(
[user] => Aram
)
Session status: 1 (1 = none, 2 = active)| Codeکۆد | What it doesچی دەکات | What it does not doچی ناکات |
|---|---|---|
unset($_SESSION['color']) | Removes one session variable.یەک لادەبات. | The other variables and the session stay. تر و دەمێننەوە. |
session_unset() | Removes all session variables. Same as $_SESSION = [].هەموو لادەبات. وەک $_SESSION = []. | The session file and the cookie stay. و دەمێننەوە. |
session_destroy() | Deletes the session data on the server and closes the session. Returns true. لەسەر دەسڕێتەوە و دادەخات. true دەگەڕێنێتەوە. | It does not empty $_SESSION in this request, and it does not delete the browser's PHPSESSID cookie.$_SESSION لەم داواکارییەدا بەتاڵ ناکات و PHPSESSIDـی ناسڕێتەوە. |
setcookie(session_name(), '', time() - 42000, …) | Deletes the PHPSESSID cookie in the browser. PHPSESSID لە دەسڕێتەوە. | The data on the server stays unless you also destroy it. سەر دەمێننەوە، مەگەر ئەوانیش لەناو ببەیت. |
Ending a sessionکۆتاییهێنان بە سێشن
A complete logoutچوونەدەرەوەیەکی تەواو
<?php
session_start();
$_SESSION = []; // 1. empty the session array
// 2. tell the browser to delete the session cookie
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params['path'], $params['domain'], $params['secure'], $params['httponly']);
session_destroy(); // 3. delete the data on the server
echo "You are logged out.";Set-Cookie: PHPSESSID=deleted; expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0; path=/ You are logged out.
The browser forgets the ID. The next page gets a brand-new session with an empty $_SESSION.
ناسێنەرەکە لە بیر دەکات. لاپەڕەی داهاتوو تەواو نوێ وەردەگرێت کە $_SESSIONـی بەتاڵە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
Step 1: empty the array (like
session_unset()).هەنگاوی ١: بەتاڵ بکە (وەک
session_unset()).- Lineدێڕی 7-9
Step 2: delete the cookie with the same name, path and domain that PHP used.
session_get_cookie_params()tells you them.هەنگاوی ٢: بسڕەوە بە هەمان ناو، و دۆمەین کە PHP بەکاری هێنا.
session_get_cookie_params()پێت دەڵێت چین.- Lineدێڕی 11
Step 3: delete the data on the server.
هەنگاوی ٣: لەسەر بسڕەوە.
Cookies and sessions comparedبەراوردی کووکییەکان و سێشنەکان
The course names three general differences between cookies and sessions:
کۆرسەکە سێ جیاوازیی گشتی لە نێوان و دەستنیشان دەکات:
- The main difference: cookies are stored on the client side, sessions on the server side.
- Cookies can only store strings. A session can store objects (and any other PHP value).
- A cookie can be saved for future visits, but a session cannot: when users close their browser, they also lose the session (by default).
- جیاوازیی سەرەکی: لە خەزن دەکرێن، لە لای .
- تەنها دەتوانن خەزن بکەن. دەتوانێت (و هەر نرخێکی تری PHP) خەزن بکات.
- دەتوانرێت بۆ سەردانەکانی داهاتوو هەڵبگیرێت، بەڵام ناتوانرێت: کاتێک بەکارهێنەران دادەخەن، لە دەست دەدەن (بە شێوەی بنەڕەتی).
The course summary and the deck add more points. Together:
کورتەی کۆرسەکە و سلایدەکان خاڵی زیاتر زیاد دەکەن. پێکەوە:
| Questionپرسیار | Cookie | Session |
|---|---|---|
| Where is the data kept? لە کوێ هەڵدەگیرێت؟ | In the visitor's browser (client side)لە سەردانکەردا () | On the server; the browser keeps only the session IDلەسەر ؛ تەنها هەڵدەگرێت |
| What can it hold?چی دەتوانێت هەڵبگرێت؟ | Strings onlyتەنها | Any PHP value: numbers, arrays, objectsهەر نرخێکی PHP: ژمارە، ، |
| How big?چەند گەورە؟ | About 4 KB per cookieنزیکەی 4 KB بۆ هەر | Larger than 4 KB; limited only by the serverگەورەتر لە 4 KB؛ تەنها سنووری بۆ دادەنێت |
| How long does it live?چەند دەژی؟ | Until its expiry time; it can last for future visitsتا ؛ دەتوانێت بۆ سەردانەکانی داهاتوو بمێنێتەوە | By default until the browser closes (or 24 minutes without use)بە شێوەی بنەڕەتی تا دادەخرێت (یان ٢٤ خولەک بێ بەکارهێنان) |
| Can the visitor change it?سەردانکەر دەتوانێت بیگۆڕێت؟ | Yes, easilyبەڵێ، بە ئاسانی | No; the visitor can only lose or steal the IDنا؛ سەردانکەر تەنها دەتوانێت ناسێنەرەکە لە دەست بدات یان بیدزێت |
| Visible after it is setکەی دوای دانان دەبینرێت | From the next requestلە داهاتووەوە | At once, in the same requestدەستبەجێ، لە هەمان |
| Created and read withدروستکردن و خوێندنەوە بە | setcookie() / $_COOKIE | session_start() / $_SESSION |
| Good forباشە بۆ | Harmless preferences that should last: language, themeئارەزووی بێزیان کە دەبێت بمێنێتەوە: زمان، ڕووکار | Logins, shopping carts, anything private or important، سەبەتەی کڕین، هەر شتێکی تایبەت یان گرنگ |
Keeping sessions safeپاراستنی سێشنەکان
The session data cannot be changed by the visitor, but the session ID is a key to it: whoever has the ID is the user. Two attacks target the ID:
سەردانکەر ناتوانێت بگۆڕێت، بەڵام کلیلی ئەوانە: هەر کەسێک ناسێنەرەکەی هەبێت، ئەو بەکارهێنەرەکەیە. دوو هێرش ناسێنەرەکە دەکەنە ئامانج:
- : someone steals a valid ID, for example with an script that reads cookies, or by reading plain HTTP traffic on a shared Wi-Fi.
- : the attacker gets an ID first and makes the victim use it. When the victim logs in, the attacker's ID becomes a logged-in session.
- : کەسێک ناسێنەرێکی دروست دەدزێت، بۆ نموونە بە کە دەخوێنێتەوە، یان بە خوێندنەوەی هاتوچۆی HTTPـی سادە لە Wi-Fiـیەکی هاوبەشدا.
- : هێرشبەر سەرەتا ناسێنەرێک وەردەگرێت و وا لە قوربانی دەکات بەکاری بهێنێت. کاتێک قوربانی دەچێتە ژوورەوە، ناسێنەرەکەی هێرشبەر دەبێتە چووەژوورەوە.
Three habits protect you. First, give the user a new session ID right after login with session_regenerate_id(true). The old ID becomes useless:
سێ خوو دەتپارێزن. یەکەم، ڕاستەوخۆ دوای نوێ بدە بە بە session_regenerate_id(true). ناسێنەرە کۆنەکە بێسوود دەبێت:
<?php
session_start();
$old = session_id();
session_regenerate_id(true); // new id, old session file deleted
echo "Old id: $old<br>New id: " . session_id();Set-Cookie: PHPSESSID=5b571bb77eb370d9422c882ed6cc76a1; path=/ Old id: e3b6f8bafc3d481d8cb4f6303fc749bd New id: 5b571bb77eb370d9422c882ed6cc76a1
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
truealso deletes the old session file on the server.trueکۆنی لەسەر دەسڕێتەوە.
How to make sessions safer: Secure and HttpOnly settings for the session cookie, and session_regenerate_id() to give the user a new session ID.
چۆن پارێزراوتر بکەیت: Secure و بۆ و session_regenerate_id() بۆ ئەوەی نوێ وەربگرێت.
Watch 11:25–14:54: session_regenerate_id() and changing the ID regularly.
لە 11:25 تا 14:54 سەیر بکە: session_regenerate_id() و گۆڕینی هەر ماوەیەک جارێک.
Keeping sessions safeپاراستنی سێشنەکان
A safer session cookieکووکیی سێشنی سەلامەتتر
Second habit: make the session cookie HttpOnly (JavaScript cannot steal it), SameSite, and on an HTTPS site Secure. Set this before session_start():
خووی دووەم: بکە بە HttpOnly (JavaScript ناتوانێت بیدزێت)، SameSite و لەسەر HTTPS Secure. ئەمە پێش session_start() دابنێ:
<?php
// Make the session cookie safer. This must come BEFORE session_start().
session_set_cookie_params([
'lifetime' => 0, // 0 = delete the cookie when the browser closes
'path' => '/',
'httponly' => true, // JavaScript cannot read the session id
'samesite' => 'Lax',
// 'secure' => true, // turn on when the site uses HTTPS
]);
session_start();
echo "Session started.";Set-Cookie: PHPSESSID=bce9b8cf20fcb300c67bbc817f59123a; path=/; HttpOnly; SameSite=Lax Session started.
Third habit: end sessions properly on logout (all three steps above), and never put the session ID into a URL or a page where others can see it. The login system puts all of this into practice.
خووی سێیەم: لە کاتی بە دروستی کۆتایی بە بهێنە (هەر سێ هەنگاوەکەی سەرەوە) و هەرگیز مەخەرە ناو URL یان لاپەڕەیەک کە کەسانی تر بیبینن. سیستەمی چوونەژوورەوە هەموو ئەمانە جێبەجێ دەکات.
How to make sessions safer: Secure and HttpOnly settings for the session cookie, and session_regenerate_id() to give the user a new session ID.
چۆن پارێزراوتر بکەیت: Secure و بۆ و session_regenerate_id() بۆ ئەوەی نوێ وەربگرێت.
Watch 8:19–11:25: safer session cookie settings, then session_start().
لە 8:19 تا 11:25 سەیر بکە: پارێزراوتر بۆ ، پاشان session_start().
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
Output (HTML, a space, echo) before session_start() (HTML، ، echo) پێش session_start() | Warning; the session is not started (hidden on XAMPP by output buffering)؛ دەست پێناکات (لە XAMPPدا بە دەشاردرێتەوە) | session_start() on the first linesession_start() لە یەکەم دێڕدا |
Forgetting session_start() on a pageلە بیرچوونی session_start() لە لاپەڕەیەکدا | $_SESSION does not exist on that page (Warning: Undefined global variable $_SESSION)$_SESSION لەو لاپەڕەیەدا بوونی نییە (: Undefined global variable $_SESSION) | Call it on every page that uses the sessionلە هەموو ئەو لاپەڕانەدا بانگی بکە کە بەکار دەهێنن |
$SESSION instead of $_SESSION$SESSION لە جیاتی $_SESSION | A normal, empty variable: nothing is saved ئاسایی و بەتاڵ: هیچ شتێک خەزن ناکرێت | Write the underscoreهێڵی ژێرەوە بنووسە |
Reading $_SESSION['x'] before it existsخوێندنەوەی $_SESSION['x'] پێش ئەوەی هەبێت | Warning: Undefined array key: Undefined array key | isset() or ?? 'default'isset() یان ?? 'default' |
Curly quotes: $_SESSION[‘views’] خوار: $_SESSION[‘views’] | Fatal error: Undefined constant: Undefined constant | Straight quotes 'views' ڕاست 'views' |
session_destroy() without session_start()session_destroy() بەبێ session_start() | Warning: Trying to destroy uninitialized session: Trying to destroy uninitialized session | Start the session firstسەرەتا دەست پێبکە |
Only session_destroy() on logoutتەنها session_destroy() لە کاتی | The PHPSESSID cookie stays; $_SESSION still full in this request PHPSESSID دەمێنێتەوە؛ $_SESSION لەم داواکارییەدا هێشتا پڕە | Empty $_SESSION, delete the cookie, then destroy$_SESSION بەتاڵ بکە، بسڕەوە، پاشان لەناوی ببە |
| Keeping the same session ID after loginهێشتنەوەی هەمان دوای | Session fixation is possible دەکرێت | session_regenerate_id(true) |
Exercises and quick checkڕاهێنان و پشکنینی خێرا
Write two pages. choose.php shows a drop-down list with green, blue and red. When the visitor saves, store the colour in the session and redirect to show.php. show.php prints “Your favourite colour is …” in that colour, or a link to choose.php if no colour was chosen yet.
دوو لاپەڕە بنووسە. choose.php بە green، blue و red پیشان دەدات. کاتێک سەردانکەر خەزنی دەکات، ڕەنگەکە لە هەڵبگرە و ئاڕاستە بکەرەوە بۆ show.php. show.php «Your favourite colour is …» بەو ڕەنگە چاپ دەکات، یان ئەگەر هێشتا ڕەنگێک هەڵنەبژێردراوە، بۆ choose.php.
Hintئاماژە
session_start() on both pages; header('Location: show.php'); exit; after saving.
session_start() لە هەردوو لاپەڕەکەدا؛ دوای خەزنکردن header('Location: show.php'); exit;.
Show answerوەڵامەکە پیشان بدە
<?php
session_start();
$colors = ['green', 'blue', 'red'];
if (isset($_POST['color']) && in_array($_POST['color'], $colors)) {
$_SESSION['favcolor'] = $_POST['color']; // visible on every page from now on
header('Location: show.php');
exit;
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post">
Favourite colour:
<select name="color">
<?php foreach ($colors as $c): ?>
<option value="<?php echo $c; ?>"><?php echo $c; ?></option>
<?php endforeach; ?>
</select>
<button type="submit">Save</button>
</form>
</body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
Accept only the three known colours.
تەنها سێ ڕەنگە ناسراوەکە وەربگرە.
- Lineدێڕی 6
Store in the session: every page can read it now.
لە خەزنی بکە: ئێستا هەموو لاپەڕەیەک دەتوانێت بیخوێنێتەوە.
- Lineدێڕی 7-8
Redirect and stop (Post/Redirect/Get).
ئاڕاستە بکەرەوە و بوەستە (Post/Redirect/Get).
<?php
session_start();
$color = $_SESSION['favcolor'] ?? null;
?>
<!DOCTYPE html>
<html>
<body>
<?php if ($color === null): ?>
<p>You have not chosen a colour yet. <a href="choose.php">Choose one</a>.</p>
<?php else: ?>
<p style="color: <?php echo htmlspecialchars($color); ?>">Your favourite colour is <?php echo htmlspecialchars($color); ?>.</p>
<?php endif; ?>
</body>
</html>Your favourite colour is blue.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
<p style="color: blue">Your favourite colour is blue.</p>
</body>
</html>Write shop.php with three buttons (Tea, Sugar, Bread) and an “Empty the cart” button. Each click adds one item to a cart array in the session. Show how many items are in the cart and how many of each product.
shop.php بنووسە بە سێ دوگمە (Tea، Sugar، Bread) و دوگمەیەکی «Empty the cart». هەر یەک کاڵا دەخاتە ناو سەبەتە لە . پیشان بدە چەند کاڵا لە سەبەتەکەدان و لە هەر بەرهەمێک چەند دانە.
Hintئاماژە
Start with $_SESSION['cart'] = $_SESSION['cart'] ?? [];.
بە $_SESSION['cart'] = $_SESSION['cart'] ?? []; دەست پێبکە.
Show answerوەڵامەکە پیشان بدە
<?php
session_start();
$products = ['tea' => 'Tea', 'sugar' => 'Sugar', 'bread' => 'Bread'];
$_SESSION['cart'] = $_SESSION['cart'] ?? []; // an empty cart the first time
if (isset($_POST['add']) && isset($products[$_POST['add']])) {
$_SESSION['cart'][] = $_POST['add']; // add one item
}
if (isset($_POST['empty'])) {
$_SESSION['cart'] = []; // empty the cart
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post">
<?php foreach ($products as $key => $name): ?>
<button name="add" value="<?php echo $key; ?>">Add <?php echo $name; ?></button>
<?php endforeach; ?>
<button name="empty" value="1">Empty the cart</button>
</form>
<p>Items in your cart: <?php echo count($_SESSION['cart']); ?></p>
<ul>
<?php foreach (array_count_values($_SESSION['cart']) as $key => $n): ?>
<li><?php echo $products[$key] . " × " . $n; ?></li>
<?php endforeach; ?>
</ul>
</body>
</html>Items in your cart: 3 • Tea × 2 • Bread × 1
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
Create an empty cart the first time.
جاری یەکەم سەبەتەیەکی بەتاڵ دروست بکە.
- Lineدێڕی 7
[]adds one element to the end of the array.[]یەک دەخاتە کۆتایی .- Lineدێڕی 24
array_count_values()counts how often each product appears.array_count_values()دەژمێرێت هەر بەرهەمێک چەند جار هاتووە.
Add a page reset.php to the corrected page counter. It ends the session completely (variables, cookie and server data) and sends the visitor back to the counter, which then starts again at 1.
پەڕەی reset.php بۆ ژمێرەرە ڕاستکراوەکەی لاپەڕە زیاد بکە. بە تەواوی کۆتایی بە دەهێنێت (، و سەر ) و سەردانکەر دەگەڕێنێتەوە بۆ ، کە پاشان لە 1ـەوە دەست پێدەکاتەوە.
Hintئاماژە
Use the three logout steps, then header('Location: …'); exit;.
سێ هەنگاوەکەی بەکاربهێنە، پاشان header('Location: …'); exit;.
Show answerوەڵامەکە پیشان بدە
<?php
session_start();
$_SESSION = []; // remove all session variables
$p = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
session_destroy(); // delete the data on the server
header('Location: counter.php'); // start counting again
exit;You have accessed this page 1 times. You have accessed this page 2 times. (reset.php: 302 to counter.php, Set-Cookie: PHPSESSID=deleted; …) You have accessed this page 1 times.
Summary and reviewکورتە و پێداچوونەوە
- HTTP is stateless. A session remembers one user across many pages; the data stays on the server.
- The browser keeps only the session ID, in the
PHPSESSIDcookie. - Call
session_start()at the very top of every page that uses the session, before any output. - Store and read values with
$_SESSION['key']; they are visible at once and can be any PHP value. unset()removes one variable,session_unset()all of them,session_destroy()the server data; delete the cookie too for a full logout.- By default a session ends when the browser closes or after 24 minutes without use.
- Protect the ID:
session_regenerate_id(true)after login, an HttpOnly (and on HTTPS, Secure) session cookie.
- HTTP بێدۆخە. یەک لە چەندین لاپەڕەدا لە بیر دەگرێت؛ لەسەر دەمێننەوە.
- تەنها هەڵدەگرێت، لە
PHPSESSIDدا. - لە سەرەتای هەموو لاپەڕەیەک کە بەکار دەهێنێت، پێش هەر ،
session_start()بانگ بکە. - بە
$_SESSION['key']خەزن بکە و بیانخوێنەوە؛ دەستبەجێ دەبینرێن و دەتوانن هەر نرخێکی PHP بن. unset()یەک لادەبات،session_unset()هەموویان،session_destroy()سەر ؛ بۆ تەواو بسڕەوە.- بە شێوەی بنەڕەتی کاتێک دادەخرێت یان دوای ٢٤ خولەک بێ بەکارهێنان کۆتایی دێت.
- ناسێنەرەکە بپارێزە: دوای
session_regenerate_id(true)، HttpOnly (و لەسەر HTTPS، Secure).
What does session_start() do on the first visit, and on later visits?session_start() لە یەکەم سەرداندا چی دەکات و لە سەردانەکانی دواتردا؟
On the first visit it creates a new session with a random ID and sends it in a PHPSESSID cookie. Later it reads the ID that the browser sends and loads the saved data into $_SESSION.
لە یەکەم سەرداندا نوێ بە ناسێنەرێکی هەڕەمەکی دروست دەکات و لە PHPSESSIDدا دەینێرێت. دواتر ئەو ناسێنەرە دەخوێنێتەوە کە دەینێرێت و خەزنکراوەکان دەخاتە ناو $_SESSION.
Why might a late session_start() work on XAMPP but fail on another server?بۆچی لەوانەیە session_start()ێکی درەنگ لەسەر XAMPP کار بکات بەڵام لەسەر ڕاژەکارێکی تر شکست بهێنێت؟
XAMPP turns on output buffering (4096 bytes), so the headers are not sent yet when session_start() runs. A server without buffering has already sent them and shows a warning.
XAMPP هەڵدەکات (٤٠٩٦ )، بۆیە کاتێک session_start() کار دەکات سەردێڕەکان هێشتا نەنێردراون. بێ ڕاگرتن پێشتر ناردوونی و پیشان دەدات.
A page reads $_SESSION['favcolor'] and shows a warning. Give two reasons.لاپەڕەیەک $_SESSION['favcolor'] دەخوێنێتەوە و هۆشداری پیشان دەدات. دوو هۆکار بڵێ.
The page that sets the value was not visited yet (or the session expired), or this page forgot session_start(). Use ?? 'default' and call session_start() on every page.
ئەو لاپەڕەیەی دادەنێت هێشتا سەردان نەکراوە (یان بەسەرچووە)، یان ئەم لاپەڕەیە session_start()ی لە بیر چووە. ?? 'default' بەکاربهێنە و لە هەموو لاپەڕەیەکدا session_start() بانگ بکە.
Name three differences between cookies and sessions.سێ جیاوازیی نێوان کووکییەکان و سێشنەکان بڵێ.
Cookies live in the browser, sessions on the server; cookies hold only strings (about 4 KB), sessions any value; cookies can last for future visits, sessions end by default when the browser closes. Also: the visitor can edit cookies but not session data.
لە دەژین، لەسەر ؛ تەنها هەڵدەگرن (نزیکەی 4 KB)، هەر نرخێک؛ دەتوانن بۆ سەردانەکانی داهاتوو بمێننەوە، بە شێوەی بنەڕەتی کاتێک دادەخرێت کۆتاییان دێت. هەروەها: سەردانکەر دەتوانێت دەستکاری بکات بەڵام نا.
Which three steps make a complete logout?کام سێ هەنگاو چوونەدەرەوەیەکی تەواو دروست دەکەن؟
$_SESSION = [] (or session_unset()), delete the PHPSESSID cookie with setcookie() and a past time, and session_destroy(). Then redirect and exit.
$_SESSION = [] (یان session_unset())، سڕینەوەی PHPSESSID بە setcookie() و کاتێکی ڕابردوو و session_destroy(). پاشان ئاڕاستە بکەرەوە و exit.
Why call session_regenerate_id(true) after a successful login?بۆچی دوای چوونەژوورەوەی سەرکەوتوو session_regenerate_id(true) بانگ دەکەین؟
So that an ID known before login (for example one planted by an attacker) stops working. This prevents session fixation.
بۆ ئەوەی ناسێنەرێک کە پێش زانراوە (بۆ نموونە یەکێک کە هێرشبەر چاندوویەتی) کار نەکات. ئەمە ڕێگە لە دەگرێت.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
What a session is, the session ID cookie in the browser, session_start(), storing data in $_SESSION, and removing it with unset and destroy.
چییە، لە ، session_start()، هەڵگرتنی لەناو $_SESSION و سڕینەوەی بە unset() و session_destroy().
Whole video: an overview of the lesson.
هەموو ڤیدیۆکە: کورتەیەکی گشتیی وانەکە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
A Login System with Sessionsسیستەمی چوونەژوورەوە بە سێشن
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.