How To Make a SIMPLE CMS With PHP (Part 1) - humanpuff69 - 11-26-2017
Hi Guys
Today i will show you how to make a simple cms that show a post and you can also create a post to be shown on the index page . you can make a blog with it . so without further a do lets get started
Preparation
- Apache / Nginx Webserver (nginx is reccomended)
- PHP 5+ (PHP 7 IS Reccomended and that is what im using)
- 1 Mysql database with 1 table with the name (entry) that have 5 column (id , title , image_url , content , author (optional))
1.config.php
We need a file to store our CMS setting so star by making a file called config.php . technically you can use any name but i choose config.php because it is easier and the name is showing what the content it is so you need the config php to store variable / setting . in this case we need the config to store the MySQL Credential setting
Code: <?php return array (
'servername' => 'localhost (server location it is probably localhost)',
'username' => 'mysql username ( you can use root but it is very unsafe)',
'password' => 'mysql passwor to that username',
'dbname' => 'name of the database that you want to use',
);
Make sure you have a correct setting otherwise the cms wouldnt work .
2.index.php
this is where the main file of the cms / website is . so your mysql data / entries will be shown here . to start create a index.php file like this . this file basically ill connect to myql server in the config.php and fetch the data from it
Code: <?php
$config = include 'config.php';
$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title , image_url , content , author FROM entry ORDER BY id DESC";
$result = $conn->query($sql);
$conn->close();
?>
<head>
<title>humanpuff69 php tutorial</title>
</head>
<body>
<center><h1>Website Title</h1>
<p><b>HOME</b> </p></center>
<hr>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h2>" . $row["title"] . "</h2><br>
<center><img src=" . '"' . $row["image_url"] . '"' . " width=". '"' ."95%". '"' ."></img></center>
<p>" . $row["content"] . "</p>";
}
} else {
echo "Entry not found";
}
?>
</body>
after that save the file and run it . if you set it up succesfully the website should run with no entry . for how to make the entry with admin.php that is for another part . stay tooned
3.admin.php
Now we need to make the admin page to insert an entry / post to our cms . sorry for random variable name . that is a variable name in indonesian . this cms is based on my other cms that im working on that is indonesian so sorry for that but by the way i already renamed the visible user content so no worry there . some of the text is still indonesian that i forget to translate but this cms should work fine
here is the code
Code: <?php
include('session.php');
$config = include 'config.php';
$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$caption = $_POST['caption'];
$caption = nl2br($caption);
$judul = $_POST['judul'];
$image_url = $_POST['img'];
$caption = $conn->escape_string($caption);
$judul = $conn->escape_string($judul);
$image_url = $conn->escape_string($image_url);
if(!isset($_SESSION['login_user']) && !isset($_SESSION['user_hash'])){
header("location:login.php");
}else {
If ($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['judul']=="") {
$notice_display = "";
$notice_type = "danger";
$notice_bold = "Terjadi Kesalahan ";
$notice = "Tolong masukan judul entri";
} elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
$sql = "INSERT INTO entry (title , content , image_url , author)
VALUES ('$judul' , '$caption', '$image_url' , '$linkfb')";
if ($conn->query($sql) === TRUE) {
$notice_display = "";
$notice_type = "success";
$notice_bold = "Succes! ";
$notice = "Entri has been added";
} else {
$notice_display = "";
$notice_type = "danger";
$notice_bold = "error ";
$notice = "Error occured " . $sql . "<br>" . $conn->error;
}
}
}}
$conn->close();
?>
<!-- Text input-->
<head>
<title>Admin Area</title>
<body>
<p><b><?php echo $notice_bold ?></b></p><br>
<p><?php echo $notice ?></p>
<hr>
<form method="POST" action="admin.php" enctype="multipart/form-data">
<label class="col-md-4 control-label" for="judul">title</label><br>
<input id="judul" name="judul" placeholder="" class="form-control input-md" required="" style="width:100%" type="text"><br>
<label class="col-md-4 control-label" for="judul">Image url</label><br>
<input id="img" name="img" placeholder="" class="form-control input-md" required="" style="width:100%" type="text"><br>
<label class="col-md-4 control-label" for="caption">Content</label><br>
<textarea class="form-control" id="caption" name="caption" style="width:100%"></textarea><br>
<label class="col-md-4 control-label" for="linkfb">Author</label><br>
<input id="linkfb" name="linkfb" placeholder="" class="form-control input-md" required="" style="width:100%" type="text"><br>
<label class="col-md-4 control-label" for="submit"></label>
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</body>
after that save it as admin.php and open the admin.php and try to make a post with it and see the result
in part 2 i will add a login to make it more secure , make it sql injection proof and also make a page to make sure that not all the post show on the same page
thanks for seeing my tutorial hopefully it is useful for who want to learn to make a CMS
|