"Jinsi ya Kuhariri Tangazo la Tovuti kwa PHP Bila Kuingia Kwenye cPanel (Hatua kwa Hatua)"
๐ Hii ndiyo sehemu ya blog ambayo itakuwa na form ya kuhariri na kuhifadhi tangazo.
๐ Code Kamili:
php
Copy
Edit
<?php
// Nenosiri la kuingia
$password = "TANGAZO"; // Badilisha nenosiri lako hapa
// Form ya kuingiza nenosiri
if (!isset($_POST['password']) && !isset($_POST['new_message'])) {
echo '<form method="post"><h3>๐ Ingiza Nenosiri:</h3><input type="password" name="password" required /><br><br><button type="submit">Ingia</button></form>';
exit;
}
// Cheki kama nenosiri ni sahihi
if (isset($_POST['password']) && $_POST['password'] !== $password) {
die("โ Nenosiri si sahihi.");
}
// Kuhifadhi tangazo jipya
if (isset($_POST['new_message'])) {
file_put_contents("tangazo.txt", trim($_POST['new_message']));
$success = "โ Tangazo limehifadhiwa kikamilifu!";
}
// Soma tangazo la sasa
$current = file_exists("tangazo.txt") ? file_get_contents("tangazo.txt") : "";
?>
<!DOCTYPE html>
<html lang="sw">
<head>
<meta charset="UTF-8">
<title>Hariri Tangazo</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background-color: #f1f1f1; }
form { background: white; padding: 20px; border-radius: 10px; max-width: 600px; margin: auto; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
textarea { width: 100%; height: 150px; font-size: 1rem; padding: 10px; border-radius: 5px; border: 1px solid #ccc; }
button { padding: 10px 20px; background: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; }
button:hover { background: #0056b3; }
.success { color: green; font-weight: bold; margin-top: 10px; }
</style>
</head>
<body>
<h2 style="text-align:center;">๐ข Hariri Tangazo Linalotembea</h2>
<?php if (isset($success)): ?>
<p class="success"><?= $success ?></p>
<?php endif; ?>
<form method="post">
<label>Andika Tangazo Lako Hapa:</label><br>
<textarea name="new_message"><?= htmlspecialchars($current) ?></textarea><br><br>
<input type="hidden" name="password" value="<?= htmlspecialchars($_POST['password']) ?>" />
<button type="submit">๐พ Hifadhi Tangazo</button>
</form>
</body>
</html>
๐น 2. Unda au Hakikisha Faili tangazo.txt Ipo
Katika folder moja na edit_tangazo.php, tengeneza faili tangazo.txt
Iweke wazi au uandike tangazo la mwanzo.
Mfano:
nginx
Copy
Edit
Karibu kwenye Faulink.com - Tovuti ya Mifumo ya Elimu, Biashara na Teknolojia!
๐น 3. Tumia Fomu ya Web Kuhariri
Tembelea ukurasa: https://yourdomain.com/edit_tangazo.php
Ingiza nenosiri: TANGAZO (au uliloweka)
Jaza tangazo jipya na bonyeza ๐พ Hifadhi Tangazo
Litaandikwa ndani ya tangazo.txt
๐น 4. Mahali pa Kuonyesha Tangazo
Katika sehemu yoyote ya frontend ya blog yako (k.m. index.php), ongeza hii:
php
Copy
Edit
<?php
$tangazoText = file_exists('tangazo.txt') ? trim(file_get_contents('tangazo.txt')) : '';
?>
<?php if (!empty($tangazoText)): ?>
<div class="scrolling-announcement">
<span><?= htmlspecialchars($tangazoText) ?></span>
</div>
<?php endif; ?>
Na kwenye <style>:
css
Copy
Edit
.scrolling-announcement {
background: #ffc107;
color: #000;
font-weight: 700;
font-size: 1.1rem;
overflow: hidden;
white-space: nowrap;
box-sizing: border-box;
padding: 10px 0;
margin: 20px 0;
}
.scrolling-announcement span {
display: inline-block;
padding-left: 100%;
animation: scroll-left 15s linear infinite;
}
.scrolling-announcement:hover span {
animation-play-state: paused;
}
@keyframes scroll-left {
0% { transform: translateX(0%); }
100% { transform: translateX(-100%); }
}