Skip to content

Commit fb83e87

Browse files
committed
URL Rewrite
1 parent 2a319f5 commit fb83e87

19 files changed

+257
-97
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

.htaccess

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RewriteEngine On
2+
3+
RewriteRule ^p/(.*)$ view.php?id=$1 [QSA,L]

README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,35 @@ Simple blog system for personal development using procedural PHP and MYSQL.
44
For educational purposes only.
55

66
# Setup
7-
Update the `config.php` file with your database credentials.
7+
8+
Update the `connect.php` file with your database credentials.
89
Import the `database.sql` file.
910

11+
If installed on a sub-folder, edit the `config.php` and replace the empty constant with the folder's name.
12+
13+
The pagination results per page can be set on the `config.php` file.
14+
15+
### URL Rewrite
16+
The latest update introduces 'slugs', also known as 'SEO URLs'.
17+
After you update to the latest version, click on the "Generate slugs (SEO URLs)" button on the admin dashboard and slugs will be generated for all existing posts.
18+
19+
The blog posts URL structure is like this: `http://localhost/p/4/apple-reveals-apple-watch-series-7`
20+
21+
If you use Apache, enable the Apache rewrite module for the .htaccess rewrite rule to work.
22+
23+
If you use NGINX, you can insert something similar to the code below in your NGINX configuration block.
24+
```
25+
location / {
26+
rewrite ^p/(.*) view.php?id=$1;
27+
}
28+
```
29+
1030
# Default Admin Login
1131
Username: admin
12-
Password: 12345
32+
Password: 12345
33+
34+
There is no way to update the admin password through the dashboard yet.
35+
To change your password, hash your password with PHP's `password_hash()` function. Then update the database value with the new password hash.
1336

1437
# Screenshots
1538

admin.php

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
<?php
2-
include("connect.php");
3-
include("header.php");
4-
include("security.php");
2+
require_once 'connect.php';
3+
require_once 'header.php';
4+
require_once 'security.php';
55
?>
66
<h2 class="w3-container w3-teal w3-center">Admin Dashboard</h2>
77
<div class="w3-container">
88
<p>Welcome <?php echo $_SESSION['username']; ?>,</p>
9-
<a href="new.php">Create new post</a>
9+
<p><a href="new.php" class="w3-button w3-teal">Create new post</a></p>
10+
<p><a href="generate_slugs.php" class="w3-button w3-teal">Generate slugs (SEO URLs)</a></p>
11+
1012
</div>
1113
<h5 class="w3-center">Posts</h5>
1214
<?php
1315
$sql = "SELECT COUNT(*) FROM posts";
1416
$result = mysqli_query($dbcon, $sql);
1517
$r = mysqli_fetch_row($result);
18+
1619
$numrows = $r[0];
17-
$rowsperpage = 5;
20+
$rowsperpage = PAGINATION;
1821
$totalpages = ceil($numrows / $rowsperpage);
1922
$page = 1;
23+
2024
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
2125
$page = (INT)$_GET['page'];
2226
}
@@ -42,16 +46,20 @@
4246
echo "<th>Views</th>";
4347
echo "<th>Action</th>";
4448
echo "</tr>";
49+
4550
while ($row = mysqli_fetch_assoc($result)) {
4651
$id = $row['id'];
4752
$title = $row['title'];
48-
$by = $row['posted_by'];
53+
$slug = $row['slug'];
54+
$author = $row['posted_by'];
4955
$time = $row['date'];
56+
57+
$permalink = "p/".$id ."/".$slug;
5058
?>
5159

5260
<tr>
5361
<td><?php echo $id; ?></td>
54-
<td><a href="view.php?id=<?php echo $id; ?>"><?php echo substr($title, 0, 50); ?></a></td>
62+
<td><a href="<?php echo $permalink; ?>"><?php echo substr($title, 0, 50); ?></a></td>
5563
<td><?php echo $time; ?></td>
5664
<td><a href="edit.php?id=<?php echo $id; ?>">Edit</a> | <a href="del.php?id=<?php echo $id; ?>"
5765
onclick="return confirm('Are you sure you want to delete this post?')">Delete</a>
@@ -61,8 +69,9 @@
6169
<?php
6270
}
6371
echo "</table>";
72+
6473
// pagination
65-
echo "<div class='w3-bar w3-center'>";
74+
echo "<p><div class='w3-bar w3-center'>";
6675
if ($page > 1) {
6776
echo "<a href='?page=1' class='w3-btn'><<</a>";
6877
$prevpage = $page - 1;
@@ -83,6 +92,6 @@
8392
echo "<a href='?page=$nextpage' class='w3-btn'>></a>";
8493
echo "<a href='?page=$totalpages' class='w3-btn'>>></a>";
8594
}
86-
echo "</div>";
95+
echo "</div></p>";
8796

