Chapterبەش 8 · Lectureوانە 8.1
PHP Superglobalsسووپەرگلۆباڵەکانی PHP
Superglobals are built-in arrays that PHP fills before your script runs: $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE, $_SESSION, $_FILES, $_ENV and $GLOBALS. Learn what each one holds, why you can use them everywhere, and how to read them safely.
ئامادەن کە PHP پێش کارکردنی خۆی پڕیان دەکاتەوە: $_GET، $_POST، $_REQUEST، $_SERVER، $_COOKIE، $_SESSION، $_FILES، $_ENV و $GLOBALS. فێر دەبیت هەریەکەیان چی تێدایە، بۆچی لە هەموو شوێنێک بەردەستن و چۆن بە سەلامەتی بیانخوێنیتەوە.
What you will learnچی فێر دەبیت
- Explain what a superglobal is and why it can be used inside functions without
global. - Name all nine superglobals and say what each one contains.
- Tell the difference between
$_GET,$_POSTand$_REQUEST. - Use
$_SERVERkeys such asREQUEST_METHODandPHP_SELF. - Read superglobals without warnings, using
isset()and??. - Recognise outdated or wrong names:
$php_errormsg,$_GLOBALS,$_COOKIES.
- ڕوون بکەیتەوە چییە و بۆچی بەبێ
globalلەناو بەکار دێت. - ناوی هەر نۆ بهێنیت و بڵێیت هەریەکەیان چی تێدایە.
- جیاوازیی نێوان
$_GETو$_POSTو$_REQUESTبزانیت. -
$_SERVERوەکREQUEST_METHODوPHP_SELFبەکاربهێنیت. - بەبێ بخوێنیتەوە، بە
isset()و??. - ناوە کۆن یان نادروستەکان بناسیتەوە:
$php_errormsg،$_GLOBALS،$_COOKIES.
Why superglobals matterبۆچی سووپەرگلۆباڵەکان گرنگن
Until now, your PHP pages only printed things. They did not listen. A real website must react to its visitors: what they typed in a , which link they clicked, which page they came from, which file they sent.
تا ئێستا لاپەڕەکانی PHPـت تەنها شتیان چاپ دەکرد و گوێیان لە سەردانکەر نەدەگرت. بەڵام ڕاستەقینە دەبێت کاردانەوەی هەبێت بەرامبەر بە سەردانکەرەکانی: چییان لە نووسیوە، کرتەیان لە کام کردووە، لە کام لاپەڕەوە هاتوون، یان چ ناردووە.
All of this information reaches your PHP code through . A superglobal is a special that PHP fills in for you, and that you can read from anywhere in your . As the course slides say, this is where a web page starts to listen.
هەموو ئەم زانیارییانە لە ڕێگەی دەگەنە کۆدی PHPـەکەت. تایبەتە کە PHP خۆی بۆت پڕی دەکاتەوە و لە هەر شوێنێکی دەتوانیت بیخوێنیتەوە. وەک کۆرسەکە دەڵێن: لێرەوە لاپەڕەی وێب دەست دەکات بە گوێگرتن.
In the course slides this topic opens Module 4, "Superglobals and Forms: moving data between pages, and redirecting the browser". The module covers the superglobals; GET, POST and REQUEST; cookies, sessions and file uploads; and browser redirection with header() — sending the visitor straight to another page, without them clicking anything. Redirection has its own lecture: sending the browser somewhere else.
لە کۆرسەکەدا ئەم بابەتە مۆدیولی ٤ دەکاتەوە: « و : گواستنەوەی زانیاری لە نێوان لاپەڕەکاندا و ». مۆدیولەکە ئەمانە دەگرێتەوە: ؛ GET و POST و REQUEST؛ ، و ؛ و بە header() — واتە ناردنی ڕاستەوخۆی سەردانکەر بۆ لاپەڕەیەکی تر، بێ ئەوەی کرتە لە هیچ شتێک بکات. وانەی تایبەتی خۆی هەیە: ناردنی تۆڕگەڕ بۆ شوێنێکی تر.
Think of a post office in Erbil. Before the clerk starts work each morning, the letters have already been sorted into labelled pigeonholes: one for letters with the address written on the envelope, one for sealed parcels, one for notes from regular customers, and so on. The clerk does not go out to collect anything; he just opens the right pigeonhole. PHP does the same: before your first line runs, it sorts everything that arrived with the visitor's into labelled arrays — $_GET, $_POST, $_COOKIE, $_FILES, $_SERVER — and your code simply opens the right one.
پۆستەخانەیەک لە هەولێر بهێنە بەرچاوت. پێش ئەوەی فەرمانبەرەکە هەموو بەیانییەک دەست بە کار بکات، نامەکان پێشتر لە کونە ناونووسکراوەکاندا ڕیز کراون: کونێک بۆ ئەو نامانەی ناونیشانیان لەسەر زەرفەکە نووسراوە، کونێک بۆ پاکەتە داخراوەکان، کونێک بۆ تێبینی کڕیارە هەمیشەییەکان و هتد. فەرمانبەرەکە ناچێتە دەرەوە بۆ کۆکردنەوەی هیچ شتێک؛ تەنها کونە گونجاوەکە دەکاتەوە. PHP هەمان کار دەکات: پێش ئەوەی یەکەم دێڕی کۆدەکەت کار بکات، هەموو ئەو شتانەی لەگەڵ سەردانکەرەکەدا هاتوون لە چەند ناونووسکراودا ڕێک دەخات — $_GET، $_POST، $_COOKIE، $_FILES، $_SERVER — و کۆدەکەت تەنها ئەوەی پێویستە دەیکاتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
A pigeonhole keeps letters for days. Superglobals are filled again for every single request and are emptied when the script ends — except $_SESSION, whose data PHP saves on the server between requests.
کونی پۆستەخانە نامەکان چەند ڕۆژێک هەڵدەگرێت. بەڵام بۆ هەر لە نوێوە پڕ دەکرێنەوە و کاتێک تەواو دەبێت بەتاڵ دەبنەوە — جگە لە $_SESSION کە PHP زانیارییەکانی لە نێوان لەسەر هەڵدەگرێت.
In this lecture you will meet all nine superglobals, learn what makes them "super", and learn to read them without warnings or security holes. The next lectures use them every day: form handling with GET and POST, validation and security and sticky forms.
لەم وانەیەدا هەر نۆ دەناسیت، فێر دەبیت چی وایان لێدەکات «سووپەر» بن و چۆن بەبێ و بەبێ کونی بیانخوێنیتەوە. وانەکانی دواتر هەموو ڕۆژێک بەکاریان دەهێنن: مامەڵەکردن لەگەڵ فۆرم بە GET و POST، پشتڕاستکردنەوە و ئاسایش و فۆرمە لکاوەکان.
What is a superglobal?سووپەرگلۆباڵ چییە؟
A built-in array variable that PHP fills in and that can be used everywhere, even inside functions. Examples: $_GET, $_POST, $_SERVER, $_FILES, $_COOKIE, $_SESSION and $GLOBALS.
گۆڕاوێکی ئامادەیە لە شێوەی ، کە PHP خۆی پڕی دەکاتەوە و لە هەموو شوێنێک، تەنانەت لەناو ، بەکار دێت. نموونە: $_GET، $_POST، $_SERVER، $_FILES، $_COOKIE، $_SESSION و $GLOBALS.
Superglobals are predefined variables: PHP creates them and fills them before your script starts. You never declare them yourself. They have four things in common:
پێشوەختە دیاریکراون: PHP خۆی دروستیان دەکات و پێش دەستپێکردنی پڕیان دەکاتەوە. تۆ هەرگیز خۆت ناساندنیان ناکەیت. چوار خاڵی هاوبەشیان هەیە:
- They are always in scope. You can read them inside any function or class, on any line, in any included file, without passing them in and without the
global. - Their names are written in capital letters. Most start with
$_(for example$_POST); the one exception is$GLOBALS, which has no underscore. - They are . You read a value by its , written as a in square brackets:
$_POST["go"],$_SERVER["PHP_SELF"]. - Variable names are .
$_POSTworks;$_postor$_Postis a different, empty variable.
- هەمیشە لەناو . دەتوانیت لەناو هەر یان ، لە هەر دێڕێک و لە هەر هاوردەکراودا بیانخوێنیتەوە، بەبێ ئەوەی بیاننێریتە ناو و بەبێ
global. - ناوەکانیان بە پیتی گەورە دەنووسرێن. زۆربەیان بە
$_دەست پێدەکەن (بۆ نموونە$_POST)؛ تەنها$GLOBALSجیاوازە و هێڵی ژێرەوەی (_) نییە. - . هەر بە خۆی دەخوێنیتەوە، کە وەک لەناو کەوانەی چوارگۆشەدا دەینووسیت:
$_POST["go"]،$_SERVER["PHP_SELF"]. - ناوی .
$_POSTکار دەکات؛ بەڵام$_postو$_Postدوو جیاواز و بەتاڵن.
What is a superglobal?سووپەرگلۆباڵ چییە؟
Example: a superglobal inside a functionنموونە: سووپەرگلۆباڵێک لەناو کردارێکدا
The page below is opened as greet.php?name=Shilan. The function uses an ordinary global variable ($siteName) and a superglobal ($_GET). Look at which one needs the global line.
ئەم لاپەڕەیەی خوارەوە وەک greet.php?name=Shilan دەکرێتەوە. ئاسایی ($siteName) و ($_GET) بەکار دەهێنێت. سەیر بکە کامیان پێویستی بە دێڕی global هەیە.
<?php
$siteName = "CêIT"; // an ordinary global variable
function greet() {
global $siteName; // an ordinary global needs "global" here
$name = $_GET["name"] ?? "guest"; // a superglobal needs nothing
echo "Welcome to $siteName, " . htmlspecialchars($name) . "!";
}
greet();Welcome to CêIT, Shilan!
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
An ordinary global variable must be imported into the function with
global.ئاسایی دەبێت بە
globalبهێنرێتە ناو .- Lineدێڕی 6
$_GETis a superglobal, so it is already visible here.?? "guest"gives a default when no name was sent.$_GET، بۆیە لێرەدا خۆی دیارە.?? "guest"دەدات ئەگەر هیچ ناوێک نەنێردرابێت.- Lineدێڕی 7
htmlspecialchars()makes the visitor's text safe to print. You will study it in the security lecture.htmlspecialchars()دەقی سەردانکەر بۆ چاپکردن سەلامەت دەکات. لە وانەی ئاسایشدا دەیخوێنیت.
Welcome to CêIT, guest!
Now remove the global line. The ordinary variable is no longer visible inside the function, and PHP 8 prints a :
ئێستا دێڕی global لاببە. ئیتر لەناو دیار نییە و PHP 8 چاپ دەکات:
<?php
$siteName = "CêIT";
function showSite() {
echo $siteName; // no "global" line
}
showSite();Warning: Undefined variable $siteName in C:\xampp\htdocs\forms\index.php on line 5
A superglobal never has this problem. That is exactly why form data, cookies and server information are given to you as superglobals: you can use them in any function you write.
هەرگیز ئەم کێشەیەی نییە. هەر بۆیە زانیاریی ، و زانیاریی وەک دەدرێنە دەستت: لە هەر کە خۆت دەینووسیت دەتوانیت بەکاریان بهێنیت.
The nine superglobalsنۆ سووپەرگلۆباڵەکە
PHP has exactly nine superglobals. The picture shows where the data of each one comes from: five are filled from the visitor's , four are prepared on the .
PHP ڕێک نۆ هەیە. وێنەکە پیشانی دەدات زانیاریی هەریەکەیان لە کوێوە دێت: پێنجیان لە سەردانکەرەوە پڕ دەکرێنەوە، چواریان لەسەر ئامادە دەکرێن.
| Superglobal | What it holdsچی تێدایە | Where its keys come from لە کوێوە دێن |
|---|---|---|
$GLOBALS | All global variables of the script (variables made outside any function).هەموو گشتییەکانی (ئەو لە دەرەوەی هەموو دروست کراون). | The variable names, without $: $GLOBALS["x"] is $x.ناوی بەبێ $: $GLOBALS["x"] هەمان $xـە. |
$_SERVER | Information about headers, paths, script locations and the current request.زانیاری دەربارەی ، ، شوێنی و ئێستاکە. | Predefined by PHP and the web server, e.g. PHP_SELF, REQUEST_METHOD.PHP و پێشوەختە دیاریان کردوون، وەک PHP_SELF و REQUEST_METHOD. |
$_GET | Values that arrive in the address bar: the URL (HTTP GET).ئەو لە دێن: URL (GET لە HTTP). | The names in the query string. ?city=Erbil gives the key city.ناوەکانی ناو . ?city=Erbil city دروست دەکات. |
$_POST | Values sent inside the body of an HTTP POST request, e.g. from <form method="post">.ئەو لەناو جەستەی POSTـی HTTPـدا دەنێردرێن، بۆ نموونە لە <form method="post">. | The name attributes of the form fields. nameـی . |
$_REQUEST | The contents of $_GET and $_POST (and $_COOKIE, if php.ini allows it) together.ناوەڕۆکی $_GET و $_POST (و $_COOKIE، ئەگەر php.ini ڕێگە بدات) پێکەوە. | The same keys as those arrays.هەمان ئەو . |
$_COOKIE | The the browser sent with this request. Singular: not $_COOKIES.ئەو لەگەڵ ئەم ناردوونی. ناوەکەی تاکە، نەک $_COOKIES. | The cookie names chosen when the cookie was created with setcookie().ناوی ، کە لە کاتی دروستکردنیان بە setcookie() هەڵبژێردران. |
$_SESSION | Session variables: data kept on the server for this visitor and shared by many pages. Needs session_start() first. : زانیاری کە لەسەر بۆ ئەم سەردانکەرە هەڵدەگیرێت و چەند لاپەڕەیەک بەکاری دەهێنن. سەرەتا session_start() پێویستە. | Names you choose: $_SESSION["user"] = "Hana";ئەو ناوانەی خۆت هەڵیاندەبژێریت: $_SESSION["user"] = "Hana"; |
$_FILES | Information about files uploaded with POST. It is a two-dimensional array.زانیاری دەربارەی ئەو بە POST بار کراون. دوو ڕەهەندییە. | First the file field's name, then name, type, tmp_name, error, size (and full_path since PHP 8.1).یەکەم nameـی ، پاشان name، type، tmp_name، error، size (و لە PHP 8.1ـەوە full_path). |
$_ENV | Environment variables of the system PHP runs on. It may be empty, depending on the variables_order setting. ژینگەی ئەو سیستەمەی PHP لەسەری کار دەکات. لەوانەیە بەتاڵ بێت، بەپێی variables_order. | Predefined by the operating system, e.g. PATH. پێشوەختە دیاری کردوون، وەک PATH. |
You will use $_GET, $_POST and $_SERVER in this chapter. $_COOKIE and $_SESSION have their own lectures (cookies, sessions), and $_FILES is explained in file uploads.
لەم بەشەدا $_GET و $_POST و $_SERVER بەکار دەهێنیت. $_COOKIE و $_SESSION وانەی تایبەت بە خۆیان هەیە (کووکی، سێشن) و $_FILES لە بارکردنی فایلدا ڕوون دەکرێتەوە.
$_GET, $_POST and $_REQUEST$_GET و $_POST و $_REQUEST
These three arrays receive what the visitor sent. The difference is where the data travelled inside the HTTP .
ئەم سێ ئەو شتانە وەردەگرن کە سەردانکەر ناردوونی. جیاوازییەکەیان لەوەدایە کە زانیارییەکان لەناو HTTPـدا بە کوێدا ڕۆیشتوون.
$_GET, $_POST and $_REQUEST$_GET و $_POST و $_REQUEST
$_GET: data in the URL$_GET: زانیاری لەناو URL
$_GET is an associative array of variables passed to the current script through the URL parameters, that is, with the HTTP GET method. A form with method="get" adds its fields to the end of the address as a :
$_GET کە ئەو تێدایە کە لە ڕێگەی URLـەوە، واتە بە ، بۆ ئێستاکە نێردراون. کە method="get"ـی هەبێت، وەک دەخاتە کۆتایی ناونیشانەکەوە:
<form method="get">
<input name="city">
</form>
<!-- after typing Erbil and pressing Enter, the address becomes: -->
<!-- page.php?city=Erbil -->Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
There is no
action, so the form is sent back to the same page.actionنییە، بۆیە بۆ هەمان لاپەڕە دەنێردرێتەوە.- Lineدێڕی 2
There is no submit button. A form with a single text field is still sent when the user presses Enter.
نییە. کە تەنها یەک هەبێت، بە لێدانی Enter هەر دەنێردرێت.
- Lineدێڕی 5-6
The slide wrote this line as
// page.php?city=Erbil. It is a note for you, not HTML; in HTML a comment is written<!-- -->.ئەم دێڕەی بە شێوەی
// page.php?city=Erbilنووسیوە. تێبینییە بۆ تۆ، نەک HTML؛ لە HTMLـدا بە<!-- -->دەنووسرێت.
In page.php the value is then waiting in $_GET["city"]. The key is the field's name; the value is what the user typed. Because the data is in the URL, the user can also type page.php?city=Duhok by hand — no form is needed. Never assume that $_GET came from your form.
لە page.phpدا لە $_GET["city"]دا چاوەڕێ دەکات. nameـی ؛ ئەوەیە کە نووسیویەتی. لەبەر ئەوەی زانیارییەکان لەناو URLـدان، دەتوانێت بە دەستیش page.php?city=Duhok بنووسێت — پێویستی بە نییە. هەرگیز وا دامەنێ کە $_GET لە خۆتەوە هاتووە.
$_GET, $_POST and $_REQUEST$_GET و $_POST و $_REQUEST
$_POST: data in the request body$_POST: زانیاری لەناو جەستەی داواکاری
$_POST is an associative array of variables passed to the current script with the HTTP POST method. The data travels inside the body of the request, not in the URL. Forms send POST data when they have method="post" (PHP fills $_POST for the normal form encoding application/x-www-form-urlencoded and for file-upload forms, multipart/form-data).
$_POST کە ئەو تێدایە کە بە بۆ ئێستاکە نێردراون. زانیارییەکان لەناو جەستەی دەڕۆن، نەک لەناو URL. کاتێک method="post"یان هەبێت زانیاری بە POST دەنێرن (PHP بۆ شێوەی ئاسایی ، application/x-www-form-urlencoded و بۆ ، multipart/form-data، $_POST پڕ دەکاتەوە).
<form method="post">
<input name="city">
</form>
<!-- nothing shows in the URL: the address stays page.php -->The full story of GET and POST — when to use which, length limits, what is safe — is in the next lecture.
هەموو چیرۆکی GET و POST — کەی کامیان بەکاربهێنیت، سنووری درێژی، چی سەلامەتە — لە وانەی داهاتوودایە.
$_GET, $_POST and $_REQUEST$_GET و $_POST و $_REQUEST
$_REQUEST: everything in one array$_REQUEST: هەموو شتێک لە یەک ڕیزەکدا
$_REQUEST is an associative array that contains the contents of $_GET, $_POST and $_COOKIE together. You can read a value without caring how it was sent.
$_REQUEST کە ناوەڕۆکی $_GET و $_POST و $_COOKIEی پێکەوە تێدایە. دەتوانیت بخوێنیتەوە بێ ئەوەی گرنگی بدەیت بە چ شێوەیەک نێردراوە.
In the example below the form was sent with POST to the address page.php?lang=ckb, so one value came in the URL and one in the body. print_r() prints the whole array:
لەم نموونەیەی خوارەوەدا بە POST بۆ ناونیشانی page.php?lang=ckb نێردرا، بۆیە لە URLـەوە هات و لە جەستەکەوە. print_r() هەموو چاپ دەکات:
<?php
echo "<pre>";
echo '$_GET = '; print_r($_GET);
echo '$_POST = '; print_r($_POST);
echo '$_REQUEST = '; print_r($_REQUEST);
echo "</pre>";$_GET = Array
(
[lang] => ckb
)
$_POST = Array
(
[city] => Erbil
)
$_REQUEST = Array
(
[lang] => ckb
[city] => Erbil
)HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>$_GET = Array
(
[lang] => ckb
)
$_POST = Array
(
[city] => Erbil
)
$_REQUEST = Array
(
[lang] => ckb
[city] => Erbil
)
</pre>When the same key arrives both in the URL and in the body, the later source in request_order wins: with "GP", the POST value replaces the GET value.
ئەگەر هەمان هەم لە URL و هەم لە جەستەکەدا بێت، ئەو سەرچاوەیەی لە request_orderدا دواترە سەردەکەوێت: بە "GP"، POST شوێنی GET دەگرێتەوە.
$_SERVER: about the request and the server$_SERVER: دەربارەی داواکاری و ڕاژەکار
$_SERVER holds information about headers, paths and script locations. The web server creates these entries; the keys are predefined. Here are the ones you will use most:
$_SERVER زانیاری دەربارەی ، و شوێنی تێدایە. ئەم بڕگانە دروست دەکات؛ پێشوەختە دیاری کراون. ئەمانە ئەوانەن کە زۆرتر بەکاریان دەهێنیت:
| Key | Meaningواتا | Example valueنموونەی نرخ |
|---|---|---|
PHP_SELF | The path of the running script, from the web root. ئەو کار دەکات، لە ڕەگی . | /test/info.php |
REQUEST_METHOD | How the page was requested: GET or POST.لاپەڕەکە چۆن داوا کراوە: GET یان POST. | GET |
QUERY_STRING | Everything after ? in the URL.هەموو شتێکی دوای ? لە URLـدا. | city=Erbil |
SERVER_NAME | The name of the host.ناوی خانەخوێ (هۆست). | localhost |
REMOTE_ADDR | The IP address of the visitor. سەردانکەر. | 127.0.0.1 |
HTTP_USER_AGENT | What the browser says about itself. The browser can lie.ئەوەی دەربارەی خۆی دەیڵێت. دەتوانێت درۆ بکات. | Mozilla/5.0 (…) Firefox/131.0 |
<?php
echo "PHP_SELF: " . htmlspecialchars($_SERVER["PHP_SELF"]) . "<br>";
echo "REQUEST_METHOD: " . $_SERVER["REQUEST_METHOD"] . "<br>";
echo "QUERY_STRING: " . htmlspecialchars($_SERVER["QUERY_STRING"]) . "<br>";
echo "SERVER_NAME: " . $_SERVER["SERVER_NAME"] . "<br>";
echo "REMOTE_ADDR: " . $_SERVER["REMOTE_ADDR"] . "<br>";
echo "HTTP_USER_AGENT: " . htmlspecialchars($_SERVER["HTTP_USER_AGENT"]);PHP_SELF: /test/info.php REQUEST_METHOD: GET QUERY_STRING: city=Erbil SERVER_NAME: localhost REMOTE_ADDR: 127.0.0.1 HTTP_USER_AGENT: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Firefox/131.0
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
PHP_SELF: /test/info.php<br>REQUEST_METHOD: GET<br>QUERY_STRING: city=Erbil<br>SERVER_NAME: localhost<br>REMOTE_ADDR: 127.0.0.1<br>HTTP_USER_AGENT: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Firefox/131.0Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2, 4, 7
These values contain text that the visitor controls, so they are escaped with
htmlspecialchars()before printing.ئەم دەقێکیان تێدایە کە سەردانکەر کۆنترۆڵی دەکات، بۆیە پێش چاپکردن بە
htmlspecialchars()سەلامەت دەکرێن.
REQUEST_METHOD is a reliable way to ask "was a form just sent?":
REQUEST_METHOD ڕێگایەکی متمانەپێکراوە بۆ ئەوەی بپرسیت «ئایا ئێستا نێردرا؟»:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
echo "The form was sent with POST.";
} else {
echo "This is a normal visit (GET). Please fill in the form.";
}This is a normal visit (GET). Please fill in the form.
The form was sent with POST.
$_COOKIE, $_SESSION, $_FILES, $_ENV and $GLOBALS$_COOKIE و $_SESSION و $_FILES و $_ENV و $GLOBALS
These five are explained fully in later lectures. Here is what you need to recognise them now.
ئەم پێنجە لە وانەکانی دواتردا بە تەواوی ڕوون دەکرێنەوە. ئەمە ئەوەیە کە ئێستا بۆ ناسینەوەیان پێویستتە.
$_COOKIE, $_SESSION, $_FILES, $_ENV and $GLOBALS$_COOKIE و $_SESSION و $_FILES و $_ENV و $GLOBALS
$_COOKIE$_COOKIE
$_COOKIE contains the cookies — small notes stored in the visitor's browser — that the browser sent back with this request. You create a cookie with setcookie(); $_COOKIE only reads the cookies that arrived. The name is singular: $_COOKIES does not exist.
$_COOKIE لەخۆ دەگرێت — تێبینییە بچووکەکان کە لە سەردانکەردا هەڵگیراون — کە لەگەڵ ئەم ناردوونیەتەوە. بە setcookie() دروست دەکەیت؛ $_COOKIE تەنها ئەو دەخوێنێتەوە کە گەیشتوون. ناوەکەی تاکە: $_COOKIES بوونی نییە.
<?php
// The browser sent the cookie: lang=ckb
echo $_COOKIE["lang"] . "<br>"; // correct: $_COOKIE (singular)
echo $_COOKIES["lang"] . "<br>"; // wrong: $_COOKIES does not existckb Warning: Undefined variable $_COOKIES in C:\xampp\htdocs\forms\page.php on line 4 Warning: Trying to access array offset on null in C:\xampp\htdocs\forms\page.php on line 4
$_COOKIE, $_SESSION, $_FILES, $_ENV and $GLOBALS$_COOKIE و $_SESSION و $_FILES و $_ENV و $GLOBALS
$_SESSION$_SESSION
$_SESSION holds session variables: information about this visitor that PHP keeps on the server and that many pages can use, for example who is logged in. You must call session_start() before you use it. HTTP itself forgets everything between requests; sessions are how a site remembers you.
$_SESSION لەخۆ دەگرێت: زانیاری دەربارەی ئەم سەردانکەرە کە PHP لەسەر هەڵیدەگرێت و چەندین لاپەڕە دەتوانن بەکاری بهێنن، بۆ نموونە کێ چووەتە ژوورەوە. پێش بەکارهێنانی دەبێت session_start() بانگ بکەیت. HTTP خۆی لە نێوان هەموو شتێک لەبیر دەکات؛ ئەو ڕێگایەن کە پێی لە بیرت دەکات.
$_COOKIE, $_SESSION, $_FILES, $_ENV and $GLOBALS$_COOKIE و $_SESSION و $_FILES و $_ENV و $GLOBALS
$_FILES$_FILES
$_FILES is a two-dimensional associative array with information about files uploaded to the script with the POST method. The first key is the name of the file field in the form; the second key chooses the piece of information: name (the original file name), type, tmp_name (where PHP saved it for now), error, size in bytes, and since PHP 8.1 full_path.
$_FILES دوو ڕەهەندییە کە زانیاری دەربارەی ئەو تێدایە کە بە POST بۆ نێردراون (). یەکەم ناوی (name) لە ؛ دووەم جۆری زانیارییەکە هەڵدەبژێرێت: name (ناوی سەرەکیی )، type، tmp_name (ئەو شوێنەی PHP بۆ ماوەیەکی کاتی تێیدا هەڵیگرتووە)، error، size بە و لە PHP 8.1ـەوە full_path.
<?php
echo $_FILES["fileUploaded"]["name"] . "<br>";
echo $_FILES["fileUploaded"]["size"] . " bytes";notes.pdf 48213 bytes
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
notes.pdf<br>48213 bytesUploading files needs a special form (enctype="multipart/form-data") and careful checks. See file uploads.
پێویستی بە تایبەت (enctype="multipart/form-data") و پشکنینی ورد هەیە. بڕوانە بارکردنی فایل.
$_COOKIE, $_SESSION, $_FILES, $_ENV and $GLOBALS$_COOKIE و $_SESSION و $_FILES و $_ENV و $GLOBALS
$_ENV and $GLOBALS$_ENV و $GLOBALS
$_ENV contains information about the environment PHP runs in (environment variables such as PATH). Its keys are predefined by the system. On many installations it is empty because of the variables_order setting in php.ini; getenv("PATH") reads one environment variable directly.
$_ENV زانیاری دەربارەی ئەو ژینگەیەی PHP تێیدا کار دەکات لەخۆ دەگرێت ( ژینگە، وەک PATH). سیستەمەکە پێشوەختە دیاریان دەکات. لە زۆر دامەزراندندا بەتاڵە بەهۆی variables_order لە php.iniدا؛ getenv("PATH") ڕاستەوخۆ یەک ژینگە دەخوێنێتەوە.
$GLOBALS stores all global variables of the script. The variable names are the keys. It lets a function read or change a global variable without the global keyword:
$GLOBALS هەموو گشتییەکانی هەڵدەگرێت. ناوی . ڕێگە بە دەدات بخوێنێتەوە یان بیگۆڕێت بەبێ global:
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS["z"] = $GLOBALS["x"] + $GLOBALS["y"];
}
addition();
echo $z;100
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
Reads
$xand$ythrough$GLOBALSand creates a new global variable$z.$xو$yلە ڕێگەی$GLOBALSـەوە دەخوێنێتەوە و گشتیی نوێ بە ناوی$zدروست دەکات.
$php_errormsg: removed in PHP 8$php_errormsg: لە PHP 8دا لابرا
The course slides describe $php_errormsg as "a variable containing the text of the last error message generated by PHP", and list it among the superglobals.
کۆرسەکە $php_errormsg وا وەسف دەکەن: « کە دەقی دوایین PHPـی تێدایە» و دەیخەنە ناو لیستی .
To read the last error message today, call error_get_last(). It returns an array with the keys type, message, file and line (or null if there was no error):
ئەمڕۆ بۆ خوێندنەوەی دوایین ، error_get_last() بانگ بکە. دەگەڕێنێتەوە کە type، message، file و lineـی تێدایە (یان null ئەگەر هیچ نەبووبێت):
<?php
$file = @fopen("no-such-file.txt", "r");
if ($file === false) {
$error = error_get_last();
echo "Error: " . $error["message"];
}Error: fopen(no-such-file.txt): Failed to open stream: No such file or directory
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The
@operator hides the warning, so that we can show our own message.@دەشارێتەوە، تاکو پەیامی خۆمان پیشان بدەین.- Lineدێڕی 5
error_get_last()gives the details of the last error.error_get_last()وردەکاریی دوایین دەدات.
For many newer functions, errors are reported with instead, which you catch with try … catch.
بۆ زۆرێک لە نوێیەکان، لە جیاتی ئەوە بە ڕادەگەیەندرێن، کە بە try … catch دەیانگریت.
Reading superglobals safelyخوێندنەوەی سەلامەتی سووپەرگلۆباڵەکان
A superglobal only contains what was really sent. If you read a key that is not there, PHP 8 prints a warning such as Undefined array key "city". Before you read a value, check that it exists:
تەنها ئەوەی تێدایە کە بەڕاستی نێردراوە. ئەگەر بخوێنیتەوە کە بوونی نییە، PHP 8 وەک Undefined array key "city" چاپ دەکات. پێش خوێندنەوەی ، دڵنیا ببەوە کە هەیە:
isset($_GET["city"])istrueonly if the key exists (and is notnull).$_GET["city"] ?? "unknown"gives the value if it exists, or the default after??— without any warning.??is the .
isset($_GET["city"])تەنها کاتێکtrueـە کە هەبێت (وnullنەبێت).$_GET["city"] ?? "unknown"دەدات ئەگەر هەبێت، ئەگینا بنەڕەتییەکەی دوای??— بەبێ هیچ .??بە ئینگلیزی پێی دەوترێت .
<?php
// The page was opened as: page.php?name=Hana
$name = $_GET["name"] ?? "guest"; // the key was sent
$city = $_GET["city"] ?? "unknown"; // the key was not sent: use a default
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "City: " . htmlspecialchars($city) . "<br>";
if (isset($_GET["city"])) {
echo "A city was sent.";
} else {
echo "No city was sent.";
}Name: Hana City: unknown No city was sent.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Name: Hana<br>City: unknown<br>No city was sent.Common mistakesهەڵە باوەکان
The page below is opened as page.php?name=Hana. Only the first line is correct; each of the others makes a typical mistake.
ئەم لاپەڕەیەی خوارەوە وەک page.php?name=Hana کراوەتەوە. تەنها دێڕی یەکەم دروستە؛ هەریەک لە دێڕەکانی تر هەڵەیەکی باو دەکات.
<?php
// The page was opened as: page.php?name=Hana
echo $_GET["name"] . "<br>"; // correct
echo $_get["name"] . "<br>"; // wrong: lowercase name
echo $_GET["Name"] . "<br>"; // wrong: key "Name" is not "name"
echo $_GET["city"] . "<br>"; // wrong: this key was never sentHana Warning: Undefined variable $_get in C:\xampp\htdocs\forms\page.php on line 4 Warning: Trying to access array offset on null in C:\xampp\htdocs\forms\page.php on line 4 Warning: Undefined array key "Name" in C:\xampp\htdocs\forms\page.php on line 5 Warning: Undefined array key "city" in C:\xampp\htdocs\forms\page.php on line 6
- Wrong capital letters.
$_get,$_Post: variable names are case-sensitive. Always write superglobals in capitals. - Wrong key spelling. Keys are case-sensitive too:
name="email"in the form must be read as$_POST["email"], not$_POST["Email"]. - Wrong names.
$_COOKIESand$_GLOBALSdo not exist. The correct names are$_COOKIEand$GLOBALS. - Reading a key that was not sent without
isset()or??gives a warning. - Unquoted keys.
$_FILES[photo][name]is a fatal error in PHP 8; write$_FILES["photo"]["name"]. - Looking in the wrong array. If the form uses
method="post", the data is in$_POST, and$_GETis empty. - Printing user input as it is. Always escape it with
htmlspecialchars(). - Using
$php_errormsg. It was removed in PHP 8; useerror_get_last().
- قەبارەی نادروستی پیت.
$_get،$_Post: ناوی هەستیارە بە قەبارەی پیت. هەمیشە بە پیتی گەورە بنووسە. - ڕێنووسی نادروستی . هەستیارن بە قەبارەی پیت:
name="email"لە دەبێت وەک$_POST["email"]بخوێنرێتەوە، نەک$_POST["Email"]. - ناوی نادروست.
$_COOKIESو$_GLOBALSبوونیان نییە. ناوە دروستەکان$_COOKIEو$GLOBALSـن. - خوێندنەوەی کە نەنێردراوە بەبێ
isset()یان??دەدات. - بێ گوتە.
$_FILES[photo][name]لە PHP 8دا ؛ بنووسە$_FILES["photo"]["name"]. - گەڕان لە نادروستدا. ئەگەر
method="post"بەکاربهێنێت، زانیارییەکان لە$_POSTدان و$_GETبەتاڵە. - چاپکردنی وەک خۆی. هەمیشە بە
htmlspecialchars()هێما تایبەتەکانی بگۆڕە. - بەکارهێنانی
$php_errormsg. لە PHP 8دا لابرا؛error_get_last()بەکاربهێنە.
Practiceڕاهێنان
Write hello.php. Inside a function sayHello(), read the name from the URL (hello.php?name=Karwan) and print Hello, Karwan!. If no name was sent, print Hello, guest!. Do not use the global keyword, and make the output safe.
hello.php بنووسە. لەناو sayHello()دا ناوەکە لە URL بخوێنەوە (hello.php?name=Karwan) و Hello, Karwan! چاپ بکە. ئەگەر هیچ ناوێک نەنێردرابوو، Hello, guest! چاپ بکە. global بەکارمەهێنە و سەلامەت بکە.
Hintئاماژە
$_GET works inside functions. Use ?? for the default and htmlspecialchars() for printing.
$_GET لەناو کار دەکات. بۆ ?? و بۆ چاپکردن htmlspecialchars() بەکاربهێنە.
Show answerوەڵامەکە پیشان بدە
<?php
function sayHello() {
$name = $_GET["name"] ?? "guest";
echo "Hello, " . htmlspecialchars($name) . "!";
}
sayHello();Hello, Karwan!
Hello, guest!
This page is opened as page.php?lang=ckb, and the browser also sends the cookie theme=dark. Every line inside info() prints a warning. Find the three mistakes and fix them.
ئەم لاپەڕەیە وەک page.php?lang=ckb دەکرێتەوە و theme=darkیش دەنێرێت. هەموو دێڕێکی ناو info() چاپ دەکات. سێ بدۆزەوە و چاکیان بکەرەوە.
<?php
$siteName = "CêIT";
function info() {
echo $_GLOBALS["siteName"] . "<br>";
echo $_COOKIES["theme"] . "<br>";
echo $_get["lang"] . "<br>";
}
info();Warning: Undefined variable $_GLOBALS in C:\xampp\htdocs\forms\page.php on line 5 Warning: Trying to access array offset on null in C:\xampp\htdocs\forms\page.php on line 5 Warning: Undefined variable $_COOKIES in C:\xampp\htdocs\forms\page.php on line 6 Warning: Trying to access array offset on null in C:\xampp\htdocs\forms\page.php on line 6 Warning: Undefined variable $_get in C:\xampp\htdocs\forms\page.php on line 7 Warning: Trying to access array offset on null in C:\xampp\htdocs\forms\page.php on line 7
Show answerوەڵامەکە پیشان بدە
$_GLOBALS→$GLOBALS(no underscore).$_COOKIES→$_COOKIE(singular).$_get→$_GET(capital letters).
$_GLOBALS←$GLOBALS(بێ هێڵی ژێرەوە).$_COOKIES←$_COOKIE(تاک).$_get←$_GET(پیتی گەورە).
We also escape the visitor's values and give defaults with ??:
هەروەها سەردانکەر سەلامەت دەکەین و بە ?? دەدەین:
<?php
$siteName = "CêIT";
function info() {
echo $GLOBALS["siteName"] . "<br>";
echo htmlspecialchars($_COOKIE["theme"] ?? "") . "<br>";
echo htmlspecialchars($_GET["lang"] ?? "") . "<br>";
}
info();CêIT dark ckb
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
CêIT<br>dark<br>ckb<br>Name the superglobal you would use for each job: (a) the search word in search.php?q=php; (b) the password from a login form with method="post"; (c) the name of a photo the user uploaded; (d) the path of the current script; (e) the user name you saved at login, on another page; (f) a global variable $total, inside a function.
بۆ هەر کارێک ناوی ئەو بنووسە کە بەکاری دەهێنیت: (أ) لە search.php?q=php؛ (ب) لە کە method="post"ـی هەیە؛ (ج) ناوی ئەو وێنەیەی باری کردووە؛ (د) ئێستاکە؛ (هـ) ئەو لە کاتی هەڵتگرتووە، لە لاپەڕەیەکی تردا؛ (و) بە ناوی $total، لەناو .
Show answerوەڵامەکە پیشان بدە
- (a)
$_GET["q"] - (b)
$_POST["password"](whatever the field'snameis) - (c)
$_FILES["photo"]["name"] - (d)
$_SERVER["PHP_SELF"] - (e)
$_SESSION["user"](aftersession_start()) - (f)
$GLOBALS["total"]
- (أ)
$_GET["q"] - (ب)
$_POST["password"](هەرnameێک هەیبێت) - (ج)
$_FILES["photo"]["name"] - (د)
$_SERVER["PHP_SELF"] - (هـ)
$_SESSION["user"](دوایsession_start()) - (و)
$GLOBALS["total"]
What is a superglobal?سووپەرگلۆباڵ چییە؟
A predefined variable, always an associative array, that PHP fills for every request and that can be used in every scope — even inside functions — without global.
پێشوەختە دیاریکراوە، هەمیشە ، کە PHP بۆ هەر پڕی دەکاتەوە و لە هەموو — تەنانەت لەناو — بەبێ global بەکار دێت.
List the nine superglobals.نۆ سووپەرگلۆباڵەکە بژمێرە.
$GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV.
$GLOBALS، $_SERVER، $_GET، $_POST، $_FILES، $_COOKIE، $_SESSION، $_REQUEST، $_ENV.
What is the difference between $_GET and $_POST?جیاوازیی نێوان $_GET و $_POST چییە؟
$_GET holds values sent in the URL query string (GET method); $_POST holds values sent in the body of a POST request.
$_GET ئەو تێدایە کە لە URLـدا نێردراون ()؛ $_POST ئەو تێدایە کە لە جەستەی POSTـدا نێردراون.
Is $php_errormsg a superglobal? What should you use?ئایا $php_errormsg سووپەرگلۆباڵە؟ چی بەکاربهێنیت؟
No. It was an ordinary variable that needed track_errors, and it was removed in PHP 8.0. Use error_get_last() (or exceptions).
نەخێر. ئاسایی بوو کە پێویستی بە track_errors بوو و لە PHP 8.0دا لابرا. error_get_last() (یان ) بەکاربهێنە.
Why is $_REQUEST not recommended?بۆچی $_REQUEST پێشنیار ناکرێت؟
You cannot tell where a value came from, and what it contains depends on php.ini (request_order), so code can behave differently on another server.
ناتوانیت بزانیت لە کوێوە هاتووە و ناوەڕۆکەکەی بەندە بە php.ini (request_order)، بۆیە کۆد لەسەر تر دەتوانێت جیاواز ڕەفتار بکات.
How do you read $_GET["page"] without a warning when it may be missing?چۆن $_GET["page"] بەبێ هۆشداری دەخوێنیتەوە کاتێک لەوانەیە نەبێت؟
Check with isset($_GET["page"]), or use a default: $_GET["page"] ?? "home".
بە isset($_GET["page"]) بیپشکنە، یان بەکاربهێنە: $_GET["page"] ?? "home".
Summaryپوختە
- Superglobals are nine built-in associative arrays that PHP fills before your script runs.
- They are available in every scope, so functions can use them without
global. $_GET= URL query string;$_POST= request body;$_REQUEST= both (cookies depend onphp.ini).$_SERVERdescribes the request and the server;$_COOKIEreads cookies;$_SESSIONkeeps data on the server;$_FILESdescribes uploads;$_ENVholds environment variables;$GLOBALSholds your global variables.$php_errormsgis gone (PHP 8): useerror_get_last().- Check keys with
isset()or??, and escape everything the visitor sent before you print it.
- نۆ ئامادەن کە PHP پێش کارکردنی پڕیان دەکاتەوە.
- لە هەموو بەردەستن، بۆیە بەبێ
globalبەکاریان دەهێنن. $_GET= URL؛$_POST= ؛$_REQUEST= هەردووکیان ( بەندە بەphp.ini).$_SERVERو وەسف دەکات؛$_COOKIEدەخوێنێتەوە؛$_SESSIONزانیاری لەسەر هەڵدەگرێت؛$_FILESبارکراوەکان وەسف دەکات؛$_ENVژینگەی تێدایە؛$GLOBALSگشتییەکانی خۆتی تێدایە.$php_errormsgنەماوە (PHP 8):error_get_last()بەکاربهێنە.- بە
isset()یان??بپشکنە و هەموو ئەوەی سەردانکەر ناردوویەتی پێش چاپکردن سەلامەت بکە.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
What superglobals are, and a short tour of $_SERVER, $_GET, $_POST, $_REQUEST, $_FILES, $_COOKIE, $_SESSION and $_ENV with small examples.
چییە و ناساندنێکی کورتی $_SERVER، $_GET، $_POST، $_REQUEST، $_FILES، $_COOKIE، $_SESSION و $_ENV، بە نموونەی بچووک.
Stops at 13:57, before the preview of upcoming lessons
لە 13:57 دەوەستێت، پێش ناساندنی وانەکانی داهاتوو.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Form Handling with GET and POSTمامەڵەکردن لەگەڵ فۆرم بە GET و POST
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 ن.