国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

基于PHP和MySQL的新聞發(fā)布系統(tǒng)——【功能優(yōu)化】

這篇具有很好參考價值的文章主要介紹了基于PHP和MySQL的新聞發(fā)布系統(tǒng)——【功能優(yōu)化】。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

2023年第一篇文章,祝大家在新的一年里“卯”足干勁,在技術(shù)上 “兔”飛猛進(jìn)!
基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

上一篇文章 基于PHP和MySQL的新聞發(fā)布系統(tǒng) 給大家介紹了制作一個新聞發(fā)布系統(tǒng)的主要功能的實現(xiàn),在文章的末尾還提出了一些需要完善的方面。那么這篇文章就給我們的新聞發(fā)布系統(tǒng)增加cookie和session來提高用戶的登錄體驗以及增強登錄的安全性。

效果展示

基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

這次我們仿照一個學(xué)校的官網(wǎng)去做一個新聞發(fā)布系統(tǒng),通過增加cookie來實現(xiàn)一天(或多天)免登錄功能;通過添加session來實現(xiàn)非法用戶禁止登錄的功能。
基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

cookie

當(dāng)我們在注冊登錄進(jìn)入某一網(wǎng)站時,就可以通過cookie實現(xiàn)免登錄功能;cookie是網(wǎng)站用戶登陸的憑據(jù)(瀏覽器記住密碼的功能就是通過與瀏覽器中保存的cookie進(jìn)行驗證后進(jìn)行登錄的)。
基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

// 設(shè)置cookie
setcookie("username","張三");
$user=$_COOKIE['username'];
echo "$user";//輸出張三

//設(shè)置cookie過期時間
setcookie("username","李四",time()+3600*24);//表示一天后過期
$user=$_COOKIE['username'];
echo "$user";

// 訪問時間  訪問時間顯示格式 
setcookie("visitime",date("Y-m-d H:i:s"));
$time=$_COOKIE['visitime'];
echo "$time";//輸出當(dāng)前訪問的時間

//記錄登錄時間
date_default_timezone_set("Asia/Shanghai");
if(!isset($_COOKIE['visitime'])){
    setcookie("visitime",date("Y-m-d  H:i:s"),time()+3600);
    // $time=$_COOKIE['visitime'];
    echo "歡迎您首次訪問該網(wǎng)站";
}else{
    echo "您上次訪問網(wǎng)站的時間是".$_COOKIE['visitime'];
}

// 刪除cookie存儲數(shù)據(jù)
setcookie("visitime","",time()-1);

代碼實現(xiàn)功能
(1)登錄頁面添加 “實現(xiàn)免登錄”功能的按鈕

<form action="login_do.php" method="post" id="myform">
        請輸入用戶名:<input type="text" name="username" id="username">
        請輸入密碼:<input type="password" name="userpwd" id="userpwd">
        <input type="submit" value="登錄">
        <span><input type="checkbox" name="free">七天免登錄</span>
    </form>

(2)判斷用戶是否選擇免登錄功能,并實現(xiàn)免登錄功能。

include "./conn.php";
$username=$_POST['username'];
$userpwd=md5($_POST['userpwd']);
$free=$_POST['free'];//post傳遞復(fù)選框選中與否的狀態(tài)
$sel="select * from admin where username='{$username}';";
$rs=$mysqli->query($sel);
$rows=$mysqli->affected_rows;
if($free){
    setcookie("username",$username,time()+7*24*3600);
    setcookie("userpwd",$userpwd,time()+7*24*3600);
}
if($rows>0){
    // 用戶名存在  驗證用戶輸入的密碼和數(shù)據(jù)表中存在的用戶名對應(yīng)的密碼是否一致
$result=$rs->fetch_assoc();
if($userpwd==$result['userpwd']){
    echo "<script>alert('登錄成功');location.href='news_select.php'</script>";
}else{
//密碼有誤不可登錄,清除cookie數(shù)據(jù)
    setcookie("username","",time()-1);
    setcookie("userpwd","",time()-1);
    echo "<script>alert('密碼錯誤,請重新輸入或注冊');location.href='login.php'</script>";
}
}else{
    // 用戶名不存在   清除cookie數(shù)據(jù)
    setcookie("username","",time()-1);
    setcookie("userpwd","",time()-1);
    echo "<script>alert('用戶名不存在,請重新輸入或者先注冊');location.href='login.php';</script>" ;
}

session

session用于保存登錄用戶的信息,可以實現(xiàn)禁止非法用戶(未登錄用戶)登錄。
基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

