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 خەزن بکە و بیانخوێنەوە، بە تەواوی کۆتایی بە بهێنە، لەگەڵ بەراورد بکە و پارێزراو ڕابگرە.

23 min read21 خولەک خوێندنەوە · 19 stepsهەنگاو

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 هەموو شتێک لە بیر دەکات و ئەو ڕێگایەن کە پێی لە بیرت دەگرێت.

The tailor’s receiptپسوولەی بەرگدروو

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سێشن چییە

Definitionپێناسە

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:

چۆن دەزانێت کام هی کام سەردانکەرە؟ بە فێڵێکی بچووک:

  1. session_start() gives each new visitor a long random , for example a1b2c3….
  2. PHP sends this ID to the browser in a cookie named PHPSESSID. This is the only thing the browser stores.
  3. The data itself stays on the server, in a file named after the ID (sess_a1b2c3…).
  4. On every later request the browser sends the cookie back. session_start() reads the ID, opens the matching file and fills $_SESSION with its data.
  1. session_start() هەڕەمەکیی درێژ دەدات بە هەر سەردانکەرێکی نوێ، بۆ نموونە a1b2c3….
  2. PHP ئەم ناسێنەرە لە بە ناوی PHPSESSID دەنێرێت بۆ . ئەمە تاکە شتە کە هەڵیدەگرێت.
  3. خۆیان لەسەر دەمێننەوە، لە فایلێکدا کە ناوەکەی لە ناسێنەرەکەوە وەرگیراوە (sess_a1b2c3…).
  4. لە هەموو دواتردا دەنێرێتەوە. session_start() ناسێنەرەکە دەخوێنێتەوە، فایلە هاوتاکە دەکاتەوە و $_SESSION بە پڕ دەکات.
