Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Custom Question in Registration Form

I have a laravel site that has a registration form with name/username/email fields. I want to add a required field that asks custom questions like "Which month comes after July?" etc to block most spamming.

The email field is like that.

Code
<div class="form-element has-icon margin-bottom-20">
                                        <input type="email" name="email" class="input-field {{ $errors->has('url') ? ' is-invalid' : '' }}" value="{{ old('email') }}" placeholder="email">
                                        <div class="the-icon">
                                            <i class="far fa-envelope"></i>
                                        </div>
                                        @if ($errors->has('email'))
                                            <strong class="text-danger">{{ $errors->first('name') }}</strong>
                                        @endif
                                    </div>

Can you help me create a template for custom questions?
The way I would go about doing such a thing would be like this.
- Just as a side note, I haven't tested this code, as I'm away from my main machine. If you need a little more help than this, just let me know.


So, in your Controller which renders your registration page, you'd want something like this:

PHP Code

<?php 
public function registration()
{
    
$question_date = Carbon::now()->addWeeks(rand(1, 52);
    
$answer_date   = $question_date->addMonth();

    
$captcha_question = "What month follows " . $question_date->englishMonth . "?";
    
    
Session::put('captcha_answer', $answer_date->englishMonth);

    return 
view('auth.registration', compact('captcha_question'));
} 

This gets the current date, and adds a random amount of weeks to it, from 1 to 52. This will form the question.
The answer is the date we generated, plus a month.

We construct the question you want to ask, by grabbing just the month from our random date, and store the date+1 month into a session as captcha_answer. Return the captcha question to the view so that we can display it.




To show the question and to get the answer from the user, this will need adding to the view you shared:

PHP Code

<?php 
<div class="form-group">
    <
label for="captcha">{{ $captcha_question }}</label>
    <
input type="text" name="captcha" id="captcha" class="input-field" value="" placeholder="your answer">
</
div> 



After your user submits the form, we need to check their answer to see if it matches. How you have set up your Laravel app will depend on how you will want to do this. In this example, I'm assuming you're using a FormRequest. If you are validating within the Controller, you'll need to add the captcha rule into there, alongside your other validation rules.

PHP Code

<?php 
public function rules()
    {
        return [
              
'name' => 'required',
              [...]
              
'captcha' => 'required|in:' . \Session::get('captcha_answer'),
          ];
    } 

We are telling Laravel to check that they have typed in a reply, and that the reply matches the answer we generated in step 1, and stored into the session.

Now this is validated, and the user has passed your question, you can continue as you normally would.


As I say, I haven't tested this, so apologies if there are any typos. If you need a little more help, just let me know and I'll run home and jump onto my work PC for you.
(Feb 3, 2020 05:07 AM)Jam1e Wrote:  The way I would go about doing such a thing would be like this.
- Just as a side note, I haven't tested this code, as I'm away from my main machine. If you need a little more help than this, just let me know.


So, in your Controller which renders your registration page, you'd want something like this:

PHP Code

<?php 
public function registration()
{
 
   $question_date = Carbon::now()->addWeeks(rand(1, 52);
 
   $answer_date   = $question_date->addMonth();

 
   $captcha_question = "What month follows " . $question_date->englishMonth . "?";
 
   
    Session
::put('captcha_answer', $answer_date->englishMonth);

 
   return view('auth.registration', compact('captcha_question'));
} 

This gets the current date, and adds a random amount of weeks to it, from 1 to 52. This will form the question.
The answer is the date we generated, plus a month.

We construct the question you want to ask, by grabbing just the month from our random date, and store the date+1 month into a session as captcha_answer. Return the captcha question to the view so that we can display it.




To show the question and to get the answer from the user, this will need adding to the view you shared:

PHP Code

<?php 
<div class="form-group">
 
   <label for="captcha">{{ $captcha_question }}</label>
 
   <input type="text" name="captcha" id="captcha" class="input-field" value="" placeholder="your answer">
</
div> 



After your user submits the form, we need to check their answer to see if it matches. How you have set up your Laravel app will depend on how you will want to do this. In this example, I'm assuming you're using a FormRequest. If you are validating within the Controller, you'll need to add the captcha rule into there, alongside your other validation rules.

PHP Code

<?php 
public function rules()
 
   {
 
       return [
 
             'name' => 'required',
 
             [...]
 
             'captcha' => 'required|in:' . \Session::get('captcha_answer'),
 
         ];
 
   } 

We are telling Laravel to check that they have typed in a reply, and that the reply matches the answer we generated in step 1, and stored into the session.

Now this is validated, and the user has passed your question, you can continue as you normally would.


As I say, I haven't tested this, so apologies if there are any typos. If you need a little more help, just let me know and I'll run home and jump onto my work PC for you.
Thank you for your answer. I really appreciate that you took the time to write it.

I copied the first and the third code in RegisterController and the second one in register.blade. Yet when I go to the registration page I get an error page. I also checked laravel.log and I can't see anything generated there.

I am not the developer, I bought a script from codecanyon and I am trying to improve it. Maybe I shouldn't paste in RegisterController?

Would it be easier to make all the answers predefined instead of having one new to be generated on the fly?
You should set up a test-registration site before you go about changing the code of the original.
plus this way ur site stays up (if it is)


(Feb 3, 2020 07:56 AM)James Burnham Wrote:  Thank you for your answer. I really appreciate that you took the time to write it.

I copied the first and the third code in RegisterController and the second one in register.blade. Yet when I go to the registration page I get anerror page.  I also checked laravel.log and I can't see anything generated there.

I am not the developer, I bought a script from codecanyon and I am trying to improve it. Maybe I shouldn't paste in RegisterController?

Would it be easier to make all the answers predefined instead of having one new to be generated on the fly?

Having like 3-5 predefined will always be easier than making code that will randomize questions.
[Image: tCsHIDM.gif]
(Feb 3, 2020 08:09 AM)Manks Wrote:  You should set up a test-registration site before you go about changing the code of the original.
plus this way ur site stays up (if it is)



Having like 3-5 predefined will always be easier than making code that will randomize questions.

My site is not advertised anywhere yet, there is no traffic. Do you know how I can create a question with predefined answers?
(Feb 3, 2020 08:15 AM)James Burnham Wrote:  My site is not advertised anywhere yet, there is no traffic. Do you know how I can create a question with predefined answers?

you could just change random things in this guys code; im not too educated in php so idk much

Since Jam1e is just using public functions, you should be able to go within the code of the website for registration and change how captcha works

or, since Laravel seems pretty open, there might be an add-on area to add custom code to parts of the site. You might be able to do with this code.
[Image: tCsHIDM.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
by f o r m i d a b l eJan 21, 2020 06:07 PM 1 145 Jan 21, 2020 06:07 PM
Last Post: al5ina5
by EpicToasterApr 13, 2017 12:27 PM 3 199 Apr 13, 2017 12:27 PM
Last Post: Nachfolger
by srcsameerMar 10, 2017 10:37 AM 4 232 Mar 10, 2017 10:37 AM
Last Post: srcsameer