How could I verify if google new recaptcha is checked - recaptcha

I'm now trying to use new google recaptcha in my webpage.
In previous recapthca, I usually use this script to check if user fill the recaptca challenge.
if (isset($_POST['cmdlogin'])){
$resp = recaptcha_check_answer ($privatekey,$_SERVER['REMOTE_ADDR'],strip_tags($_POST['recaptcha_challenge_field']),strip_tags($_POST['recaptcha_response_field']));
if (!$resp->is_valid) {// Bila captcha not true
echo "<script>alert('Invalid! try again!');</script>";
echo "<meta http-equiv='refresh' content='0; url=login.php'>";
exit();
}
....
Then, I found this tutorial [http://codeforgeek.com/2014/12/google-recaptcha-tutorial/#comment-22729, but it did not satisfy me.
Besides, when user reloads the page or time is out, there will be a "previous recaptcha", so how should I handle it then? Otherwise user will reload the page to avoid the previous one. thanks for any help.

html_element is the id of my empty div.. Then you can put the following in a function and call it on submit:
var googleResponse = jQuery('#g-recaptcha-response').val();
if (!googleResponse) {
$('<p style="color:red !important" class="error-captcha"><span class="glyphicon glyphicon-remove " ></span> Please fill up the captcha.</p>" ').insertAfter("#html_element");
return false;
} else {
return true;
}

UPDATED:
Ref: https://github.com/google/ReCAPTCHA/tree/master/php
I think this question is now solved. I foundthe link and it gave me full tutorial about using new google recaptcha in php script.
ISSUE:
When a user reload the page, there will be an old google recaptcha appears and we do not need to verify the old one since when user type the correct recaptcha, the "RESULT" will be sent to new recaptcha. On the other hand, the focus of the verification is not the old one but still the new one. This occurs to protect it from spam when unknown user uses other bots to attack the new one. By appearing the old one, the attack can, say, get failure.
if ($resp != null && $resp->success) {
echo "<script>alert('SUCCESS Verifying Recaptcha!');</script>";
echo "<meta http-equiv='refresh' content='0; url=login.php'>";
exit();
}

To check if google recaptcha is checked or not can be done by the following javascript condition :
<script>
if(grecaptcha && grecaptcha.getResponse().length > 0)
{
//the recaptcha is checked
// Do what you want here
alert('Well, recaptcha is checked !');
}
else
{
//The recaptcha is not cheched
//You can display an error message here
alert('Oops, you have to check the recaptcha !');
}
</script>

Related

Google ReCaptcha reload button does not work in Joomla 1.5

I'm working in an implementation of ReCaptcha from Google in my Joomla 1.5 application. I know, it's an old version of Joomla, but as the time got passed, the hard code practice at the company made the upgrading impossible.
I found an old ReCaptcha plugin for Joomla 1.5 at https://code.google.com/p/joomla-recaptcha/, by the way, recommended by developers.google (https://developers.google.com/recaptcha/old/intro).
That plugin is really simple to use. Enough to install the plugin and enable it. This way it's only a matter of use its static methods. One thing I had to do was change the path to the ReCaptcha ajax API, both in components/com_community/helpers/recaptcha.php and in plugins/system/recaptcha/recaptchalib.php. That's because the plugin reference the old path.
I'm working well with server side part of ReCaptcha. So I'm mostly concerned with a client-side issue. To display ReCaptcha snippet, is only a matter of include the line
<div><?php echo ReCaptcha::get('html'); ?></div>
in the template .php.
The ReCaptcha::get static method generates the following piece of code in my template
<div>
<div id='recaptcha_ajax_instance_'></div>
<script type='text/javascript' src='http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'></script>
<script type='text/javascript'>
(function(){
function loadRecaptcha(){
Recaptcha.create('6LcKaAgTAAAAAP3CgGF04VUsWTPGwzDs3EaVlajH','recaptcha_ajax_instance_');
}
if( window.addEvent ){
window.addEvent('domready', loadRecaptcha);
}
else{
if( window.addEventListener ){ window.addEventListener('load', loadRecaptcha); }
else if( window.attachEvent ){ window.attachEvent('onload', loadRecaptcha); }
else{ old = window.onload; window.onload = function(){ if( old && typeof old == 'function'){ old(); } loadRecaptcha(); }; }
}
})();
</script>
</div>
That issue is simply the button to reload captcha doesn't work. Neither mouse changes to pointer when hovering button, nor button change the words when clicked. The two other buttons (audio chalange and help) work well.
I wondering if the code above is outdated with the most updated API in google URL.
If somebody could help, I appreciate.

PHPMailer - Show success message after redirect

I building a very simple app with Laravel4 and so far i have managed to set up PHPMailer to work with a contact form, the users fill in their details and send me an email, normal stuff, everything works fine.
After the user sent the email successfully, he is redirected to the home page via
if($m->send()) {
header('Location: /path/to/home/');
die();
}
Now what i need is a success message that appears at the top of the homepage if the user has been redirected after a successfully sent email.
I have a div with .success class sitting on top of my home page, absolutely positioned out of view, with a negative Y value.
I tried pulling it down after on $m->send() like so:
if($m->send()) {
header('Location: /path/to/home/');
echo "<script type='text/javascript'>
$('.success').animate({
top: 0
}, 2000);
</script>";
die();
}
but it didnt work. In fact, nothing i echo after the header() has any effect.
What can I do?
Thank you guys!
This is simple HTTP - when you set the Location header, it's telling the browser to leave the page you're on and go somewhere else - anything that happens afterwards (like that little JS snippet) will never reach the browser. You need to put that snippet on the page you're redirecting to, not on this one.
I solved the problem after realizing that you can't use jquery on the document before you actually link the jquery lib in.
So, in my phpmailer config file, I set $_SESSION['success'] = true before i redirect with the header('Location: /path/to/home/'); , and then, on the Homepage, the page I wanted the success message to be displayed on, I added this bit of code (AFTER linking the jQuery library):
<?php
if(isset($_SESSION['success']) && $_SESSION['success'] == true ) {
?>
<script type='text/javascript'> $('.success').animate({top : 0}, 'normal').delay(3000).animate({top : -57}, 'normal');</script>
<?php
} else {
$_SESSION['success'] = false;
}
?>
I don't know if this is a good practice but it does work.
I also had to session_start(); on my Homepage (obviously).
Hope this helps anyone in the same situation!

client side validation not working for model window / ajax-loaded-form in yii

I am using Yii-user extension in the main layout i have a sign up link which is common to all the Cmenu
view/main layout
echo CHtml::link('Signup','#',array('id'=>'regi'));
$("#regi").click(function(){
$.ajax({
type:'GET',
url:'<?php echo Yii::app()->request->baseUrl;?>/index.php/user/registration',
success:function(res){
$("#dispdata").show();
$("#dispdata").html(res);
}
});
});
<div id="dispdata"><div>
**yii user extension **renders this perfectly and even submit its correctly if form values a re valid.
but if the values are incorrect and blank it redirect to url .../user/registration
which is not what my need .I need guidance what do i do such that if the values are incorrect or blank it should not redirect and display the errors in model window.
I did tried but hardly could get the satisfied results
if i place the following the model window itself doesnt appear what do i do
module registrationController i placed
....//some code here (**in yiiuser register controller**)
if ($model->save()) {
echo CJSON::encode(array(
'status'=>'success',
));
}
....//some code here...
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderPartial('registration',array('model'=>$model,),false,true);
in module view registration
<?php echo CHtml::ajaxSubmitButton(Yii::t('registration'),CHtml::normalizeUrl(array('user/registration','render'=>false)),array('dataType'=>'json',
'success'=>'function(data) {
if(data != null && data.status == "success") {
$("#registration-form").append(data.data);
}
}')); ?>
can anyone please guide me am working past 10 ten days tried every hook or crook method but could not obtain the results......how can the model window with client side validation be done appear..... Please guide me or let me know something better can be done
rules in registration model
if (!(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')) {
array_push($rules,array('verifyCode', 'captcha', 'allowEmpty'=>!UserModule::doCaptcha('registration')));
as well was not with attributes for reqired field
have changed to
array_push($rules,array('verifyCode', 'captcha','message' => UserModule::t("captcha cannot be blank.")));
and added the verifycode to required field
yet not working,
The simple way is using render method in your Ajax action and creating empty layout for this action. If you do so, validation scripts will be included in the server response. Also you need to exclude jquery.js and other script with Yii::app()->clientScript->scriptMap and include them in main layout always.

You are not authorised to view this resource - Joomla

I am using joomla 2.5.9 version, and I would like Joomla to redirect me to the login page if I am not logged in when i click an article which the Permission Access is for Registered only, but instead Joomla returns me this message: You are not authorised to view this resource.
And I dont see any reason why joomla by default havent made it redirect to login page.
Thanks
This doesn't answer your exact question, but I think it's a good workaround. I'm working on the same issue. My approach at the moment is to check the messages for the "not authorised" string, and then set a flag based on that. You can then check that flag anywhere in template and either redirect, or just choose to optionally show the login form.`
/* get message from app */
$app = JFactory::getApplication();
$messages = $app->getMessageQueue();
/* set login flag to 0 */
$showlogin = 0;
/* if there is a message set... */
if (isset($messages[0])) {
/* loop through messages and check for the "not authorised" string */
foreach ($messages as $msg) {
if ($msg["type"] == "error" && strpos($msg["message"], "not authorised") ) {
/* if found, update login flag */
$showlogin = 1;
}
}
}
/* include in template body - you could redirect here instead of including login form */
if ($showlogin) { ?>
<jdoc:include type="modules" name="login-form" style="none" />
<?php } ?>
`
this happens when you try to access an article which is not visible, but the category is publically visible.
Seems like it is not considered a bug, but I think its a pretty unexpected "feature".
To fix this you can edit:
joomla/components/com_content/views/article/view.html.php
// Check the view access to the article (the model has already computed the values).
if ($item->params->get('access-view') == false && ($item->params->get('show_noauth', '0') == '0'))
{
$app->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR'), 'error');
$uri = urlencode(base64_encode(JURI::getInstance()->toString()));
JFactory::getApplication()->redirect(
JRoute::_('index.php?option=com_users&view=login&return='. $uri, false)
);
return;
}
This will show the login screen and return to the article after a succesfull login.
If you dont want to edit the core file (because you want to update your system), you have to create a system plugin to override this.

How to make sure a file is selected before uploading

Hi I am using codeigniter for file uploading and I am using this code
echo form_open_multipart('controller_a');
echo form_upload('userfile');
echo form_submit('Upload','upload');
echo form_close();
I store the pointer to the uploaded file in the database,
My question is how do I make sure that the user has selected a file before clicking on upload button because as of now the code submits even if the user clicks directly on upload without selecting a file
Along with client side verification, you should use server side verification, too. Currently, CodeIgniter does not provide a function, so one can use native PHP function is_uploaded_file:
if (is_uploaded_file($_FILES['myfile']['tmp_name']))
{
$this->load->library('upload');
$this->upload->do_upload('myfile');
}
You can't, not in CodeIgniter at least. You'll need to have JS overwrite the onsubmit property of the form and then test the userfile input's value.
use JS
very basic code, but it works.
<script type="text/javascript">
<!--
function validate_form ( )
{
valid = true;
if ( document.upload_form.something.value == "" )
{
alert ( "Please select a file before clicking upload ! " );
valid = false;
}
return valid;
}
//-->
</script>
and use onsubmit even in the form
onSubmit="return validate_form ( );"
when a user click on upload button without selecting any file, it will alert the user .
Your best bet is to use a jQuery plugin like the following:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
This will allow you to select what input values will need to be selected, and customize a message to inform the user what field(s) they are missing.

Resources