Two browsers on the left, Aram's and Shilan's. Each stores only one cookie with a random id, PHPSESSID=a1b2c3… and PHPSESSID=9f8e7d…, and sends it to the server. On the right, the server's session store holds two files: sess_a1b2c3… with user_id => 7, username => 'Aram' and cart => ['tea', 'sugar'], and sess_9f8e7d… with favcolor => 'green' and favanimal => 'cat'. session_start() reads the id from the cookie and loads the matching file into $_SESSION. Notes: closing the browser deletes the PHPSESSID cookie (default lifetime 0); a file not used for 24 minutes may be deleted (session.gc_maxlifetime = 1440).دوو تۆڕگەڕ لە لای چەپ، هی ئارام و هی شیلان. هەریەکەیان تەنها یەک کووکی هەڵدەگرێت کە ناسێنەرێکی هەڕەمەکیی تێدایە، PHPSESSID=a1b2c3… و PHPSESSID=9f8e7d… و دەینێرێت بۆ ڕاژەکار. لە لای ڕاست، کۆگای سێشنی ڕاژەکار دوو فایلی تێدایە: sess_a1b2c3… کە user_id => 7 و username => 'Aram' و cart => ['tea', 'sugar']ی تێدایە و sess_9f8e7d… کە favcolor => 'green' و favanimal => 'cat'ی تێدایە. session_start() ناسێنەرەکە لە کووکییەکە دەخوێنێتەوە و فایلە هاوتاکە دەخاتە ناو $_SESSION. تێبینییەکان: داخستنی تۆڕگەڕ کووکیی PHPSESSID دەسڕێتەوە (ماوەی بنەڕەتیی 0)؛ فایلێک کە ٢٤ خولەک بەکار نەهێنرێت لەوانەیە بسڕدرێتەوە (session.gc_maxlifetime = 1440).
The data stays on the server; only the random session ID travels in a cookie. لەسەر دەمێننەوە؛ تەنها هەڕەمەکی لە دەڕوات و دێتەوە.CêIT
Quick checkپشکنینی خێراWhat does the browser store for a PHP session? بۆ PHP چی هەڵدەگرێت؟

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 PHPSESSID cookie has the lifetime 0 (setting session.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_maxlifetime seconds: 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⁩ چرکە = ٢٤ خولەک. بۆیە ئەگەر سەردانکەر ماوەیەک هیچ نەکات، کۆتایی دێت، تەنانەت ئەگەر کراوە بێت.
settings.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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');

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 بە خەزنکراوەکان پڕ دەکات.

start.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?PHP
session_start();
?>
<html>
<head>  </head>
<body>  </body>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

<?PHP in capitals is valid: the opening tag is not case-sensitive. Lower case <?php is 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 output⁦session_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.

late.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<html>
<body>
<?php
session_start();
$_SESSION['views'] = 1;
echo "Pageviews=" . $_SESSION['views'];
?>
</body>
</html>
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.

زۆر درەنگە: دەست پێناکات و هیچ شتێک خەزن ناکرێت.

Left: a 'wrong order' file where <html> and <body> come first and session_start() and header() follow on lines 4 and 5, both marked 'Warning: too late'; below it a 'right order' file where session_start(), header() and exit come first and the HTML comes last. Right: one HTTP response, with part 1 the headers (HTTP/1.1 302 Found, Set-Cookie: PHPSESSID=…, Location: index.php), an empty line, and part 2 the body (<html>, <body>). The first byte of the body closes the headers. A note: XAMPP's output_buffering = 4096 can hide the wrong order on your computer.لای چەپ: فایلێکی «ڕیزبەندیی هەڵە» کە <html> و <body> سەرەتا دێن و session_start() و header() لە دێڕی ٤ و ٥دا دێن، هەردووکیان نیشانەی «هۆشداری: درەنگە»یان لەسەرە؛ لە خوارەوەی فایلێکی «ڕیزبەندیی دروست» کە session_start() و header() و exit سەرەتا دێن و HTML لە کۆتاییدا. لای ڕاست: یەک وەڵامی HTTP، بەشی ١: سەردێڕەکانی HTTP (HTTP/1.1 302 Found، Set-Cookie: PHPSESSID=…، Location: index.php)، دێڕێکی بەتاڵ و بەشی ٢ ناوەڕۆکە (<html>، <body>). یەکەم بایتی ناوەڕۆک سەردێڕەکان دادەخات. تێبینییەک: output_buffering = 4096ـی XAMPP دەتوانێت ڕیزبەندیی هەڵە لە کۆمپیوتەرەکەتدا بشارێتەوە.
Headers first, body second. session_start(), setcookie() and header() all send headers, so they must run before the first output.سەرەتا ، پاشان ناوەڕۆک. session_start()، setcookie() و header() هەموویان سەردێڕ دەنێرن، بۆیە دەبێت پێش یەکەم کار بکەن.CêIT
Quick checkپشکنینی خێراA page prints HTML and then calls session_start(). On a server without output buffering, what happens?لاپەڕەیەک HTML چاپ دەکات و پاشان session_start() بانگ دەکات. لەسەر بەبێ ، چی ڕوودەدات؟

PHP Sessions & Cookies - Output Buffering - Headers Already Sent Warning - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 36K viewsبینین · 1.3K likesلایک · Partبەشی 0:36–6:09 (ofلە 14:55)

PHP 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ـە. : هەڵدەبژێریت و پێ دەدەیت و بە هەمان دەیخوێنیتەوە.

views.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
session_start();        // store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php   //retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</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 views gets the value 1.

خەزنکردن: views 1 وەردەگرێت.

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() بانگ بکات. کۆرسەکە تەنها لاپەڕەی خوێندنەوە پیشان دەدات؛ ئەمە لاپەڕەی دانانی :

page1.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>
page2.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

This page needs session_start() too. Without it, $_SESSION does 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⁩ ئاگادار دەکاتەوە:

page2.php in a new browser⁦page2.php⁩ لە تۆڕگەڕێکی نوێدا
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 .
page2.php (safe)PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>
Quick checkپشکنینی خێراpage2.php reads $_SESSION["favcolor"] but has no session_start(). Page 1 has already set the value. What happens?page2.php $_SESSION["favcolor"] دەخوێنێتەوە بەڵام session_start()ی تێدا نییە. لاپەڕەی ١ پێشتر داناوە. چی ڕوودەدات؟

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 لەسەر خەزنی دەکات و لە لاپەڕەی داهاتوودا بێ گۆڕان دەیگەڕێنێتەوە. لێرەدا سەبەتەی کڕین لە ناو :

cart.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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']);

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 دەوەستێت (بڕوانە کووکییەکان).

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 زیاد دەکات. ئەمە کۆدی سلایدەکەیە وەک خۆی:

example1.php (slide)PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

On the first visit the key intCount does not exist yet, so reading it gives a warning.

لە یەکەم سەرداندا intCount هێشتا بوونی نییە، بۆیە خوێندنەوەی دەدات.

Lineدێڕی 4-6

An if/else without braces, with else on the same line as the first statement: legal, but hard to read.

if/elseێک بەبێ کەوانەی لوولپێچ و else لەسەر هەمان دێڕی یەکەم ڕستەدا: یاساییە، بەڵام خوێندنەوەی قورسە.

example1.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>
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;. ئەم ژمێرەرە لەگەڵ ژمێرەری کووکی بەراورد بکە: لەوێ ژمارەکە لە سەردانکەردا دەژی و دەتوانرێت دەستکاری بکرێت؛ لێرە لەسەر دەژی و سەردانکەر ناتوانێت بیگۆڕێت.

Quick checkپشکنینی خێراWhich line adds 1 to a session counter without a warning, also on the first visit?کام دێڕ بەبێ ، لە یەکەم سەردانیشدا، 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() بەکار دەهێنێت:

destroy.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>
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 ($_SESSION becomes 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:

چەند ئامرازێک هەن و هەریەکەیان کارێکی جیاواز دەکات. ئەم تاقیکردنەوەیە یەک لە دوای یەک پیشانیان دەدات:

destroy-check.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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)";
Four ways to remove session dataچوار ڕێگا بۆ لابردنی
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. سەر دەمێننەوە، مەگەر ئەوانیش لەناو ببەیت.
Quick checkپشکنینی خێراWhich line removes only the session variable color?کام دێڕ تەنها colorـی لادەبات؟

Ending a sessionکۆتاییهێنان بە سێشن

A complete logoutچوونەدەرەوەیەکی تەواو

logout.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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.";
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.

هەنگاوی ٣: لەسەر بسڕەوە.

Quick checkپشکنینی خێراAfter session_destroy(), what is still left?دوای session_destroy()، چی هێشتا ماوە؟

Cookies and sessions comparedبەراوردی کووکییەکان و سێشنەکان

The course names three general differences between cookies and sessions:

کۆرسەکە سێ جیاوازیی گشتی لە نێوان و دەستنیشان دەکات:

  1. The main difference: cookies are stored on the client side, sessions on the server side.
  2. Cookies can only store strings. A session can store objects (and any other PHP value).
  3. A cookie can be saved for future visits, but a session cannot: when users close their browser, they also lose the session (by default).
  1. جیاوازیی سەرەکی: لە خەزن دەکرێن، لە لای .
  2. تەنها دەتوانن خەزن بکەن. دەتوانێت (و هەر نرخێکی تری PHP) خەزن بکات.
  3. دەتوانرێت بۆ سەردانەکانی داهاتوو هەڵبگیرێت، بەڵام ناتوانرێت: کاتێک بەکارهێنەران دادەخەن، لە دەست دەدەن (بە شێوەی بنەڕەتی).

The course summary and the deck add more points. Together:

کورتەی کۆرسەکە و سلایدەکان خاڵی زیاتر زیاد دەکەن. پێکەوە:

Cookie or session? یان ؟
QuestionپرسیارCookieSession
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() / $_COOKIEsession_start() / $_SESSION
Good forباشە بۆHarmless preferences that should last: language, themeئارەزووی بێ‌زیان کە دەبێت بمێنێتەوە: زمان، ڕووکارLogins, shopping carts, anything private or important، سەبەتەی کڕین، هەر شتێکی تایبەت یان گرنگ
Quick checkپشکنینی خێراWhere is a cookie kept, and where is a session kept? لە کوێ هەڵدەگیرێت و لە کوێ؟

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). ناسێنەرە کۆنەکە بێ‌سوود دەبێت:

