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. فێر دەبیت هەریەکەیان چی تێدایە، بۆچی لە هەموو شوێنێک بەردەستن و چۆن بە سەلامەتی بیانخوێنیتەوە.

19 min read19 خولەک خوێندنەوە · 20 stepsهەنگاو

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, $_POST and $_REQUEST.
  • Use $_SERVER keys such as REQUEST_METHOD and PHP_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() — واتە ناردنی ڕاستەوخۆی سەردانکەر بۆ لاپەڕەیەکی تر، بێ ئەوەی کرتە لە هیچ شتێک بکات. وانەی تایبەتی خۆی هەیە: ناردنی تۆڕگەڕ بۆ شوێنێکی تر.

The post office pigeonholesکونەکانی پۆستەخانە

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?سووپەرگلۆباڵ چییە؟

Definitionپێناسە

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 . $_POST works; $_post or $_Post is 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 هەیە.

Superglobal vs ordinary global variableسووپەرگلۆباڵ بەرامبەر گۆڕاوی گشتیی ئاساییgreet.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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();
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

An ordinary global variable must be imported into the function with global.

ئاسایی دەبێت بە global بهێنرێتە ناو .

Lineدێڕی 6

$_GET is 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() دەقی سەردانکەر بۆ چاپکردن سەلامەت دەکات. لە وانەی ئاسایشدا دەیخوێنیت.

Opened as greet.php (no name in the URL)کرایەوە وەک ⁦greet.php⁩ (بێ ناو لە URL)
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⁩ چاپ دەکات:

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$siteName = "CêIT";

function showSite() {
    echo $siteName;          // no "global" line
}

showSite();

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.

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

Quick checkپشکنینی خێراWhy can you use $_POST inside a function without writing global $_POST;?بۆچی دەتوانیت $_POST لەناو بەکاربهێنیت بێ ئەوەی global $_POST; بنووسیت؟

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 ڕێک نۆ هەیە. وێنەکە پیشانی دەدات زانیاریی هەریەکەیان لە کوێوە دێت: پێنجیان لە سەردانکەرەوە پڕ دەکرێنەوە، چواریان لەسەر ئامادە دەکرێن.

A map of the nine PHP superglobals. From the browser, inside the HTTP request: the URL query string goes into $_GET, the body of a POST request goes into $_POST, the cookies the browser sends back go into $_COOKIE, files uploaded with a POST form go into $_FILES, and $_REQUEST holds GET and POST together (and cookies, depending on request_order in php.ini). From the server: details of the request, the server and paths go into $_SERVER, environment variables go into $_ENV, session data stored on the server goes into $_SESSION, and your own global variables are in $GLOBALS. On the right: PHP fills these arrays before your first line runs; you can read them anywhere, even inside a function without global; they are all associative arrays, read by key, like $_POST["name"]; and they start fresh for every request.نەخشەیەک بۆ نۆ سووپەرگلۆباڵەکەی PHP. لە تۆڕگەڕەوە، لەناو داواکاریی HTTPـدا: زنجیرەی پرسی URL دەچێتە ناو $_GET، جەستەی داواکارییەکی POST دەچێتە ناو $_POST، ئەو کووکییانەی تۆڕگەڕەکە دەیاننێرێتەوە دەچنە ناو $_COOKIE، ئەو فایلانەی بە فۆرمێکی POST بار دەکرێن دەچنە ناو $_FILES و $_REQUEST GET و POST پێکەوە هەڵدەگرێت (و کووکییەکانیش، بەپێی request_order لە php.ini). لە ڕاژەکارەوە: وردەکاریی داواکاری، ڕاژەکار و ڕێڕەوەکان دەچنە ناو $_SERVER، گۆڕاوەکانی ژینگە دەچنە ناو $_ENV، زانیاریی سێشن کە لەسەر ڕاژەکار هەڵگیراوە دەچێتە ناو $_SESSION و گۆڕاوە گشتییەکانی خۆت لە $GLOBALSدان. لە لای ڕاست: PHP پێش ئەوەی یەکەم دێڕی کۆدەکەت کار بکات ئەم ڕیزەکانە پڕ دەکاتەوە؛ لە هەموو شوێنێک دەیانخوێنیتەوە، تەنانەت لەناو کردارێکدا بەبێ global؛ هەموویان ڕیزەکی پەیوەندیدارن و بە کلیل دەخوێنرێنەوە، وەک $_POST["name"]؛ و بۆ هەر داواکارییەک لە سەرەتاوە پڕ دەکرێنەوە.
Where each superglobal gets its dataهەر زانیارییەکانی لە کوێوە وەردەگرێتCêIT
All nine superglobalsهەر نۆ
SuperglobalWhat it holdsچی تێدایەWhere its keys come from لە کوێوە دێن
$GLOBALSAll global variables of the script (variables made outside any function).هەموو گشتییەکانی (ئەو لە دەرەوەی هەموو دروست کراون).The variable names, without $: $GLOBALS["x"] is $x.ناوی بەبێ $: $GLOBALS["x"] هەمان $xـە.
$_SERVERInformation 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.
$_GETValues 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 دروست دەکات.
$_POSTValues 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ـی .
$_REQUESTThe contents of $_GET and $_POST (and $_COOKIE, if php.ini allows it) together.ناوەڕۆکی $_GET و $_POST (و $_COOKIE، ئەگەر php.ini ڕێگە بدات) پێکەوە.The same keys as those arrays.هەمان ئەو .
$_COOKIEThe the browser sent with this request. Singular: not $_COOKIES.ئەو لەگەڵ ئەم ناردوونی. ناوەکەی تاکە، نەک $_COOKIES.The cookie names chosen when the cookie was created with setcookie().ناوی ، کە لە کاتی دروستکردنیان بە setcookie() هەڵبژێردران.
$_SESSIONSession 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";
$_FILESInformation 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).
$_ENVEnvironment 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 لە بارکردنی فایلدا ڕوون دەکرێتەوە.