// 啟動會話
session_start();

// 注冊會話
$_SESSION['username']='張三';

//刪除會話
unset($_SESSION['username']);
//判斷是否為登錄用戶,如果不是就彈出警告框,阻止非法用戶對信息的查看。
//將該代碼寫在需要進(jìn)行登錄后才能查看的頁面中即可。
session_start();
if(!isset($_SESSION['username'])){
    echo "<script>alert('非法用戶,請先登錄!');location.href='login.php';</script>" ;
    exit;
}

代碼實現(xiàn)功能

include "./conn.php";
$username=$_POST['username'];
$userpwd=md5($_POST['userpwd']);
$free=$_POST['free'];//設(shè)置cookie時不要忘記接收登錄頁面?zhèn)鱽淼?free
$sel="select * from admin where username='{$username}';";
$rs=$mysqli->query($sel);
$rows=$mysqli->affected_rows;
if($free){
    setcookie("username",$username,time()+7*24*3600);
    setcookie("userpwd",$userpwd,time()+7*24*3600);
}
if($rows>0){
    // 用戶名存在  驗證用戶輸入的密碼和數(shù)據(jù)表中存在的用戶名對應(yīng)的密碼是否一致
$result=$rs->fetch_assoc();
if($userpwd==$result['userpwd']){
    echo "<script>alert('登錄成功');location.href='news_select.php'</script>";
    //登錄成功就開啟session,存儲成功登錄的用戶名
    session_start();
    $_SESSION['username']=$username;
}else{
    setcookie("username","",time()-1);
    setcookie("userpwd","",time()-1);
    echo "<script>alert('密碼錯誤,請重新輸入或注冊');location.href='login.php'</script>";
}
}else{
    // 用戶名不存在
    setcookie("username","",time()-1);
    setcookie("userpwd","",time()-1);
    echo "<script>alert('用戶名不存在,請重新輸入或者先注冊');location.href='login.php';</script>" ;
}

兩者區(qū)別

cookie session
存儲在本地 存儲在服務(wù)器
存活時間可設(shè)置 存活時間較短
安全性相對較低,但不需要占用服務(wù)器的資源 安全性相對較高,但會大量占用服務(wù)器的資源

新增功能——圖片上傳和讀取

在這個新聞發(fā)布系統(tǒng)中,我們可以通過增加一個圖片上傳和讀取的功能來優(yōu)化這個系統(tǒng):實現(xiàn)這個功能的主要思路是:上傳圖片時將圖片的類型以及名稱讀取并存儲到新建的數(shù)據(jù)表中;顯示圖片時將所選需要查看的圖片的id通過get傳遞,然后執(zhí)行SQL語句將圖片顯示出來。

新建文件夾

首先我們需要創(chuàng)建一個用于存放需要上傳的圖片的文件夾:
基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

新建數(shù)據(jù)表

接著創(chuàng)建一個用于存儲上傳圖片的數(shù)據(jù)表:
基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

代碼完善

上傳圖片:

基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

 <!-- test_insert.php -->
    <form action="test_insert_do.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label><br />
        請輸入資源標(biāo)題:<input type="text" name="test"></br>
        請輸入上傳者姓名:<input type="text" name="author"><br>
        請輸入試題描述:<textarea name="content" id="" cols="30" rows="10"></textarea><br>
        請上傳文件: <input type="file" name="file" id="file" /> <br />
        <input type="submit" name="submit" value="上傳" />

    </form>