regen.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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();
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

true also deletes the old session file on the server.

true کۆنی لەسەر دەسڕێتەوە.

Quick checkپشکنینی خێراWhy is $_SESSION['user_id'] safer than a cookie user_id?بۆچی $_SESSION['user_id'] سەلامەتترە لە user_id؟

26 | Session Security Basics in PHP for Beginners | 2023 | Learn PHP Full Course For BeginnersDani Krossing · Englishئینگلیزی · 49K viewsبینین · 1.3K likesلایک · Partبەشی 11:25–14:54 (ofلە 16:32)

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() و گۆڕینی هەر ماوەیەک جارێک.

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 لە جیاتی $_SESSIONA 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ڕاهێنان و پشکنینی خێرا

ExerciseڕاهێنانChoose a favourite colourڕەنگێکی دڵخواز هەڵبژێرە

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وەڵامەکە پیشان بدە
choose.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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)⁩.

show.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>
ExerciseڕاهێنانA shopping cartسەبەتەی کڕین

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وەڵامەکە پیشان بدە
shop.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>
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() دەژمێرێت هەر بەرهەمێک چەند جار هاتووە.

ExerciseڕاهێنانReset the counterژمێرەرەکە سفر بکەرەوە

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وەڵامەکە پیشان بدە
reset.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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;
Quick checkپشکنینی خێراWhat can a session variable hold that a cookie cannot? چی دەتوانێت هەڵبگرێت کە ناتوانێت؟

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 PHPSESSID cookie.
  • 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).
Review questionsپرسیارەکانی پێداچوونەوە
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

25 | How to Create Sessions in PHP for Beginners | 2023 | Learn PHP Full Course For BeginnersDani Krossing · Englishئینگلیزی · 77K viewsبینین · 1.6K likesلایک

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

A Login System with Sessionsسیستەمی چوونەژوورەوە بە سێشن

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.