Quick checkپشکنینی خێراA visitor uploads a photo with a form. Which superglobal tells your script the name and size of that file?سەردانکەرێک بە وێنەیەک بار دەکات. کام ناو و قەبارەی ئەو بە دەڵێت؟

$_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"ـی هەبێت، وەک دەخاتە کۆتایی ناونیشانەکەوە:

GET puts the data in the URLGET زانیارییەکان دەخاتە ناو URLpage.phpHTMLRuns in the browserلە ناو تۆڕگەڕ کار دەکات
<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 پڕ دەکاتەوە).

POST keeps the data out of the URLPOST زانیارییەکان لە URL دوور دەخاتەوەpage.phpHTMLRuns in the browserلە ناو تۆڕگەڕ کار دەکات
<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() هەموو چاپ دەکات:

page.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "<pre>";
echo '$_GET = ';     print_r($_GET);
echo '$_POST = ';    print_r($_POST);
echo '$_REQUEST = '; print_r($_REQUEST);
echo "</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 دەگرێتەوە.

Quick checkپشکنینی خێراWhich superglobal holds the contents of $_GET, $_POST and $_COOKIE together?کام ناوەڕۆکی $_GET و $_POST و $_COOKIE پێکەوە لەخۆ دەگرێت؟

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

Common $_SERVER keys باوەکانی $_SERVER
KeyMeaningواتاExample valueنموونەی نرخ
PHP_SELFThe path of the running script, from the web root. ئەو کار دەکات، لە ڕەگی ./test/info.php
REQUEST_METHODHow the page was requested: GET or POST.لاپەڕەکە چۆن داوا کراوە: GET یان POST.GET
QUERY_STRINGEverything after ? in the URL.هەموو شتێکی دوای ? لە URLـدا.city=Erbil
SERVER_NAMEThe name of the host.ناوی خانەخوێ (هۆست).localhost
REMOTE_ADDRThe IP address of the visitor. سەردانکەر.127.0.0.1
HTTP_USER_AGENTWhat the browser says about itself. The browser can lie.ئەوەی دەربارەی خۆی دەیڵێت. دەتوانێت درۆ بکات.Mozilla/5.0 (…) Firefox/131.0
info.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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"]);
Explain 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 ڕێگایەکی متمانەپێکراوە بۆ ئەوەی بپرسیت «ئایا ئێستا نێردرا؟»:

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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.";
}
After a form was sent with POSTدوای ناردنی فۆرمێک بە POST
The form was sent with POST.
Quick checkپشکنینی خێراWhich $_SERVER key tells you whether the page was requested with GET or with POST?کام $_SERVER پێت دەڵێت لاپەڕەکە بە GET داوا کراوە یان بە 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

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

Quick checkپشکنینی خێراWhere is the data of $_SESSION stored?زانیاریی $_SESSION لە کوێ هەڵدەگیرێت؟

$_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.

upload.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo $_FILES["fileUploaded"]["name"] . "<br>";
echo $_FILES["fileUploaded"]["size"] . " bytes";

Uploading 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:

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$x = 75;
$y = 25;

function addition() {
    $GLOBALS["z"] = $GLOBALS["x"] + $GLOBALS["y"];
}

addition();
echo $z;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

