我正在尝试编写一个简单的数学验证码函数来验证允许用户在博客上发布特定文章的表单。
函数:
function create_captcha($num1, $num2) {
$num1 = (int)$num1;
$num2 = (int)$num2;
$rand_num_1 = mt_rand($num1, $num2);
$rand_num_2 = mt_rand($num1, $num2);
$result = $rand_num_1 + $rand_num_2;
return $result;
}
表格:
<h2 class="email_users_headers">Post Article</h1>
<form action="" method="post" id="post_blog_entry">
<input type="text" name="title" id="title" placeholder="title... *"><br>
<textarea name="content" id="content" cols="30" rows="5" placeholder="content... *"></textarea><br>
<?php echo create_captcha(1, 20) . ' + ' . create_captcha(1, 20) . ' = '; ?>
<input type="text" name="captcha_results" size="2">
<input type="hidden" name='num1' value='<?php echo create_captcha(1, 20); ?>'>
<input type="hidden" name='num2' value='<?php echo create_captcha(1, 20); ?>'>
<input type="submit" name="publish" value="post entry" id="publish">
</form>
验证码验证:
if (empty($_POST['captcha_results']) === true) {
$errors[] = 'Please enter captcha';
} else {
if ($_POST['captcha_results'] != $_POST['num1'] + $_POST['num2']) {
$errors[] = 'Incorrect captcha';
}
}
我知道这是完全错误的,因为无论输入什么,结果总是不正确的。有人可以指出我正确的方向,因为我没有任何 PHP 经验。
最佳答案
每次运行 create_captcha(1, 20) 时,它都会生成不同的值。
在您的表单中,您调用它 4 次。
我认为你应该这样做:
$num1 = create_captcha(1, 20);
$num2 = create_captcha(1, 20);
并在您的表单中使用此值:
<textarea name="content" id="content" cols="30" rows="5" placeholder="content... *"></textarea><br>
<?php echo $num1 . ' + ' . $num2 . ' = '; ?>
<input type="text" name="captcha_results" size="2">
<input type="hidden" name='num1' value='<?php echo $num1; ?>'>
<input type="hidden" name='num2' value='<?php echo $num2; ?>'>
关于PHP简单数学验证码函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897892/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url