//test_insert_do.php
include './conn.php';
$test = $_POST['test'];
$content = $_POST['content'];
$author = $_POST['author'];
$submit = $_POST['submit'];
$ctime = time();
$mtime = time();
// 當(dāng)點擊submit時,存儲上傳圖片的信息
if (isset($_POST['submit'])) {
    if ($_FILES["file"]["error"] > 0) { //如果上傳出錯
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    } else {
        $image = $_FILES["file"]["name"]; // 存儲在服務(wù)器的文件的臨時副本的名稱
        $type = $_FILES["file"]["type"]; //被上傳文件的類型 

        // 判斷在該路徑中是否有相同名字和類型的圖片
        if (file_exists("../imgs" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            //不存在的話就將圖片另存為自己的路徑下
            move_uploaded_file(
                $_FILES["file"]["tmp_name"],
                "../imgs/" . $_FILES["file"]["name"]
            );
        }
    }
}
//將圖片存入tests數(shù)據(jù)表
$in = "insert into tests(test,content,author,ctime,mtime,type,photoname)values
('{$test}','{$content}','{$author}',$ctime,$mtime,'{$type}','{$image}');";
$st = $mysqli->query($in);

if ($st) {
    echo "<script>alert('發(fā)布成功!');location.href='test_select.php';</script>";
} else {
    echo "<script>alert('發(fā)布失?。?);location.href='test_select.php';</script>";
}

對圖片實現(xiàn)查、刪、改

基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言查看圖片

//test_check.php
include "./conn.php";
include "./session.php";
$id = $_GET['id'];
//找到指定圖片——將查找的指定id的圖片的信息轉(zhuǎn)化成一維數(shù)組,根據(jù)圖片名+存儲路徑的方式將其顯示出來。
$sqlstr2 = "select * from tests where id={$id}";
$word2 = $mysqli->query($sqlstr2);
$thread = $word2->fetch_assoc();
if ($thread) {
    header('content_type:' . $thread['type']);
    echo "<img  src='../imgs/" . $thread['photoname'] . "'/>";
}

其中刪除圖片和修改圖片不再贅述,詳細(xì)的注釋可以參考上一篇文章 基于PHP和MySQL的新聞發(fā)布系統(tǒng) 。
刪除圖片

//test_delete.php
include "./conn.php";
include "./session.php";
$id=$_GET['id'];
$de="delete from tests where id={$id};";
$del=$mysqli->query($de);
if($del){
    echo "<script>alert('刪除成功!');location.href='./test_select.php';</script>";
}else{
    echo "<script>alert('刪除失敗!');location.href='./test_select.php';</script>";
}

修改圖片

//test_update.php
include "./conn.php";
$id = $_GET['id'];
include "./session.php";
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="test_update_do.php?id=<?= $id ?>" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label><br />
        請輸入資源標(biāo)題:<input type="text" name="test"></br>
        請輸入上傳者姓名:<input type="text" name="author"><br>
        請輸入試題描述:<textarea name="content" id="" cols="30" rows="10"></textarea><br>
        <!-- 隱藏域獲取id -->
        <input type="hidden" name="id" value="<?= $id ?>">
        請上傳文件: <input type="file" name="file" id="file" /> <br />
        <input type="submit" name="submit" value="修改" />
    </form>
</body>

</html>
//test_update_do.php
require("./conn.php");
$id=$_POST['id'];
$test=$_POST['test'];
$content=$_POST['content'];
$author=$_POST['author'];
$submit=$_POST['submit'];
$mtime=time();

if (isset($_POST['submit'])) {
    if ($_FILES["file"]["error"] > 0) { //如果上傳出錯
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    } else {
        $image = $_FILES["file"]["name"]; // 存儲在服務(wù)器的文件的臨時副本的名稱
        $type = $_FILES["file"]["type"]; //被上傳文件的類型 

        // 判斷在改路徑中是否有相同名字和類型的圖片
        if (file_exists("../imgs" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            //不存在的話就將圖片另存為自己的路徑下
            move_uploaded_file(
                $_FILES["file"]["tmp_name"],
                "../imgs/" . $_FILES["file"]["name"]
            );
        }
    }
}

$upd="update  tests set test='{$test}',content='{$content}',author='{$author}',mtime=$mtime,type='{$type}',photoname='{$image}' where id={$id};"; 
$st=$mysqli->query($upd);
if($st){
    echo "<script>alert('修改成功');location.href='test_select.php';</script>";
}else{
    echo "<script>alert('修改失敗');location.href='test_select.php';</script>";
}

總結(jié)

如果我們需要經(jīng)常登錄一個站點時,可以用cookie來保存信息,這樣可以避免每次都需要重新輸入用戶名和密碼進(jìn)行登錄的繁瑣;當(dāng)對于需要安全性高的站點以及更好的控制數(shù)據(jù)時,我們可以結(jié)合兩者,使網(wǎng)站按照我們的想法進(jìn)行運行。
基于php的新聞發(fā)布系統(tǒng),PHP,MySQL數(shù)據(jù)庫,php,mysql,數(shù)據(jù)庫,開發(fā)語言

當(dāng)然在忙碌的學(xué)習(xí)中,也不要忘記在這美好的時光里多陪伴陪伴家人呀!再次祝大家事事順心!

如有不足,感謝指正!文章來源地址http://www.zghlxwxcb.cn/news/detail-777488.html

到了這里,關(guān)于基于PHP和MySQL的新聞發(fā)布系統(tǒng)——【功能優(yōu)化】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包