Reads $x and $y through $GLOBALS and 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 ئەگەر هیچ نەبووبێت):

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$file = @fopen("no-such-file.txt", "r");

if ($file === false) {
    $error = error_get_last();
    echo "Error: " . $error["message"];
}
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"]) is true only if the key exists (and is not null).
  • $_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" دەدات ئەگەر هەبێت، ئەگینا بنەڕەتییەکەی دوای ?? — بەبێ هیچ . ?? بە ئینگلیزی پێی دەوترێت .
page.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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.";
}
Quick checkپشکنینی خێراThe page is opened as page.php (nothing after ?). What does $_GET["city"] ?? "unknown" give?لاپەڕەکە وەک page.php دەکرێتەوە (هیچ شتێک دوای ? نییە). $_GET["city"] ?? "unknown" چی دەدات؟

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 کراوەتەوە. تەنها دێڕی یەکەم دروستە؛ هەریەک لە دێڕەکانی تر هەڵەیەکی باو دەکات.

page.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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 sent
  • 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. $_COOKIES and $_GLOBALS do not exist. The correct names are $_COOKIE and $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 $_GET is empty.
  • Printing user input as it is. Always escape it with htmlspecialchars().
  • Using $php_errormsg. It was removed in PHP 8; use error_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ڕاهێنان

Quick checkپشکنینی خێراA form has <input name="Color"> and method="post". Which line reads the chosen value? <input name="Color"> و method="post"ـی هەیە. کام دێڕ هەڵبژێردراوەکە دەخوێنێتەوە؟

Quick checkپشکنینی خێراWhich of these is a real superglobal name?کامیان ناوی ڕاستەقینەیە؟

Quick checkپشکنینی خێراIn PHP 8, how do you read the text of the last error message?لە ⁦PHP 8⁩دا چۆن دەقی دوایین دەخوێنیتەوە؟

ExerciseڕاهێنانGreet the visitor from the URLسڵاوکردن لە سەردانکەر لە ڕێگەی URL

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وەڵامەکە پیشان بدە
hello.phpPHP
<?php
function sayHello() {
    $name = $_GET["name"] ?? "guest";
    echo "Hello, " . htmlspecialchars($name) . "!";
}

sayHello();
Opened as hello.phpکرایەوە وەک ⁦hello.php⁩
Hello, guest!
ExerciseڕاهێنانFix the superglobal namesناوی سووپەرگلۆباڵەکان چاک بکەرەوە

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() چاپ دەکات. سێ بدۆزەوە و چاکیان بکەرەوە.

page.phpPHP
<?php
$siteName = "CêIT";

function info() {
    echo $_GLOBALS["siteName"] . "<br>";
    echo $_COOKIES["theme"] . "<br>";
    echo $_get["lang"] . "<br>";
}

info();
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 ??:

هەروەها سەردانکەر سەلامەت دەکەین و بە ?? دەدەین:

page.phpPHP
<?php
$siteName = "CêIT";

function info() {
    echo $GLOBALS["siteName"] . "<br>";
    echo htmlspecialchars($_COOKIE["theme"] ?? "") . "<br>";
    echo htmlspecialchars($_GET["lang"] ?? "") . "<br>";
}

info();
ExerciseڕاهێنانWhich superglobal?کام سووپەرگلۆباڵ؟

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's name is)
  • (c) $_FILES["photo"]["name"]
  • (d) $_SERVER["PHP_SELF"]
  • (e) $_SESSION["user"] (after session_start())
  • (f) $GLOBALS["total"]
  • (أ) $_GET["q"]
  • (ب) $_POST["password"] (هەر nameێک هەیبێت)
  • (ج) $_FILES["photo"]["name"]
  • (د) $_SERVER["PHP_SELF"]
  • (هـ) $_SESSION["user"] (دوای session_start())
  • (و) $GLOBALS["total"]
Review questionsپرسیارەکانی پێداچوونەوە
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 on php.ini).
  • $_SERVER describes the request and the server; $_COOKIE reads cookies; $_SESSION keeps data on the server; $_FILES describes uploads; $_ENV holds environment variables; $GLOBALS holds your global variables.
  • $php_errormsg is gone (PHP 8): use error_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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

5 | Built-In Superglobal Variables in PHP | 2023 | Learn PHP Full Course for BeginnersDani Krossing · Englishئینگلیزی · 145K viewsبینین · 3.2K likesلایک · Partبەشی 0:00–13:57 (ofلە 15:28)

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

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

Form Handling with GET and POSTمامەڵەکردن لەگەڵ فۆرم بە GET و POST

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

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