[英]blank page after send
I tried figured out how I can get the if empty() for different field input also if captcha is empty or wrong我试图弄清楚如果验证码为空或错误,我如何获得不同字段输入的 if empty()
this is the page of comments.php这是comments.php的页面
<div class="box">
<ul>
<?php
$result = $com->index();
while ($data = $result->fetch_assoc()) {
?>
<li><b><?php echo $data['name']; ?></b> -
<?php echo date('M d, Y', strtotime($data['comment_time'])); ?> -
<span class="rating"><?=str_repeat('★', $data['rating'])?></span><br>
<?php echo $data['comment'] ?></li>
<?php } ?>
</ul>
</div><br>
<center>
<?php
if (isset($_GET['msg'])) {
$msg = $_GET['msg'];
echo "<span style='font-size:18px'>".$msg."</span>";
}
?>
<div>
<form action="post_comment.php" method="post">
<table>
<tr>
<td>Rating:</td>
<td><ul class="rate-area">
<input type="radio" id="5-star" name="rating" value="5" /><label for="5-star" title="Amazing">5 stars</label>
<input type="radio" id="4-star" name="rating" value="4" /><label for="4-star" title="Good">4 stars</label>
<input type="radio" id="3-star" name="rating" value="3" /><label for="3-star" title="Average">3 stars</label>
<input type="radio" id="2-star" name="rating" value="2" /><label for="2-star" title="Not Good">2 stars</label>
<input type="radio" id="1-star" name="rating" value="1" /><label for="1-star" title="Bad">1 star</label>
</ul></td>
</tr>
<tr>
<td>Your Name:<span style='color:red;font-size:20px'>*</span></td>
<td><input type="text" name="name" placeholder="Please enter your name"></td>
</tr>
<tr>
<td>Comment:<span style='color:red;font-size:20px'>*</span></td>
<td>
<textarea name="comment" placeholder="Please enter your comment"></textarea>
</td>
</tr>
<tr>
<td> <img src="captcha.php?rand=<?php echo rand(); ?>" id='captchaimg' > </td>
<td> <a href='javascript: refreshCaptcha();'><img src="refresh.png" width="25px"></a> </td>
</tr>
<tr>
<td colspan="2">Enter the code above here :
<input id="6_letters_code" name="6_letters_code" type="text" class="form-control"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" id="contact-submit" value="Post"></td>
<!-- <td><input style="width: 230px;height: 40px;" type="submit" name="submit" value="Post"></td> -->
</tr>
</table>
</form>
</div>
<center>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
and this is the post_comments.php这是 post_comments.php
<?php
include_once 'controllers/Comment.php';
$com = new Comment();
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$comment = $_POST['comment'];
$rating = $_POST['rating'];
$capctha = $_POST['6_letters_code'];
if(empty($_SESSION['6_letters_code'] ) ||
strcmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
header('Location: indexcomment.php?msg='.urlencode('The captcha code does not match!'));
}
if (empty($name) || empty($comment)) {
header('Location: indexcomment.php?msg='.urlencode('<span style="color:red;font-size:20px">Field must not be empty !</span>'));
} else {
$com->setData($name, $comment, $rating);
if ($com->create()) {
header('Location: indexcomment.php?msg='.urlencode('Comment Posting Successfully'));
}
}
}
?>
this is the Comment.php这是 Comment.php
<?php
include_once './database/DB.php';
class Comment
{
private $db;
private $name;
private $comment;
private $rating;
private $table = "tbl_comments";
public function __construct()
{
$this->db = new DB();
}
public function setData($name, $comment, $rating)
{
$this->name = $name;
$this->comment = $comment;
$this->rating = $rating;
}
public function create()
{
$query = "INSERT INTO $this->table(name, comment, rating, comment_time) VALUES('$this->name', '$this->comment', '$this->rating', now())";
$insert_comment = $this->db->insert($query);
return $insert_comment;
}
public function index()
{
$query = "SELECT * FROM $this->table ORDER BY id DESC";
$result = $this->db->select($query);
return $result;
}
public function dateFormat($data)
{
date_default_timezone_set('Europe/Berlin');
$date = date('M j, Y', time());
return $date;
}
}
?>
this the DB.php这是 DB.php
<?php
include_once './config/config.php';
class DB
{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
public $link;
public $error;
public function __construct()
{
$this->connectDB();
}
private function connectDB()
{
$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
if (!$this->link) {
echo "Connection fail".$this->link->connect_error;
return false;
}
}
// Insert Data
public function insert($query)
{
$insert_row = $this->link->query($query) or die($this->link->error.__LINE__);
if ($insert_row) {
return $insert_row;
} else {
return false;
}
}
// Select or Read data
public function select($query)
{
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if ($result->num_rows > 0) {
return $result;
} else {
return false;
}
}
}
?>
and this a config.php这是一个 config.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '****************');
define('DB_NAME', 'dbcomment');
?>
and this is the captcha.php这是captcha.php
<?php
session_start();
//Settings: You can customize the captcha here
$image_width = 280;
$image_height = 40;
$characters_on_image = 6;
$font = './fonts/monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ';
$random_dots = 30;
$random_lines = 20;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * .75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>
Somehow I don't get this to work.不知何故,我没有得到这个工作。 I been trying to get captcha done when is wrong or empty to show the error notice like "Security code is invalid" and I also tried to get when the name or text is empty that it will show error notice "Name should not be empty" or "Text should not be empty"我一直在尝试在错误或为空时完成验证码以显示诸如“安全代码无效”之类的错误通知,并且我还尝试在名称或文本为空时显示错误通知“名称不应为空”或“文本不应为空”
I have Debian 10, apache with PHP 8 and mysqli我有 Debian 10、带 PHP 8 的 apache 和 mysqli
Assuming that the $_SESSION['6_letters_code']
exists, this condition is omitted but afterwards nothing happens:假设$_SESSION['6_letters_code']
存在,则省略此条件,但之后没有任何反应:
if(empty($_SESSION['6_letters_code'] ) ||
strcmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
header('Location: indexcomment.php?msg='.urlencode('The captcha code does not match!'));
}
That means even the details in your code are logical correct, there is a gap in the combination of all the details.这意味着即使代码中的细节在逻辑上是正确的,所有细节的组合也存在差距。
I propose the changes like this (in short removing the last else
but executing the code without any condition):我提出这样的更改(简而言之,删除最后一个else
但没有任何条件地执行代码):
<?php
include_once 'controllers/Comment.php';
$com = new Comment();
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$comment = $_POST['comment'];
$rating = $_POST['rating'];
if (empty($name)) {
header('Location: indexcomment.php?msg='.urlencode('<span style="color:red;font-size:20px">Name must not be empty !</span>'));
exit;
}
if (empty($comment)) {
header('Location: indexcomment.php?msg='.urlencode('<span style="color:red;font-size:20px">Comment must not be empty !</span>'));
exit;
}
if(empty($_SESSION['6_letters_code']) ||
strcmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) {
header('Location: indexcomment.php?msg='.urlencode('The captcha code does not match!'));
exit;
}
$com->setData($name, $comment, $rating);
if ($com->create()) {
header('Location: indexcomment.php?msg='.urlencode('Comment Posting Successfully'));
exit;
}
}
?>
I added exit
after each redirect to be sure that the script is stopped, as the redirect opens a new site / script.我在每次重定向后添加了exit
以确保脚本停止,因为重定向会打开一个新站点/脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.