:::

6-1 post.php

<?php
  // 啟用 session
  session_start();

  /*----引入檔案----*/
  require_once "config.php";
  require_once "function.php";



  /*----整理傳進來的變數或變數初始值----*/

  //設定樣板中 [var.變數] 對應到的幾個變數其預設值
  $news_title=isset($_SESSION['news_title'])?$_SESSION['news_title']:"";
  $news_content=isset($_SESSION['news_content'])?$_SESSION['news_content']:"";
  $author=isset($_SESSION['author'])?$_SESSION['author']:"";
  $post_time=isset($_SESSION['post_time'])?$_SESSION['post_time']:date("Y-m-d H:i:s");
  $ip=$_SERVER['REMOTE_ADDR'];
  $passwd_error=$next_op='';

  $op=isset($_REQUEST['op'])?$_REQUEST['op']:"";
  $sn=isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";


  /*----流程控制----*/

  switch ($op) {
    case 'passwd_error':
      $passwd_error=error_msg("密碼錯誤!!請檢查密碼是否正確!");
      $next_op=empty($sn)?"save":"update";
      break;

    case 'db_error':
      $passwd_error=error_msg("資料庫連線錯誤!!請檢查資料庫帳號、密碼是否正確!");
      break;

    case 'save':
      save_news();
      header("location:index.php");
      exit;
      break;

    case 'update':
      update_news();
      header("location:index.php?op=view&sn={$sn}");
      exit;
      break;

     case 'modify':
       link_db();
       //讀取eznews資料表所有欄位,並指定某一筆特定資料
       $sql="select * from eznews where sn='$sn'";

       //傳回值存到 $result 以供抓取資料用
       $result=mysql_query($sql) or die("{$sql}<br>".mysql_error());
       $news=mysql_fetch_assoc($result);
       $news_title=$news['news_title'];
       $news_content=$news['news_content'];
       $next_op="update";
       break;

    default:
      $next_op="save";
      break;
  }


  /*----輸出----*/
  show_page('post_tpl');



  /*----所有函數----*/
  //顯示錯誤訊息
  function error_msg($msg=""){
    $passwd_error="
    <div class='alert alert-danger'>
      $msg
    </div>";
    return $passwd_error;
  }

  //儲存新聞
  function save_news(){

    //過濾外面傳來的變數
    $op=isset($_POST['op'])?$_POST['op']:"";
    $password=isset($_POST['password'])?$_POST['password']:"";
    $news_title=isset($_POST['news_title'])?check_input($_POST['news_title']):"";
    $news_content=isset($_POST['news_content'])?check_input($_POST['news_content']):"";
    $author=isset($_POST['author'])?check_input($_POST['author']):"";
    $ip=isset($_POST['ip'])?check_input($_POST['ip']):"";
    $post_time=isset($_POST['post_time'])?check_input($_POST['post_time']):"";

    //接收使用者輸入密碼,失敗轉回發布頁
    if(empty($password) or $password!=_POST_PASSWD){
      // 產生 cookie
      // setcookie('news_title',$news_title);
      // setcookie('news_content',$news_content);

      // 產生 session,以記住剛剛使用者輸入的資料,避免需要重打
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=passwd_error");
      exit;
    }else{
      // nl2br()將換行符號轉換為<br>標籤
      //$news_content=nl2br($news_content);
      // 發布正確,剛剛輸入的內容無需在記住,因此清空之。
      $_SESSION['news_title']=$_SESSION['news_content']='';
    }

    if(!link_db()){
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=db_error");
      exit;
    }
    //存到資料庫
    $sql="insert into eznews (news_title, news_content, ip, author, post_time) values('$news_title', '$news_content', '$ip', '$author', '$post_time')";
    mysql_query($sql) or die($sql."<br>".mysql_error());
  }

  

  //更新新聞
  function update_news(){

    //過濾外面傳來的變數
    $password=isset($_POST['password'])?$_POST['password']:"";
    $news_title=isset($_POST['news_title'])?check_input($_POST['news_title']):"";
    $news_content=isset($_POST['news_content'])?check_input($_POST['news_content']):"";
    $author=isset($_POST['author'])?check_input($_POST['author']):"";
    $ip=isset($_POST['ip'])?check_input($_POST['ip']):"";
    $post_time=isset($_POST['post_time'])?check_input($_POST['post_time']):"";

    $sn=isset($_POST['sn'])?intval($_POST['sn']):"";

    //接收使用者輸入密碼,失敗轉回發布頁
    if(empty($password) or $password!=_POST_PASSWD){
      // 產生 cookie
      // setcookie('news_title',$news_title);
      // setcookie('news_content',$news_content);

      // 產生 session,以記住剛剛使用者輸入的資料,避免需要重打
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=passwd_error&sn=$sn");
      exit;
    }else{
      // nl2br()將換行符號轉換為<br>標籤
      //$news_content=nl2br($news_content);
      // 發布正確,剛剛輸入的內容無需在記住,因此清空之。
      $_SESSION['news_title']=$_SESSION['news_content']='';
    }

    if(!link_db()){
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=db_error");
      exit;
    }

    //存到資料庫
    $sql="update eznews set news_title='$news_title', news_content='$news_content', ip='$ip', author='$author', post_time='$post_time' where sn='$sn'";
    mysql_query($sql) or die($sql."<br>".mysql_error());
  }


  //替特殊符號加入反斜線
  function check_input($value){
    if (!get_magic_quotes_gpc()){
      $value = addslashes($value);
    }
    return $value;
  }


?>

 


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1071%26tbsn%3D34

書籍目錄

展開 | 闔起

線上使用者

361人線上 (147人在瀏覽線上書籍)

會員: 0

訪客: 361

更多…