PHP

PHP is a general-purpose scripting language especially suited to web development. Its syntax is similar to Java and Perl and was originally created by Canadian programmer Rasmus Lerdorf in 1993 as a set of Common Gateway Interface (CGI) binaries written in C for tracking visits to his online resume. Over time, PHP evolved into a powerful server-side scripting language that now powers millions of websites, such as soyjak.party and even this very wiki. Even though it is sometimes seen as “old” or “messy” compared to newer frameworks and languages, PHP still sees heavy use so don’t believe what the angry Chud programmers on Twitter or Reddit say. In fact, PHP still gets plenty of feature updates making it still just as good with other competitive server-side languages. Its ease of deployment, gigantic ecosystem of frameworks and libraries, and the sheer number of legacy PHP sites still online ensure that PHP will be around for a long time to come.
Notable features
[edit | edit source]
PHP is best known for:
- PHP code runs on the server and generates HTML to be sent to the client’s browser. This makes it ideal for building websites, content management systems, and web applications.
- PHP has an massive library of frameworks, plugins, and tools thanks to it's wide adoption and ecosystem from platforms such as ACK!press.
- PHP can be mixed directly with HTML code, making it beginner-friendly and great for creating templates.
- PHP works seamlessly with SQL systems, such as MySQL, PostgreSQL, SQLite, and other databases to create dynamic, data-driven websites.
- PHP runs on Windows, Linux, macOS, and most web servers, making it highly versatile.
- By using the
declare(strict_types=1)directive, PHP can enforce strict types and type information. - Having weird-ass syntax. You're telling me that
function nigger()andarray()are not the same? One is a function while the other is a type of data? <-- meds this makes perfect sense - Being a massive piece of shit and inferior in every way to Python and Ruby.
How does PHP work?
[edit | edit source]At the heart of PHP is the Zend Engine, a open-source compiler and runtime environment for the PHP scripting language. Whenever a PHP file is requested, the following happens behind the scenes:
- The PHP interpreter lexes the PHP code, then converts it into an internal representation called an abstract syntax tree.
- The Zend Engine then compiles the syntax tree into low-level opcodes, which are a simplified set of instructions optimized for execution.
- The Zend Engine executes the opcodes line by line, handling function calls, database queries, file I/O, and any other server-side logic.
- The result of the script (usually HTML, or another format) is sent back to the web server, which delivers it to the client’s browser.
Scripts
[edit | edit source]CSS switcher
[edit | edit source]- css.php
Include this in the <head> of your php script.
<?php
if (!isset($_COOKIE['theme'])) {
setcookie("theme", "light", time() + (86400 * 399), "/");
$theme = "light";
} else {
$theme = $_COOKIE['theme'];
}
if ($theme == "light") {
echo("<link href='/css/base.css' rel='stylesheet'>");
} else if ($theme == "dark") {
echo("<link href='/css/base.css' rel='stylesheet'>");
echo("<link href='/css/dark.css' rel='stylesheet'>");
}
- theme.php
This is where the user chooses their theme.
<?php
function setTheme($theme) {
setcookie("theme", $theme, time() + (86400 * 399), "/");
header("Location: index.php");
exit;
}
$themearray = ['light', 'dark'];
if (isset($_GET['a']) && in_array($_GET['a'], $themearray)) {
setTheme($_GET['a']);
} else {
echo "Available themes: ";
foreach($themearray as $theme) {
echo "<a href=\"?a=$theme\">$theme</a> ";
}
die();
}
Textboard
[edit | edit source]This is a simple way of how an imageboard works.
- You go to the server, and fetch "index.html".
- You enter your shit thread / reppey in the post form, and send it off; usually to
imgboard.php - The server submits your thread into the database and regenerates index.html with the new thread in it
The reason that the post index doesn't generate on the fly (e.g. with index.php) is because:
- That would use more processing power than needed
- It's an anonymous imageboard, there are no user-specific algorithms.
The code itself
[edit | edit source]This is a textboard because I can't be asked to make an imageboard in php. You can change it all you like, freely, if you want. It's based on Gazou BBS.
<?php
// Original script by North, made it actually work doe
// These are constants: variables that don't change
define("PHP_SELF", "imgboard.php"); // This file
define("PHP_SELF2", "index.html"); // The index file
define("LOG", "imglog.log"); // The post log file
define("TITLE", "Textboard"); // Your board title
// The log should look like:
// 1|01-01-2025|Subject|Name|Body
// 2|02-01-2025|Another post|North|sdfasdfasgdfafgs
// Read all posts into an array
function readlog() {
$readlines = file(LOG) ?: [];
$posts = [];
foreach ($readlines as $line) {
$line = trim($line);
if ($line === '') continue; // Skip empty lines
if (substr_count($line, '|') < 4) continue; // Skip malformed lines
$posts[] = explode("|", $line); // Split the line into its components and add to posts array
}
return array_reverse(array_values($posts)); // Reverse the order so the latest posts come first
}
// Flash a message to the user and redirect to the index page
function flashmsg($msg) {
$dat = '<meta http-equiv="refresh" content="1;url='.PHP_SELF2.'">'; // Redirects the user to the index page after 1 second
$dat .= '<p>'.$msg.'</p>'; // Display our flash message
die($dat);
}
// Show an error message
function error($why) {
$dat = "<font color=#FF0000>Error: $why</font>";
$dat .= '<p><a href="'.PHP_SELF2.'">Return</a></p>';
die($dat);
}
// Validate a form field and html-escape its content to prevent XSS attacks
function fieldProc($fieldName,$txt,$lenMax,$allowBrk) {
$txt = trim($txt);
if (!$allowBrk) $txt = str_replace(array("\r\n", "\r", "\n"), '', $txt);
if (strlen($txt) > $lenMax) error($fieldName." is too long!");
$txt = nl2br(htmlspecialchars($txt, ENT_QUOTES, 'UTF-8')); // Escape HTML to prevent XSS attacks
$txt = str_replace('|', '|', $txt); // Encode pipe character to prevent malformed log entries
return $txt;
}
// Register a new post
function regist() {
$name = fieldProc("Name", $_POST['name'] ?? '', 20, false);
$sub = fieldProc("Subject", $_POST['sub'] ?? '', 100, false);
$com = fieldProc("Comment", $_POST['com'] ?? '', 200, true);
if ($name==='') $name = 'Anonymous'; // Default to 'Anonymous' if no name is provided
if ($com==='') error("No comment entered.");
$date = gmdate("Y/m/d(D) H:i",time()); // Format the post date
$fp = fopen(LOG, 'a');
if (!$fp) error("Failed to read the log.");
if (!is_writable(LOG)) error("Log is not writable.");
if (!flock($fp, LOCK_EX)) error("Failed to lock the log file."); // Lock the log file so two posts cannot be written simultaneously
$log = readlog();
// Compute the next post number
$postno = 0;
for ($i = 0; $i < count($log); $i++) {
$no = $log[$i][0]; // Get the post number from the log entry
if ($no > $postno) $postno = $no;
}
$postno++;
// Write the post to disk
$entry = "$postno|$date|$sub|$name|$com\n";
fwrite($fp, $entry); // Write the post to disk
flock($fp, LOCK_UN); // Release the lock on the log file
fclose($fp); // Close the log file
}
// Rebuild the index page
function rebuild() {
$dat="<html><head><title>".TITLE."</title></head>
<body bgcolor=#ffffee text=#800000>
<p align=right>
[<a href='".PHP_SELF."'>Rebuild Index</a>]
</p>
<p align=center>
<font color=#800000 face='MS PGothic' size=5>
<b><SPAN>".TITLE."</SPAN></b><br></font></p><hr>";
$dat.='
<center>
<form action="'.PHP_SELF.'" method="POST" enctype="multipart/form-data">
<input type=hidden name=mode value="regist">
<table cellpadding=1 cellspacing=1>
<tr>
<td bgcolor=#eeaa88><b>Name</b></td>
<td><input type=text name=name size="28" value=""><input type=submit value="Submit"><input type=reset value="Reset"></td>
</tr>
<tr>
<td bgcolor=#eeaa88><b>Subject</b></td>
<td><input type=text name=sub size="48" value=""></td>
</tr>
<tr>
<td bgcolor=#eeaa88><b>Comment</b></td>
<td><textarea name=com cols="48" rows="4" wrap=soft></textarea>
</td>
</tr>
</table></form></center>
<hr>';
$log = readlog();
foreach ($log as $post) {
$dat.='<small>No.'.$post[0].'</small> <big><font color=#FF0000>'.$post[2].'</font></big><br>
<small>Name <font color=#117743>'.$post[3].'</font> Date '.$post[1].'</small><blockquote>';
$dat.=$post[4].'</blockquote><hr>';
}
$dat.='<p align=center><small>Based on <a href="http://php.loglog.jp/">GazouBBS</a>, made by <a href="https://soyjakwiki.org/User:North">North</a></small></p></body></html>';
file_put_contents(PHP_SELF2, $dat, LOCK_EX); // Write the index html to disk
}
// Handle the mode
switch ($_POST['mode'] ?? '') {
case 'regist':
regist(); // Register the new post
rebuild(); // Build the index so the users post shows up immediately
flashmsg("Post successful!");
break;
default:
rebuild(); // Rebuild the index
flashmsg("Regenerating index...");
break;
}