8897
include("footer.php");

cat.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
include("connect.php");
3-
include("header.php");
2+
require_once 'connect.php';
3+
require_once 'header.php';
44

55
$id = (INT)$_GET['id'];
66
if ($id < 1) {

config.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
define('SITE_ROOT', ''); // If installed on a sub-folder, replace the empty constant with the folder's name
3+
define('PAGINATION', 10); // Pagination results per page

connect.php

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
ob_start();
3+
session_start();
4+
25
$dbhost = "localhost";
36
$dbuser = "root";
47
$dbpass = "";

database.sql

+6-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ CREATE TABLE `posts` (
6565
`id` int(11) NOT NULL,
6666
`title` varchar(255) NOT NULL,
6767
`description` text NOT NULL,
68+
`slug` varchar(255) DEFAULT NULL,
6869
`posted_by` varchar(255) NOT NULL,
6970
`date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
7071
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
@@ -73,11 +74,11 @@ CREATE TABLE `posts` (
7374
-- Dumping data for table `posts`
7475
--
7576

76-
INSERT INTO `posts` (`id`, `title`, `description`, `posted_by`, `date`) VALUES
77-
(1, 'Cristiano Ronaldo Returns to Manchester United', '<img class=\"w3-image\" src=\"https://assets.manutd.com/AssetPicker/images/0/0/15/128/1016013/CR_Home_21630596121972_large.jpg\" alt=\"\"><p>\r\n\r\nManchester United is delighted to confirm the signing of Cristiano Ronaldo on a two-year contract with the option to extend for a further year, subject to international clearance.\r\n\r\n\r\nCristiano, a five-time Ballon D’or winner, has so far won over 30 major trophies during his career, including five UEFA Champions League titles, four FIFA Club World Cups, seven league titles in England, Spain and Italy, and the UEFA European Championship for his native Portugal. Cristiano is the first player to have won league titles in England, Spain and Italy. He was also the highest goalscorer in last season’s Serie A and won the golden boot at this year’s European Championship. In his first spell for Manchester United, he scored 118 goals in 292 games.</p><p>Cristiano Ronaldo said:\r\n\r\n“Manchester United is a club that has always had a special place in my heart, and I have been overwhelmed by all the messages I have received since the announcement on Friday. I cannot wait to play at Old Trafford in front of a full stadium and see all the fans again. I\'m looking forward to joining up with the team after the international games, and I hope we have a very successful season ahead.”</p><p>Ole Gunnar Solskjaer said:\r\n\r\n“You run out of words to describe Cristiano. He is not only a marvellous player, but also a great human being. To have the desire and the ability to play at the top level for such a long period requires a very special person. I have no doubt that he will continue to impress us all and his experience will be so vital for the younger players in the squad. Ronaldo’s return demonstrates the unique appeal of this club and I am absolutely delighted he is coming home to where it all started.” </p> ', 'Admin', '2021-09-19 15:25:04'),
78-
(2, 'Leo Messi signs for Paris Saint-Germain', '<p style=\"text-align: center; \"><img src=\"https://images.psg.media/media/209006/leo-cp.jpg?anchor=center&amp;mode=crop&amp;width=800&amp;height=500&amp;quality=80\" alt=\"\"><br></p><p><strong>Paris Saint-Germain is delighted to announce the signing of Leo Messi on a two-year contract with an option of a third year.\r\n\r\nThe six-time Ballon d’Or winner is justifiably considered a legend of the game and a true inspiration for those of all ages inside and outside football.</strong></p><p>The signing of Leo reinforces Paris Saint-Germain’s aspirations as well as providing the club’s loyal fans with not only an exceptionally talented squad, but also moments of incredible football in the coming years.</p><p>Leo Messi said: “I am excited to begin a new chapter of my career at Paris Saint-Germain. Everything about the club matches my football ambitions. I know how talented the squad and the coaching staff are here. I am determined to help build something special for the club and the fans, and I am looking forward to stepping out onto the pitch at the Parc des Princes.”</p><p>Nasser Al-Khelaifi, Chairman and CEO of Paris Saint-Germain said: “I am delighted that Lionel Messi has chosen to join Paris Saint-Germain and we are proud to welcome him and his family to Paris. He has made no secret of his desire to continue competing at the very highest level and winning trophies, and naturally our ambition as a club is to do the same. The addition of Leo to our world class squad continues a very strategic and successful transfer window for the club. Led by our outstanding coach and his staff, I look forward to the team making history together for our fans all around the world.” </p> ', 'Admin', '2021-09-19 15:28:16'),
79-
(3, 'Apple Introduces iPhone 13 and iPhone 13 Mini', '<p style=\"text-align: center; \"><img src=\"https://www.apple.com/newsroom/images/product/iphone/geo/Apple_iphone13_hero_geo_09142021_inline.jpg.large.jpg\" alt=\"\"><strong><br></strong></p><p><strong>CUPERTINO, CALIFORNIA</strong> Apple today introduced iPhone 13 and iPhone 13 mini, the next generation of the world’s best smartphone, featuring a beautiful design with sleek flat edges in five gorgeous new colours. Both models feature major innovations, including the most advanced dual-camera system ever on iPhone — with a new Wide camera with bigger pixels and sensor-shift optical image stabilisation (OIS) offering improvements in low-light photos and videos, a new way to personalise the camera with Photographic Styles, and Cinematic mode, which brings a new dimension to video storytelling. iPhone 13 and iPhone 13 mini also boast super-fast performance and power efficiency with A15 Bionic, longer battery life, a brighter Super Retina XDR display that brings content to life, incredible durability with the Ceramic Shield front cover, double the entry-level storage at 128GB, an industry-leading IP68 rating for water resistance, and an advanced 5G experience.</p>', 'admin', '2021-09-19 15:48:29'),
80-
(4, 'Apple Reveals Apple Watch Series 7', '<img src=\"https://www.apple.com/newsroom/images/product/watch/standard/Apple_watch-series7_hero_09142021_big.jpg.large.jpg\" alt=\"\"><p><strong>\r\nCUPERTINO, CALIFORNIA</strong> Apple today announced Apple Watch Series 7, featuring a reengineered Always-On Retina display with significantly more screen area and thinner borders, making it the largest and most advanced display ever. The narrower borders allow the display to maximise screen area, while minimally changing the dimensions of the watch itself. The design of Apple Watch Series 7 is refined with softer, more rounded corners, and the display has a unique refractive edge that makes full-screen watch faces and apps appear to seamlessly connect with the curvature of the case. Apple Watch Series 7 also features a user interface optimised for the larger display, offering greater readability and ease of use, plus two unique watch faces — Contour and Modular Duo — designed specifically for the new device. With the improvements to the display, users benefit from the same all-day 18-hour battery life,1 now complemented by 33 percent faster charging.</p>', 'admin', '2021-09-19 16:50:00');
77+
INSERT INTO `posts` (`id`, `title`, `description`, `slug`, `posted_by`, `date`) VALUES
78+
(1, 'Cristiano Ronaldo Returns to Manchester United', '<img class=\"w3-image\" src=\"https://assets.manutd.com/AssetPicker/images/0/0/15/128/1016013/CR_Home_21630596121972_large.jpg\" alt=\"\"><p>\r\n\r\nManchester United is delighted to confirm the signing of Cristiano Ronaldo on a two-year contract with the option to extend for a further year, subject to international clearance.\r\n\r\n\r\nCristiano, a five-time Ballon D’or winner, has so far won over 30 major trophies during his career, including five UEFA Champions League titles, four FIFA Club World Cups, seven league titles in England, Spain and Italy, and the UEFA European Championship for his native Portugal. Cristiano is the first player to have won league titles in England, Spain and Italy. He was also the highest goalscorer in last season’s Serie A and won the golden boot at this year’s European Championship. In his first spell for Manchester United, he scored 118 goals in 292 games.</p><p>Cristiano Ronaldo said:\r\n\r\n“Manchester United is a club that has always had a special place in my heart, and I have been overwhelmed by all the messages I have received since the announcement on Friday. I cannot wait to play at Old Trafford in front of a full stadium and see all the fans again. I\'m looking forward to joining up with the team after the international games, and I hope we have a very successful season ahead.”</p><p>Ole Gunnar Solskjaer said:\r\n\r\n“You run out of words to describe Cristiano. He is not only a marvellous player, but also a great human being. To have the desire and the ability to play at the top level for such a long period requires a very special person. I have no doubt that he will continue to impress us all and his experience will be so vital for the younger players in the squad. Ronaldo’s return demonstrates the unique appeal of this club and I am absolutely delighted he is coming home to where it all started.” </p> ', 'cristiano-ronaldo-returns-to-manchester-united', 'Admin', '2022-02-11 15:50:41'),
79+
(2, 'Leo Messi signs for Paris Saint-Germain', '<p style=\"text-align: center; \"><img src=\"https://images.psg.media/media/209006/leo-cp.jpg?anchor=center&amp;mode=crop&amp;width=800&amp;height=500&amp;quality=80\" alt=\"\"><br></p><p><strong>Paris Saint-Germain is delighted to announce the signing of Leo Messi on a two-year contract with an option of a third year.\r\n\r\nThe six-time Ballon d’Or winner is justifiably considered a legend of the game and a true inspiration for those of all ages inside and outside football.</strong></p><p>The signing of Leo reinforces Paris Saint-Germain’s aspirations as well as providing the club’s loyal fans with not only an exceptionally talented squad, but also moments of incredible football in the coming years.</p><p>Leo Messi said: “I am excited to begin a new chapter of my career at Paris Saint-Germain. Everything about the club matches my football ambitions. I know how talented the squad and the coaching staff are here. I am determined to help build something special for the club and the fans, and I am looking forward to stepping out onto the pitch at the Parc des Princes.”</p><p>Nasser Al-Khelaifi, Chairman and CEO of Paris Saint-Germain said: “I am delighted that Lionel Messi has chosen to join Paris Saint-Germain and we are proud to welcome him and his family to Paris. He has made no secret of his desire to continue competing at the very highest level and winning trophies, and naturally our ambition as a club is to do the same. The addition of Leo to our world class squad continues a very strategic and successful transfer window for the club. Led by our outstanding coach and his staff, I look forward to the team making history together for our fans all around the world.” </p> ', 'leo-messi-signs-for-paris-saint-germain', 'Admin', '2022-02-11 15:50:41'),
80+
(3, 'Apple Introduces iPhone 13 and iPhone 13 Mini', '<p style=\"text-align: center; \"><img src=\"https://www.apple.com/newsroom/images/product/iphone/geo/Apple_iphone13_hero_geo_09142021_inline.jpg.large.jpg\" alt=\"\"><strong><br></strong></p><p><strong>CUPERTINO, CALIFORNIA</strong> Apple today introduced iPhone 13 and iPhone 13 mini, the next generation of the world’s best smartphone, featuring a beautiful design with sleek flat edges in five gorgeous new colours. Both models feature major innovations, including the most advanced dual-camera system ever on iPhone — with a new Wide camera with bigger pixels and sensor-shift optical image stabilisation (OIS) offering improvements in low-light photos and videos, a new way to personalise the camera with Photographic Styles, and Cinematic mode, which brings a new dimension to video storytelling. iPhone 13 and iPhone 13 mini also boast super-fast performance and power efficiency with A15 Bionic, longer battery life, a brighter Super Retina XDR display that brings content to life, incredible durability with the Ceramic Shield front cover, double the entry-level storage at 128GB, an industry-leading IP68 rating for water resistance, and an advanced 5G experience.</p>', 'apple-introduces-iphone-13-and-iphone-13-mini', 'admin', '2022-02-11 15:50:41'),
81+
(4, 'Apple Reveals Apple Watch Series 7', '<img src=\"https://www.apple.com/newsroom/images/product/watch/standard/Apple_watch-series7_hero_09142021_big.jpg.large.jpg\" alt=\"\"><p><strong>\r\nCUPERTINO, CALIFORNIA</strong> Apple today announced Apple Watch Series 7, featuring a reengineered Always-On Retina display with significantly more screen area and thinner borders, making it the largest and most advanced display ever. The narrower borders allow the display to maximise screen area, while minimally changing the dimensions of the watch itself. The design of Apple Watch Series 7 is refined with softer, more rounded corners, and the display has a unique refractive edge that makes full-screen watch faces and apps appear to seamlessly connect with the curvature of the case. Apple Watch Series 7 also features a user interface optimised for the larger display, offering greater readability and ease of use, plus two unique watch faces — Contour and Modular Duo — designed specifically for the new device. With the improvements to the display, users benefit from the same all-day 18-hour battery life,1 now complemented by 33 percent faster charging.</p>', 'apple-reveals-apple-watch-series-7', 'admin', '2022-02-11 15:50:41');
8182

8283
--
8384
-- Indexes for dumped tables

del.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
2-
session_start();
3-
include("connect.php");
4-
include("security.php");
2+
require_once 'connect.php';
3+
require_once 'security.php';
54

65
if (isset($_GET['id'])) {
76
$id = mysqli_real_escape_string($dbcon, (int) $_GET['id']);

0 commit comments

Comments
 (0)