Saltar al contenido
Underc0de /foro

Simple PHP Blog Script v2

🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
fleshworm ·
Greetings, well, I'll tell you I've been a bit distracted since I'm starting my studies, so I've been spending more time away from the PC and more focused on reading and some manuals. This time I'm sharing a PHP script for a Blog app made up of an Index, Config and our object or class Blog.class, which you are free to implement, redistribute or critique. It uses a NO-SQL or MonoDB system as a data abstraction layer (DAL) under the different encoding settings, in this case UTF-8 (ANSI or UNICODE), to store the posts or entries, and it is also implemented with Bootstrap as a design pattern.<br /><br />Filename (index.php)<br />
Código: php
<br /><?php<br />/*** Mini Blog Script v2 By Nickhere Under Creative Commons Atribution 4 ***/<br />error_reporting(E_ALL);<br />date_default_timezone_set("America/Los_Angeles");<br />header("Content-type: text/html; charset=UTF-8;");<br /><br />include("config.php");<br /><br />$blog = new miniblog();<br /><br />$blog->display_posts($postspath);<br /><br />if(isset($_POST["submit_entry"])){<br /> $blog->saveposts($postspath, true);<br />}<br /><br />$blog->saveposts($postspath, 0);<br /><br /><br />?><br />
<br /><br />Filename (config.php)<br />
Código: php
<br /><?php<br />/*** Mini Blog Script v2 By Nickhere Under Creative Commons Atribution 4 ***/<br /><br />include("blog.class.php"); //Include our blog class object for further reference<br /><br />$postspath = "posts_path_here/"; //Desired posts directory with trailing slash(/)<br /><br />?><br />
<br /><br />Filename (blog.class.php)<br />
Código: php
<br /><?php<br />/*** Mini Blog Script v2 By Nickhere Under Creative Commons Atribution 4 ***/<br /> <br />class miniblog{<br /> <br /> public $username;<br /> public $email;<br /> public $title;<br /> public $message;<br /> public $date;<br /> public $post;<br /> <br /> public function __construct(){<br />  $this->username = (!empty($_POST["username"]))? $_POST["username"]: "Anonimo";<br />  $this->email = (!empty($_POST["contact"]))? $_POST["contact"]: "mail@domain";<br />  $this->title = (!empty($_POST["title"]))? $_POST["title"]: "Post title fillin";<br />  $this->message = (!empty($_POST["message"]))? $_POST["message"]: "Lipsum post fillin dummy text";<br />  $this->date = date("M d,Y h:i a");<br /> }<br /> <br /> public function saveposts($path, $attr){<br />  $this->post = $path.date("Ymdhis")."-".rand(00, 99).".data";<br />  if($attr === true){<br />   $handle = fopen($this->post, "a") ;<br />   fwrite($handle, $this->username."\n".$this->email."\n".$this->title."\n".$this->message."\n".$this->date);<br />   fclose($handle);<br />   echo("Your post has been successfully stored, Please wait while we redirect you to your entry");<br />   header("Refresh: 6; url=index.php");<br />  }elseif(isset($path) && $attr === 0){<br />   $form = "<form method=\"post\" action=\"\" id=\"post_form\">";<br />   $form .= "Username<br/><input type=\"text\" name=\"username\" value=\"\" /><br/>";<br />   $form .= "Email<br/><input type=\"text\" name=\"contact\" value=\"\" /><br/>";<br />   $form .= "Title<br/><input type=\"text\" name=\"title\" value=\"\" /><br/>";<br />   $form .= "Message<br/><textarea cols=\"45\" rows=\"6\" name=\"message\"></textarea><br/>";<br />   $form .= "<input type=\"submit\" name=\"submit_entry\" value=\"Submit\" /></form>";<br />   echo($form);<br />  }<br /> }<br /> <br /> public function display_posts($path){<br />  if(is_dir($path) && ($dir = opendir($path))){<br />   while(false != ($data = readdir($dir))){<br />    if($data != "." && $data != ".."){<br />     $posts[] = $data;<br />     foreach($posts as $element){<br />      $post = file($path.$element);<br />      echo("By ".$post[0]." Email ".$post[1]." Date ".$post[4]."<br/><h3>".$post[2]."</h3><p>".$post[3]."</p>");<br />      unset($post);<br />     }<br />    }<br />   }<br />   closedir($dir);<br />  }<br /> }<br /> <br />}<br /> <br />?><br />
<br /><br />To finish, you need to create the directory where your posts will be stored (ie. post_path_here/). Note that the published version is implemented for PHP 5+ and it includes utilities such as a resource index for the next post. Good luck
DuendeSlayeR ·
Very good, thanks.<br /><br /><br />Regards
Grupo de WhatsApp