How to develop a news website using PHP and CGI

WBOY
Release: 2023-07-21 17:26:02
Original
1554 people have browsed it

How to use PHP and CGI to develop news websites

With the rapid development of the Internet, news websites have become an important way for people to obtain news information. Developing an efficient, stable, and easy-to-use news website is an important task for website developers. This article will introduce how to use PHP and CGI to develop a news website, and provide some code examples to help readers better understand.

First, we need to build a web server to run our news website. Here we can use Apache server. For specific steps to set up PHP and CGI environments on the server, please refer to the relevant documentation of the server.

Next, we need to create a database to store news information. Here we can use MySQL database. First, we need to create a news table containing fields such as title, content, release time, etc.

CREATE TABLE news (
  id INT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(255) NOT NULL,
  content TEXT,
  publish_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

Then, we can use PHP to connect to the database and implement related functions. The following is a sample code for adding news:

<?php
$dbHost = 'localhost';
$dbName = 'news_site';
$dbUser = 'root';
$dbPass = '';

$conn = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $title = $_POST['title'];
  $content = $_POST['content'];

  $stmt = $conn->prepare("INSERT INTO news (title, content) VALUES (:title, :content)");
  $stmt->bindParam(':title', $title);
  $stmt->bindParam(':content', $content);
  $stmt->execute();

  // 重定向到新闻列表页面
  header("Location: news_list.php");
  exit;
}
?>
Copy after login

Through the above code, we can get the news titles and content submitted by the user and insert them into the database. After that, we use the header function to redirect the user to the news list page.

Next, we need to write a CGI script to handle the user's request to access the news list page, and obtain the news list from the database and display it on the page. The following is a sample code for a news list page:

#!/usr/bin/php-cgi
<?php
$dbHost = 'localhost';
$dbName = 'news_site';
$dbUser = 'root';
$dbPass = '';

$conn = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);

$stmt = $conn->query('SELECT * FROM news ORDER BY publish_time DESC');
$newsList = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<html>
<head>
  <title>新闻列表</title>
</head>
<body>
  <h1>新闻列表</h1>
  <ul>
    <?php foreach ($newsList as $news) : ?>
      <li><a href="news_detail.php?id=<?php echo $news['id']; ?>"><?php echo $news['title']; ?></a></li>
    <?php endforeach; ?>
  </ul>
</body>
</html>
Copy after login

Through the above code, we can get the news list from the database and display it on the page. Each news title is a link that can be clicked to jump to the news details page.

In addition to the news list page, we also need to write a CGI script to handle the user's request to access the news details page, and obtain the news content from the database and display it. The following is a sample code for a news details page:

#!/usr/bin/php-cgi
<?php
$dbHost = 'localhost';
$dbName = 'news_site';
$dbUser = 'root';
$dbPass = '';

$conn = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);

$id = $_GET['id'];

$stmt = $conn->prepare("SELECT * FROM news WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$news = $stmt->fetch(PDO::FETCH_ASSOC);
?>

<html>
<head>
  <title><?php echo $news['title']; ?></title>
</head>
<body>
  <h1><?php echo $news['title']; ?></h1>
  <p><?php echo $news['content']; ?></p>
</body>
</html>
Copy after login

Through the above code, we can obtain the news content from the database based on the news ID requested by the user and display it on the page.

To sum up, it is not complicated to develop a news website using PHP and CGI. We can implement related functions by building a web server, creating a database, and writing PHP and CGI scripts. I hope this article can provide some help to readers in developing news websites.

The above is the detailed content of How to develop a news website using PHP and CGI. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!