【问题标题】:Email system, send or save button option电子邮件系统,发送或保存按钮选项
【发布时间】:2014-02-16 10:06:13
【问题描述】:

我正在编写一个简单的电子邮件系统,并且在将其保存到草稿时遇到了逻辑上的复杂性。我已经创建了一个用于撰写邮件的文件。

<?php
include ('connection.php');


$sender = $_POST['sender'];
$sendto = $_POST['sendto'];
$title = $_POST['title'];
$body = $_POST['body'];
$date = date("Y-m-d H:i:s");
$string = strstr($sendto, '@');


    if(!empty($_POST) and ($string != '@raymart.com')) {
    (mysql_query("INSERT INTO sent (sender, sendto, title, body, date) VALUES ('".$sender."', '".$sendto."', '".$title."', '".$body."', '".$date."')") or die(mysql_error())); {
        $message = $body . "\r\n";
        $message .= "Date sent: ". $date;
        mail($sendto,$title,$message,$sender);
        echo 'Email successfully sent!';
        }
    }
    if(!empty($_POST) and ($string == '@raymart.com')) {
    (mysql_query("INSERT INTO sent (sender, sendto, title, body, date) VALUES ('".$sender."', '".$sendto."', '".$title."', '".$body."', '".$date."')") or die(mysql_error()));
    (mysql_query("INSERT INTO inbox (sendto, sender, title, body, date) VALUES ('".$sendto."', '".$sender."', '".$title."', '".$body."', '".$date."')") or die(mysql_error()));
    {
    echo 'Email successfully sent!';
    }
    }

?>
<div class="container">
<form class="form-horizontal" method="post">
<fieldset>

<!-- Form Name -->
<legend>Compose Mail</legend>

<!-- Text input-->
<div class="control-group">
  <label class="control-label" for="sender">From: </label>
  <div class="controls">
    <input id="sender" name="sender" placeholder="Your email" class="input-xlarge cmps" type="text">
  </div>
</div>

<!-- Text input-->
<div class="control-group">
  <label class="control-label" for="sendto">Send To: </label>
  <div class="controls">
    <input id="sendto" name="sendto" placeholder="Send To" class="input-xlarge cmps" type="text">
  </div>
</div>

<!-- Text input-->
<div class="control-group">
  <label class="control-label" for="title">Title: </label>
  <div class="controls">
    <input id="title" name="title" placeholder="Title/Subject" class="input-xlarge cmps" type="text">
  </div>
</div>

<!-- Textarea -->
<div class="control-group">
  <label class="control-label" for="body">Message: </label>
  <div class="controls">                     
    <textarea id="body" name="body" class="cmps"></textarea>
  </div>
</div>
<button type="submit" id="button" class="btn btn-success">Send!</button>
<a href="save.php" class="btn-danger btn">Save to drafts</a>

</fieldset>
</form>
</div>

我还创建了动作.. 即“save.php”

<?php
include ('connection.php');


$sender = $_POST['sender'];
$sendto = $_POST['sendto'];
$title = $_POST['title'];
$body = $_POST['body'];
$date = date("Y-m-d H:i:s");
$newURL = "drafts.php";



    mysql_query("INSERT INTO draft (sender, sendto, title, body, date) VALUES ('".$sender."', '".$sendto."', '".$title."', '".$body."', '".$date."')") or die(mysql_error()); 
        $message = $body . "\r\n";
        $message .= "Date sent: ". $date;
        echo 'Email successfully sent!';
        header('Location: '.$newURL);



?>

它确实向草稿表添加了一个条目,但没有值。小伙伴们一定有什么问题?任何帮助将不胜感激。

【问题讨论】:

  • 你对 SQL 注入攻击持开放态度......而且你的 &lt;form&gt; 没有 action 属性,所以它永远不会提交给 save.php,它只会提交给自己.
  • 您的脚本对 SQL 注入开放。至少对查询字符串中的所有变量使用 mysql_real_escape_string($_POST['sender'])。
  • 感谢您的信息和提示!稍后将更新此线程:) 谢谢!

标签: php email system


【解决方案1】:

通过修改我的标签解决了这个问题

<form class="form-horizontal" method="post" action="save.php">

添加了action="save.php"

并使用 mysql_real_escape_string 来防止 SQL 注入攻击! :)

【讨论】:

  • 您正在使用 mysql_query() - 所有 mysql_* 函数都已弃用,您应该尝试学习 PDO 并使用准备好的语句(这并不难)。来自 PHP 手册:php.net/manual/en/mysqlinfo.api.choosing.php - “建议使用 mysqli 或 PDO_MySQL 扩展。不建议使用旧的 mysql 扩展进行新的开发,因为它已在 PHP 5.5.0 中被弃用,并将被将来删除”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 2011-08-12
  • 2023-03-19
  • 2016-11-09
  • 2018-02-05
  • 1970-01-01
相关资源
最近更新 更多