Chapterبەش 8 · Lectureوانە 8.5
Including Files: include and requireهاوردەکردنی فایل: include و require
Write a header, a menu or a footer once and use it on every page. Learn include, include_once, require and require_once, what happens when a file is missing, and how die() and exit stop a script.
بەشی سەرەوە، یان بەشی خوارەوەی لاپەڕە یەکجار بنووسە و لە هەموو لاپەڕەکاندا بەکاری بهێنە. include، include_once، require و require_once فێر دەبیت، دەزانیت کاتێک نەبێت چی ڕوودەدات و die() و exit چۆن ڕادەگرن.
What you will learnچی فێر دەبیت
- Explain what including a file does and why it saves work.
- Use include, include_once, require and require_once correctly.
- Predict what happens when an included file is missing: a warning or a fatal error.
- Build a site whose pages share one header and one footer.
- Stop a script with die() or exit, without showing secret error details.
- Avoid the common mistakes: wrong names and paths, double includes, and file names chosen by the visitor.
- ڕوونی بکەیتەوە چی دەکات و بۆچی کار کەم دەکاتەوە.
- include، include_once، require و require_once بە دروستی بەکاربهێنیت.
- پێشبینی بکەیت کاتێک هێنراو نییە چی ڕوودەدات: یان .
- دروست بکەیت کە لاپەڕەکانی یەک بەشی سەرەوە و یەک بەشی خوارەوەیان هاوبەش بێت.
- بە die() یان exit ڕابگریت، بەبێ پیشاندانی وردەکاریی نهێنیی .
- خۆت لە هەڵە باوەکان بپارێزیت: ناو و هەڵە، هێنانە ناوەوەی هەمان دوو جار و ناوی کە سەردانکەر هەڵیبژاردووە.
Why include files?بۆچی فایل دەهێنینە ناو یەکتر؟
Most websites repeat the same parts on every page. At the top there is a logo and a menu of links. At the bottom there is a copyright line. Imagine a site with 20 pages. If you copy the menu into all 20 files and later add one new link, you must change 20 files, and you will probably forget one.
زۆربەی هەمان بەش لە هەموو لاپەڕەیەکدا دووبارە دەکەنەوە. لە سەرەوە لۆگۆ و هەیە، لە خوارەوەش دێڕی . وای دابنێ ٢٠ لاپەڕەی هەیە. ئەگەر بۆ هەر ٢٠ کۆپی بکەیت و دواتر یەک نوێ زیاد بکەیت، دەبێت ٢٠ بگۆڕیت و بە ئەگەری زۆر یەکێکیان لەبیر دەکەیت.
PHP solves this with . The include (or require) statement takes all the text, code and markup that exist in another file and copies them into the file that uses the statement. You write a shared part once, keep it in its own file, and include it on every page that needs it. This is very useful when you want the same PHP, HTML or text on many pages of a website.
PHP ئەم کێشەیە بە چارەسەر دەکات. include (یان require) هەموو ئەو دەق و کۆد و نیشانەکردنانەی لە تردا هەن وەردەگرێت و کۆپییان دەکات بۆ ناو ئەو تێدا نووسراوە. بەم شێوەیە بەشێکی هاوبەش تەنها یەکجار دەنووسیت، لە جیادا هەڵیدەگریت و لە هەر لاپەڕەیەکدا کە پێویستی پێیەتی دەیهێنیتە ناوەوە. ئەمە زۆر بەسوودە کاتێک دەتەوێت هەمان PHP یان HTML یان دەق لە چەندین لاپەڕەی دابنێیت.
At a school office, every official letter starts with the same letterhead: the school's name, its logo and its address. Nobody draws the letterhead by hand for each letter. It is printed once on a template, and every letter uses that template. When the school moves to a new street, only the template changes, and from that day every letter shows the new address.
لە نووسینگەی قوتابخانەدا، هەموو نامەیەکی فەرمی بە هەمان سەرپەڕە دەست پێدەکات: ناوی قوتابخانە، لۆگۆکەی و ناونیشانەکەی. کەس بۆ هەر نامەیەک سەرپەڕەکە بە دەست ناکێشێتەوە. یەکجار لەسەر نموونەیەک چاپ کراوە و هەموو نامەکان ئەو نموونەیە بەکار دەهێنن. کاتێک قوتابخانەکە دەگوازرێتەوە بۆ شەقامێکی نوێ، تەنها نموونەکە دەگۆڕدرێت و لەو ڕۆژەوە هەموو نامەکان ناونیشانە نوێیەکە پیشان دەدەن.
Where the analogy stops workingسنووری ئەم نموونەیە
A printed letterhead never changes by itself. An included PHP file is read again on every request, and the PHP code inside it really runs. So it can show something different each time, for example the current year.
سەرپەڕەی چاپکراو خۆی هەرگیز ناگۆڕێت. بەڵام PHP کە دەهێنرێتە ناوەوە لە هەموو دووبارە دەخوێنرێتەوە و کۆدی PHPـی ناوی بە ڕاستی جێبەجێ دەکرێت. بۆیە دەتوانێت هەر جارەی شتێکی جیاواز پیشان بدات، بۆ نموونە ساڵی ئێستا.
In the picture, about.php holds only its own content. When a visitor asks for about.php, PHP runs it from top to bottom. At each require line, PHP reads the named file and puts its whole content at that place. The receives one complete HTML page. It never sees the separate files, and it cannot tell that the page was built from parts.
لە وێنەکەدا، about.php تەنها ناوەڕۆکی خۆی تێدایە. کاتێک سەردانکەرێک داوای about.php دەکات، PHP لە سەرەوە بۆ خوارەوە جێبەجێی دەکات. لە هەر دێڕێکی requireدا، PHP ئەو ناوی هاتووە دەخوێنێتەوە و هەموو ناوەڕۆکەکەی لەو شوێنەدا دادەنێت. یەک لاپەڕەی تەواوی HTML وەردەگرێت. هەرگیز جیاکان نابینێت و ناشزانێت کە لاپەڕەکە لە چەند بەشێک دروست کراوە.
The four statementsچوار ڕستەکە
PHP has four statements for including a file. Each one takes the name of the file (its ) as a :
PHP چوار هەیە بۆ . هەر یەکێکیان ناوی () وەک وەردەگرێت:
include 'filename';
include_once 'filename';
require 'filename';
require_once 'filename';| Statement | If the file is missingئەگەر نەبێت | If the file was already includedئەگەر پێشتر هێنرابێت |
|---|---|---|
include | Shows a ; the script continues. پیشان دەدات؛ بەردەوام دەبێت. | Includes it again.دووبارە دەیهێنێتەوە. |
include_once | Shows a ; the script continues. پیشان دەدات؛ بەردەوام دەبێت. | Skips it: only the first time counts.پشتگوێی دەخات: تەنها جاری یەکەم هەژمار دەکرێت. |
require | A ; the script stops.؛ دەوەستێت. | Includes it again.دووبارە دەیهێنێتەوە. |
require_once | A ; the script stops.؛ دەوەستێت. | Skips it: only the first time counts.پشتگوێی دەخات: تەنها جاری یەکەم هەژمار دەکرێت. |
In short: include and require do the same job and differ only when something goes wrong. The _once versions include a file only if it has not been included before.
بە کورتی: include و require هەمان کار دەکەن و تەنها کاتێک شتێک هەڵە بڕوات جیاوازن. وەشانەکانی _once تەنها ئەو کاتە دەهێننە ناوەوە کە پێشتر نەهێنرابێت.
The four statementsچوار ڕستەکە
Where PHP looks for the filePHP لە کوێ بە دوای فایلەکەدا دەگەڕێت
With a simple name such as 'footer.php', PHP looks in the folder of the page that the visitor opened. That is fine while all your files sit in one folder. When your parts live in another folder, build the path from __DIR__. This magic always holds the folder of the file in which it is written, so the path works no matter which page includes the file.
کاتێک ناوێکی سادە دەنووسیت وەک 'footer.php'، PHP لە ئەو لاپەڕەیەدا دەگەڕێت کە سەردانکەر کردوویەتیەوە. تا هەموو لە یەک بن ئەمە باشە. کاتێک بەشەکانت لە تردان، لە __DIR__ دروست بکە. ئەم تایبەتە هەمیشە ئەو تێدایە کە تێیدا نووسراوە، بۆیە کار دەکات هەر لاپەڕەیەک بهێنێتە ناوەوە.
<?php
echo __DIR__, "\n";
echo __DIR__ . '/footer.php', "\n";/var/www/html /var/www/html/footer.php
On Windows with XAMPP you would see something like C:xampphtdocssite.
لە ویندۆز و XAMPPدا شتێکی وەک C:xampphtdocssite دەبینیت.
<?php
require __DIR__ . '/parts/header.php'; // works from any page
?>When the file is missing: include vs requireکاتێک فایلەکە نییە: include بەرامبەر require
It is possible to insert the content of one PHP file into another PHP file with the include or require statement. The two statements are identical, except upon failure, for example when the file does not exist or cannot be read:
دەتوانرێت ناوەڕۆکی PHP بە include یان require بخرێتە ناو تری PHP. ئەم دوو ڕێک وەک یەکن، تەنها لە کاتی شکستدا جیاوازن، بۆ نموونە کاتێک بوونی نییە یان ناخوێنرێتەوە:
includeonly produces a (E_WARNING), and the script continues.requireproduces a and stops the script.
includeتەنها (E_WARNING) دروست دەکات و بەردەوام دەبێت.requireدروست دەکات و ڕادەگرێت.
<?php
include 'nonexistent.php';
echo "This line will still be executed.";Warning: include(nonexistent.php): Failed to open stream: No such file or directory in /var/www/html/include-missing.php on line 2 Warning: include(): Failed opening 'nonexistent.php' for inclusion (include_path='.:') in /var/www/html/include-missing.php on line 2 This line will still be executed.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Warning</b>: include(nonexistent.php): Failed to open stream: No such file or directory in <b>/var/www/html/include-missing.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>: include(): Failed opening 'nonexistent.php' for inclusion (include_path='.:') in <b>/var/www/html/include-missing.php</b> on line <b>2</b><br />
This line will still be executed.Two warnings, and then the echo still runs. In XAMPP the path and include_path look different (for example C:xampphtdocs...).
دوو و پاشان echo هەر جێبەجێ دەکرێت. لە XAMPPـدا و include_path جیاواز دەردەکەون (بۆ نموونە C:xampphtdocs...).
<?php
require 'nonexistent.php';
echo "This line will not be executed.";Warning: require(nonexistent.php): Failed to open stream: No such file or directory in /var/www/html/require-missing.php on line 2
Fatal error: Uncaught Error: Failed opening required 'nonexistent.php' (include_path='.:') in /var/www/html/require-missing.php:2 Stack trace: #0 {main} thrown in /var/www/html/require-missing.php on line 2HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Warning</b>: require(nonexistent.php): Failed to open stream: No such file or directory in <b>/var/www/html/require-missing.php</b> on line <b>2</b><br />
<br />
<b>Fatal error</b>: Uncaught Error: Failed opening required 'nonexistent.php' (include_path='.:') in /var/www/html/require-missing.php:2
Stack trace:
#0 {main}
thrown in <b>/var/www/html/require-missing.php</b> on line <b>2</b><br />A warning, then a fatal error. The line "This line will not be executed." never appears. On a live server, where error messages are hidden (display_errors = Off), the visitor sees an empty page and the server answers with status 500 (Internal Server Error). Older PHP versions (PHP 7) do not print the "Stack trace" lines.
، پاشان . دێڕی «This line will not be executed.» هەرگیز دەرناکەوێت. لەسەر ڕاستەقینە، کە تێیدا شاراونەتەوە (display_errors = Off)، سەردانکەر لاپەڕەیەکی بەتاڵ دەبینێت و بە 500 (Internal Server Error) وەڵام دەداتەوە. وەشانە کۆنەکانی PHP (PHP 7) دێڕەکانی «Stack trace» چاپ ناکەن.
Shows include and require side by side, and what error each one gives when the file cannot be found.
include و require بەراورد دەکات و کاتێک نەدۆزرێتەوە هەریەکەیان چ دەدات: یان .
Watch 0:44–4:27: include vs require and their error behaviour.
لە 0:44 تا 4:27 سەیر بکە: include بەراورد بە require و جۆری .
When the file is missing: include vs requireکاتێک فایلەکە نییە: include بەرامبەر require
Which one should you use?کامیان بەکاربهێنیت؟
Use require for files that the script cannot work without, such as configuration files, the database connection or a file of important functions. If such a file is missing, it is better to stop than to continue with unexpected behaviour. Use include for optional parts, such as an advertisement box or a "latest news" template, which can fail without breaking the whole page.
require بۆ ئەو بەکاربهێنە کە بەبێ ئەوان کار ناکات، وەک ، یان گرنگ. ئەگەر وا نەبێت، وەستان باشترە لە بەردەوامبوون بە ڕەفتارێکی چاوەڕواننەکراو. include بۆ بەشە ناپێویستەکان بەکاربهێنە، وەک سندوقێکی ڕیکلام یان «دوایین هەواڵەکان»، کە دەتوانن شکست بهێنن بەبێ ئەوەی هەموو لاپەڕەکە تێکبدەن.
<?php
require 'functions.php'; // essential: the page cannot work without it
include 'latest-news.php'; // optional: the page still works without itinclude_once and require_once: never twiceinclude_once و require_once: هەرگیز دوو جار
Including the same file twice runs it twice. For a piece of HTML you would get two copies on the page. For a file that defines functions it is worse: PHP does not allow two functions with the same name, so the script stops.
ئەگەر هەمان دوو جار بهێنیتە ناوەوە، دوو جار جێبەجێ دەکرێت. بۆ پارچەیەک HTML دوو کۆپی لە لاپەڕەکەدا دەبینیت. بۆ کە دیاری دەکات خراپترە: PHP ڕێگە نادات دوو هەمان ناویان هەبێت، بۆیە دەوەستێت.
<?php
function greet($name) {
return "Hello, $name!";
}<?php
include 'functions.php';
include 'functions.php';
echo greet("Shilan");Fatal error: Cannot redeclare function greet() (previously declared in /var/www/html/functions.php:2) in /var/www/html/functions.php on line 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Fatal error</b>: Cannot redeclare function greet() (previously declared in /var/www/html/functions.php:2) in <b>/var/www/html/functions.php</b> on line <b>2</b><br />With require_once (or include_once), PHP remembers which files it has already included and skips them the second time:
بە require_once (یان include_once)، PHP لەبیری دەمێنێت کام پێشتر هێنراوەتە ناوەوە و جاری دووەم پشتگوێیان دەخات:
<?php
require_once 'functions.php';
require_once 'functions.php'; // skipped: already included
echo greet("Shilan");Hello, Shilan!
<?php
echo "counter.php ran<br>\n";<?php
include_once 'counter.php';
include_once 'counter.php';
var_dump(include_once 'counter.php');
include 'counter.php';counter.php ran bool(true) counter.php ran
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
counter.php ran<br>
bool(true)
counter.php ran<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
The first
include_onceruns the file; the second one skips it.یەکەم
include_onceجێبەجێ دەکات؛ دووەمیان پشتگوێی دەخات.- Lineدێڕی 4
A skipped
include_oncereturnstrue.include_onceی پشتگوێخراوtrueدەگەڕێنێتەوە.- Lineدێڕی 5
A plain
includedoes not check, so the file runs again.includeی ئاسایی ناپشکنێت، بۆیە دووبارە جێبەجێ دەکرێت.
A real use: one database connection fileبەکارهێنانێکی ڕاستەقینە: یەک فایلی پەیوەندیی بنکەدراوە
In the database chapter you will write the connection code once, in a file such as dbcon.php, and include it at the top of every page that needs the . One line at the top of a page, and the connection $con is ready to use.
لە بەشی کۆدی پەیوەندییەکە یەکجار دەنووسیت، لە وەک dbcon.php و لە سەرەوەی هەموو ئەو لاپەڕانەی پێویستیان بە هەیە دەیهێنیتە ناوەوە. تەنها یەک دێڕ لە سەرەوەی لاپەڕەکە و پەیوەندییەکە، $con، ئامادەی بەکارهێنانە.
<?php
// dbcon.php - connect to MySQL once; every page that needs the database includes this file
try {
$con = mysqli_connect("localhost", "root", "", "mydb");
} catch (mysqli_sql_exception $e) {
error_log($e->getMessage()); // the real reason goes to the server's error log
die("Sorry, the database is not available right now.");
}Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
Try to connect. Since PHP 8.1 a failed connection throws an exception.
هەوڵی پەیوەندیکردن بدە. لە PHP 8.1ـەوە پەیوەندیی شکستخواردوو فڕێ دەدات.
- Lineدێڕی 6-7
Write the real reason to the server's log, show visitors only a short message, and stop.
هۆکاری ڕاستەقینە لە لۆگی بنووسە، تەنها پەیامێکی کورت بە سەردانکەران پیشان بدە و بوەستە.
<?php
require_once 'dbcon.php'; // after this line, $con is ready to use
$result = mysqli_query($con, "SELECT title FROM books");
while ($row = mysqli_fetch_assoc($result)) {
echo htmlspecialchars($row["title"]) . "<br>";
}These two files need a running MySQL server and a books table, so their output is not shown here. You will build and test them in Connecting PHP to MySQL. The die() in dbcon.php is explained below.
ئەم دوو پێویستیان بە MySQLـی کارا و بە ناوی books هەیە، بۆیە لێرەدا پیشان نادرێت. لە پەیوەستکردنی PHP بە MySQLدا دروستیان دەکەیت و تاقییان دەکەیتەوە. die()ی ناو dbcon.php لە خوارەوە ڕوون کراوەتەوە.
Stopping a script: die() and exitڕاگرتنی دەسنووس: die() و exit
Sometimes a script meets an error that it cannot fix (cannot "handle"), for example a missing settings file. Continuing would only cause more errors or damage. Then the script should stop at once. The die() does exactly this: it ends the script and can print a message to the browser (or to the command line).
هەندێک جار تووشی دەبێت کە ناتوانێت چارەسەری بکات (ناتوانێت «Handle»ی بکات)، بۆ نموونە ونبوو. بەردەوامبوون تەنها و زیانی زیاتر دروست دەکات. لەو کاتەدا دەبێت دەستبەجێ بوەستێت. die() ڕێک ئەمە دەکات: کۆتایی پێدەهێنێت و دەتوانێت پەیامێک بۆ (یان بۆ هێڵی فەرمان) چاپ بکات.
<?php
// stop the script and show a message
die("An error has occurred.");
// this code will not be executed
echo "This code will not be executed.";
?>An error has occurred.
What you should know about die():
ئەوەی دەبێت دەربارەی die() بیزانیت:
- The script stops at once. No code after
die()runs. - Output that was already produced before
die()is still sent to the browser. - The message is optional. It does not have to be an error message: any string is printed.
die()is the same asexit(). You can also writeexit;ordie;without brackets.- If you pass an instead of a string, nothing is printed. The number becomes the script's exit status, which matters when PHP runs from the command line.
- دەستبەجێ دەوەستێت. هیچ کۆدێک دوای
die()جێبەجێ ناکرێت. - ئەو پێش
die()دروست بووە هەر بۆ دەنێردرێت. - پەیامەکە ئارەزوومەندانەیە. پێویست ناکات بێت: هەر بێت چاپ دەکرێت.
die()هەمانexit()ـە. دەتوانیتexit;یانdie;یش بەبێ کەوانە بنووسیت.- ئەگەر لە جیاتی بنێریت، هیچ شتێک چاپ ناکرێت. ژمارەکە دەبێتە دۆخی دەرچوونی ، کە کاتێک PHP لە هێڵی فەرمانەوە کار دەکات گرنگە.
Stopping a script: die() and exitڕاگرتنی دەسنووس: die() و exit
exit, and die as a functionexit و die وەک کردار
exit does the same job as die(). Here it stops the page after the first line, so the second echo never runs:
exit هەمان کاری die() دەکات. لێرەدا لاپەڕەکە دوای یەکەم دێڕ ڕادەگرێت، بۆیە echoی دووەم هەرگیز جێبەجێ ناکرێت:
<?php
echo "Start<br>";
exit;
echo "Never shown";Start
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Start<br><?php
echo "Checking...";
exit(3);Checking...
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The 3 is not printed; it is only the exit status.
ژمارە 3 چاپ ناکرێت؛ تەنها دۆخی دەرچوونە.
Stopping a script: die() and exitڕاگرتنی دەسنووس: die() و exit
die() in practicedie() لە کرداردا
A practical use: stop with a polite message when a file that the page needs is missing.
نموونەیەکی ڕاستەقینە: کاتێک کە لاپەڕەکە پێویستی پێیەتی نییە، بە پەیامێکی ڕێزدارانە بوەستە.
<?php
$settingsFile = 'settings.php';
if (!file_exists($settingsFile)) {
die("Sorry, the site is not available right now.");
}
require $settingsFile;
echo "Welcome!";Sorry, the site is not available right now.
settings.php does not exist in this test, so the script stops at line 4.
لەم تاقیکردنەوەیەدا settings.php بوونی نییە، بۆیە لە دێڕی 4دا دەوەستێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
file_exists()returnstrueif the file is there.file_exists()ئەگەر هەبێتtrueدەگەڕێنێتەوە.- Lineدێڕی 4
Stop with a short, friendly message.
بە پەیامێکی کورت و دۆستانە بوەستە.
You will meet exit again right after header() in Redirects, where it stops the rest of the page from running after the visitor is sent away.
دووبارە exit دەبینیتەوە ڕاستەوخۆ دوای header() لە ئاڕاستەکردنەوە، کە لەوێ ڕێگە دەگرێت لە جێبەجێبوونی باقی لاپەڕەکە دوای ئەوەی سەردانکەر نێردرا بۆ شوێنێکی تر.
Never include a file that the visitor choosesهەرگیز فایلێک مەهێنە کە سەردانکەر هەڵیبژاردووە
Some sites have one page that shows different parts, for example show.php?page=contact. It is tempting to write include $_GET['page'] . '.php';. Never do this. The visitor controls the , so they can change the value to reach other files on the server, for example with ../ to climb out of the folder.
هەندێک یەک لاپەڕەیان هەیە کە بەشی جیاواز پیشان دەدات، بۆ نموونە show.php?page=contact. ئارەزوو دەکەیت بنووسیت include $_GET['page'] . '.php';. هەرگیز ئەمە مەکە. سەردانکەر کۆنترۆڵ دەکات، بۆیە دەتوانێت بگۆڕێت بۆ ئەوەی بگاتە تری سەر ، بۆ نموونە بە ../ بۆ دەرچوون لە .
Instead, keep an : a fixed list of the only names you accept. Anything else gets a "not found" message.
لە جیاتی ئەوە، هەڵبگرە: لیستێکی جێگیر لەو ناوانەی تەنها ئەوانە قبووڵ دەکەیت. هەر شتێکی تر پەیامی «نەدۆزرایەوە» وەردەگرێت.
<?php
$allowed = ['home', 'contact']; // the only pages we allow
$page = $_GET['page'] ?? 'home';
if (in_array($page, $allowed, true)) {
include __DIR__ . '/pages/' . $page . '.php';
} else {
echo "<p>Page not found.</p>";
}Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The allow-list: only these two names are accepted.
: تەنها ئەم دوو ناوە قبووڵ دەکرێن.
- Lineدێڕی 3
??gives'home'when there is nopagein the URL.??کاتێکpageلە URLـەکەدا نەبێت'home'دەدات.- Lineدێڕی 5
in_array(..., true)checks that the name is exactly one of the allowed names.in_array(..., true)دەپشکنێت کە ناوەکە ڕێک یەکێک لە ناوە ڕێگەپێدراوەکانە.
<p>Welcome to the home page.</p><p>Call us: 0750 000 0000</p>| Request | Resultئەنجام |
|---|---|
show.php?page=contact | <p>Call us: 0750 000 0000</p> |
show.php?page=../../../etc/passwd | <p>Page not found.</p> |
show.php | <p>Welcome to the home page.</p> |
Common mistakesهەڵە باوەکان
- Wrong file name. The file is saved as
Footer.phporindex.php, but the code includesfooter.php. You get two warnings and no footer. Check the name and the folder. - Wrong folder. The part is in another folder. Build the path with
__DIR__ . '/parts/footer.php'. - A functions file included twice. You get "Cannot redeclare function". Use
require_once. includefor an essential file. The page goes on without its settings and shows strange errors further down. Userequire(orrequire_once).- Text printed by an included file, even one blank line after
?>, comes before your HTML and can breakheader()redirects. In files that contain only PHP, leave out the final?>. - Including a file chosen by the visitor. Use an allow-list, as shown above.
- Expecting
includeto work in a.htmlfile. PHP code runs only in files that the server sends to PHP, normally.phpfiles.
- ناوی هەڵەی . بە ناوی
Footer.phpیانindex.phpخەزن کراوە، بەڵام کۆدەکەfooter.phpدەهێنێتە ناوەوە. دوو وەردەگریت و هیچ بەشی خوارەوەیەک نییە. ناو و بپشکنە. - هەڵە. بەشەکە لە تردایە. بە
__DIR__ . '/parts/footer.php'دروست بکە. - دوو جار هێنراوە. «Cannot redeclare function» وەردەگریت.
require_onceبەکاربهێنە. includeبۆ پێویست. لاپەڕەکە بەبێ بەردەوام دەبێت و لە خوارەوەتر سەیر پیشان دەدات.require(یانrequire_once) بەکاربهێنە.- دەقێک کە هێنراو چاپی دەکات، تەنانەت یەک دێڕی بەتاڵ دوای
?>، پێش HTMLـەکەت دێت و دەتوانێتheader()تێکبدات. لەو تەنها PHPیان تێدایە،?>ی کۆتایی مەنووسە. - هێنانی کە سەردانکەر هەڵیبژاردووە. وەک لە سەرەوە پیشان درا، بەکاربهێنە.
- چاوەڕوانیی ئەوەی
includeلە.htmlدا کار بکات. کۆدی PHP تەنها لەو کار دەکات کە دەیاننێرێت بۆ PHP، کە بە شێوەی ئاسایی.phpن.
Exercises and lab tasksڕاهێنان و ئەرکەکانی تاقیگە
Open a new PHP project in NetBeans and name it file_include. (1) Create a file footer.php that prints a copyright line with the current year. (2) Create a second page, second.php, with a heading and two paragraphs, and include footer.php at the bottom. (3) Run second.php and look at the result and at the page source.
پرۆژەیەکی نوێی PHP لە NetBeans بکەرەوە و ناوی بنێ file_include. (١) بە ناوی footer.php دروست بکە کە دێڕێکی لەگەڵ ساڵی ئێستا چاپ بکات. (٢) لاپەڕەیەکی دووەم، second.php، بە سەردێڕێک و دوو بڕگەوە دروست بکە و footer.php لە خوارەوەی بهێنە ناوەوە. (٣) second.php جێبەجێ بکە و سەیری ئەنجامەکە و لاپەڕەکە بکە.
Show answerوەڵامەکە پیشان بدە
<?php echo "<p>Copyright © 2023-" . date("Y") . " example.com</p>"; ?><html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>Welcome to my home page! Some text. Some more text. Copyright © 2023-2026 example.com
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<p>Copyright © 2023-2026 example.com</p> </body>
</html>Make header.php and footer.php like the bookshop example, and three pages: index.php, about.php and contact.php. Each page sets $pageTitle before it requires the header. Add the contact page to the menu by changing only one file.
header.php و footer.php وەک نموونەی کتێبفرۆشییەکە دروست بکە و سێ لاپەڕە: index.php، about.php و contact.php. هەر لاپەڕەیەک پێش ئەوەی بەشی سەرەوە بهێنێتە ناوەوە، $pageTitle دیاری دەکات. لاپەڕەی پەیوەندی بە گۆڕینی تەنها یەک بخە ناو .
Hintئاماژە
Which file contains the menu?
کام تێدایە؟
Show answerوەڵامەکە پیشان بدە
Add one line to the <nav> in header.php; all three pages show the new link.
یەک دێڕ بۆ <nav>ی ناو header.php زیاد بکە؛ هەر سێ لاپەڕەکە پیشان دەدەن.
<nav>
<a href="index.php">Home</a> |
<a href="about.php">About us</a> |
<a href="contact.php">Contact</a>
</nav><?php
$pageTitle = "Contact";
require 'header.php';
?>
<p>Phone: 0750 000 0000</p>
<?php require 'footer.php'; ?>What is the difference between include and require? Predict the output of both scripts below, then run them.
جیاوازیی نێوان include و require چییە؟ پێشبینیی هەردوو خوارەوە بکە، پاشان جێبەجێیان بکە.
<?php
include 'nonexistent.php';
echo "This line will still be executed.";<?php
require 'nonexistent.php';
echo "This line will not be executed.";Show answerوەڵامەکە پیشان بدە
Both bring another file into the script and differ only when the file cannot be found or read. The first script shows two warnings and then prints "This line will still be executed.". The second shows a warning and a fatal error (an uncaught Error in PHP 8), and the echo never runs.
هەردووکیان تر دەهێننە ناو و تەنها کاتێک جیاوازن کە نەدۆزرێتەوە یان نەخوێنرێتەوە. یەکەم دوو پیشان دەدات و پاشان «This line will still be executed.» چاپ دەکات. دووەمیان و پیشان دەدات (Errorێکی نەگیراو لە PHP 8دا) و echo ـەکە هەرگیز جێبەجێ ناکرێت.
twice.php stops with "Cannot redeclare function greet()". Change one word in each include line so that it works.
twice.php بە «Cannot redeclare function greet()» دەوەستێت. لە هەر دێڕێکی هێنانەکەدا یەک وشە بگۆڕە بۆ ئەوەی کار بکات.
<?php
include 'functions.php';
include 'functions.php';
echo greet("Shilan");Show answerوەڵامەکە پیشان بدە
<?php
require_once 'functions.php';
require_once 'functions.php'; // skipped: already included
echo greet("Shilan");Hello, Shilan!
Explain in your own words what die() does. Then write a script that stops with the message "Sorry, the site is not available right now." when the file settings.php does not exist, and otherwise requires it and prints "Welcome!".
بە زمانی خۆت ڕوونی بکەرەوە die() چی دەکات. پاشان بنووسە کە ئەگەر settings.php بوونی نەبوو بە پەیامی «Sorry, the site is not available right now.» بوەستێت، دەنا بیهێنێتە ناوەوە و «Welcome!» چاپ بکات.
Show answerوەڵامەکە پیشان بدە
die() (the same as exit) stops the script at once and can print a message; nothing after it runs. It is used when an error cannot be handled and going on would only cause more problems.
die() (هەمان exit) دەستبەجێ ڕادەگرێت و دەتوانێت پەیامێک چاپ بکات؛ هیچ شتێک دوای ئەو جێبەجێ ناکرێت. کاتێک بەکار دێت کە چارە ناکرێت و بەردەوامبوون تەنها کێشەی زیاتر دروست دەکات.
<?php
$settingsFile = 'settings.php';
if (!file_exists($settingsFile)) {
die("Sorry, the site is not available right now.");
}
require $settingsFile;
echo "Welcome!";Sorry, the site is not available right now.
Summary and reviewپوختە و پێداچوونەوە
include/requirecopy another file's content into the current file at that line; the code in it really runs.- Write shared parts (header, footer, menu, settings, connection) once, and include them everywhere.
- Missing file:
includegives a warning and continues;requirestops with a fatal error (an uncaughtErrorin PHP 8). include_once/require_oncenever include the same file twice.die()=exit(): stop the script now, optionally with a message. Functions since PHP 8.4.- Never include a file name that comes from the visitor without an allow-list.
include/requireناوەڕۆکی تر لەو دێڕەدا کۆپی دەکەن بۆ ناو ئێستا؛ کۆدی ناوی بە ڕاستی جێبەجێ دەکرێت.- بەشە هاوبەشەکان (سەرەوە، خوارەوە، ، ، پەیوەندی) یەکجار بنووسە و لە هەموو شوێنێک بیانهێنە ناوەوە.
- نەبوو:
includeدەدات و بەردەوام دەبێت؛requireبە دەوەستێت (Errorێکی نەگیراو لە PHP 8دا). include_once/require_onceهەرگیز هەمان دوو جار ناهێننە ناوەوە.die()=exit(): ئێستا ڕابگرە، ئەگەر ویستت بە پەیامێکەوە. لە PHP 8.4ـەوە .- هەرگیز ناوی کە لە سەردانکەرەوە دێت بەبێ مەهێنە ناوەوە.
What does the include (or require) statement do?ڕستەی include (یان require) چی دەکات؟
It takes all the text, code and markup in the named file and copies it into the file that uses the statement, at that line, while the script runs.
هەموو دەق و کۆد و نیشانەکردنی ناو ناوبراوەکە وەردەگرێت و لەو دێڕەدا، لە کاتی کارکردنی ، کۆپیی دەکات بۆ ناو ئەو تێدایە.
Why is including files useful?بۆچی هاوردەکردنی فایل بەسوودە؟
You can put the same PHP, HTML or text on many pages but keep it in one file, so a change is made only once.
دەتوانیت هەمان PHP یان HTML یان دەق لە چەندین لاپەڕەدا دابنێیت بەڵام لە یەک بیهێڵیتەوە، بۆیە گۆڕانکاری تەنها یەکجار دەکرێت.
What is the difference between include and require?جیاوازیی نێوان include و require چییە؟
They are identical except upon failure: include produces a warning (E_WARNING) and the script continues; require produces a fatal error and the script stops.
ڕێک وەک یەکن جگە لە کاتی شکست: include (E_WARNING) دروست دەکات و بەردەوام دەبێت؛ require دروست دەکات و دەوەستێت.
When should you use require instead of include?کەی require لە جیاتی include بەکاردەهێنیت؟
For essential files such as configuration files, the database connection or libraries of functions; use include for optional parts such as templates.
بۆ پێویستەکان وەک ، یان ؛ include بۆ بەشە ناپێویستەکانی وەک بەکاربهێنە.
What do include_once and require_once add?include_once و require_once چی زیاد دەکەن؟
They include the file only if it has not been included before, which prevents errors like "Cannot redeclare function".
تەنها ئەو کاتە دەهێننە ناوەوە کە پێشتر نەهێنرابێت، ئەمەش ڕێگە لە وەک «Cannot redeclare function» دەگرێت.
What is the DIE (die) function?کرداری DIE (die) چییە؟
It stops the script immediately when an error cannot be handled, optionally printing a message. It is the same as exit(). Since PHP 8.4 both are functions.
کاتێک چارە ناکرێت دەستبەجێ ڕادەگرێت و ئەگەر ویستت پەیامێک چاپ دەکات. هەمان exit()ـە. لە PHP 8.4ـەوە هەردووکیان .
What does include "dbcon.php"; do?include "dbcon.php"; چی دەکات؟
It brings the connection code into this page, so $con is ready.
کۆدی پەیوەندییەکە دەهێنێتە ناو ئەم لاپەڕەیە، بۆیە $con ئامادەیە.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
Shows include, require, include_once and require_once, how PHP finds the file, and how to reuse HTML parts on many pages.
include، require، include_once و require_once نیشان دەدات، چۆن دەدۆزێتەوە و چۆن پارچەکانی لە زۆر دووبارە بەکاربهێنیتەوە.
Whole video: all four include statements, PHP 8.
هەموو ڤیدیۆکە: هەر چوار جۆرەکەی ، بە 8.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Redirects: Sending the Browser Somewhere Elseئاڕاستەکردنەوە: ناردنی تۆڕگەڕ بۆ شوێنێکی تر
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 ن.