I have a page with a recaptcha in it, and it had been running without any problem for two months. But now, since a few days, it has been acting weird. I have tried many several times, but the captcha is simply not working, the verification part.
Here is the code
$captcharesponse = test_input($_POST["g-recaptcha-response"]);
$status = captcha($captcharesponse);
...
function captcha($t){
$captcharesponse = $t;
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($curl, CURLOPT_POSTFIELDS, 'secret=7...PRIVATE_KEY...S&response=' . $captcharesponse);
$result = json_decode(curl_exec($curl), true);
curl_close($curl);
if($result['success'] == false){
error_log(date("Y-M-d, D h:i:s A") . " : Result = " . $result['success'] . ", and error = " . $result['error-codes']);
}
return $result['success'];
}
And no matter what, even if I am not even entering the captcha, still the page is taking too long, and hence nothing is working. Please not that other things are simply skipped if the captcha is wrong, so there is no way that other things are causing the delay.
Thanks in advance
PS. I am not using any kind or library or anything, and it did use to work some time back without any problem.
The 'test_input()' code:
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
The problem has been resolved,
Apparently it was a problem on reCAPTCHA's end. The above provided code is now working flawlessly, and all slow-performance problems have been resolved as well.
Thank you all.
PS. Others can use this code if the want.
I'd say to use the recaptcha library, available at:
https://github.com/google/recaptcha
First of all, download the files, most important is recaptchalib.php (you can download all files clicking the download zip button at right).
Then unzip it to your folder and use it like the example unzipped along (example-recaptcha.php):
<?php
require_once "recaptchalib.php";
// Register API keys at https://www.google.com/recaptcha/admin
$siteKey = "YOURSITEKEY";
$secret = "YOURSECRET";
$lang = "en";
$resp = null; // The response from reCAPTCHA
$error = null; // The error code from reCAPTCHA, if any
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) { // Was there a reCAPTCHA response?
$resp = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
?>
<html>
<head><title>reCAPTCHA Example</title></head>
<body>
<?php
if ($resp != null && $resp->success) {
echo "You got it!";
}
?>
<form action="" method="post">
<div class="g-recaptcha" data-sitekey="<?php echo $siteKey;?>"></div>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang;?>">
</script>
<br/>
<input type="submit" value="test recaptcha" />
</form>
</body>
</html>
Related
When I open update.php on its own (with self supplied test vars), it sends the SSE to testsse.php just fine and there are no issues (Everything I need to be printed is showing up in inspect element), However, I am trying to have POST data from another page (In this case mootssetest.php) get received by update.php so it may send out the SSE containing the data. I am not sure what I am doing wrong, but this test rig is not working. Guidance would be appreciated.
testsse.php (front end page meant to receive SSE and print)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Using Server-Sent Events</title>
<script>
window.onload = function() {
var link = new EventSource("update.php");
var antispam;
var inputthing = event.data;
var splitted;
link.onmessage = function(event) {
inputthing = event.data;
splitted = inputthing.split(" ");
if (splitted[0] != antispam && splitted[1] == <?php echo $page; ?>) {
document.getElementById("livemsg").innerHTML += "<div id=\"post-" + splitted[0] + "\" class=\"reply\">" + "</div>";
antispam = splitted[0];
};
};
};
</script>
</head>
<body>
<div id="livemsg">
<!--Server response will be inserted here-->
</div>
</body>
</html>
update.php (SSE sender, post receiver)
<?php
$data = json_decode(file_get_contents('php://input'), true);
$postnum = $data[0];
$bread = $data[1];
postnum = 32;
bread = 4;
function liveupdate($postnum, $bread)
{
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
echo "data: " . $postnum . " " . $bread . "\n\n";
flush();
}
liveupdate($postnum, $bread);
?>
mootssetest.php (POST sender)
function httppost($postnum, $bread)
{
$url = "http://localhost/update.php";
$data = array($postnum, $bread);
$curl = curl_init($url);
$jsondata = json_encode($data);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $jsondata );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
httppost(420, 4);
?>
(For context, I am trying to have this print a new post in some forum software every time a function is called without refreshing the page for the user)
you haven't included the event in your window.onload() function, please fix it first and try again.
I'm unable to scrape data from few websites using curls.
I'm using CURL to scrape website from url's. It works great in 80% of the urls I use. But some url's don't seem "scrapeable". For example, when I try to scrape https://www.nextdoorhub.com/ and https://www.atknsn.com/, it doesn't work. the website keeps showing blanks and at the end it doesn't return a result.
This is my code:
<center>
<br/>
<form method="post" name="scrap_form" id="scrap_form" action="scrape_data.php">
<b>Enter Website URL To Scrape Data:</b>
<input type="input" name="website_url" id="website_url">
<input type="submit" name="submit" value="Submit" >
</form>
</center>
<?php
error_reporting(E_ALL ^ E_NOTICE );
$website_url = $_POST['website_url'];
$result = scrapeWebsiteData($website_url);
function scrapeWebsiteData($website_url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $website_url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_BINARYTRANSFER,1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$regextit = '<div id="case_textlist">(.*?)<\/div>/s';
preg_match_all($regextit, $result, $list);
/* echo "<pre>";
print_r($list[1]); die; */
$regex = '/[\'" >\t^]([^\'" \n\r\t]+\.(jpe?g|bmp|gif|png))[\'" <\n\r\t]/i';
preg_match_all($regex, $result, $url_matches);
$count = count($url_matches[1]);
// set the local path of image
$local_path = 'C:\udeytech\htdocs\tests\images\\';
for($i=0; $i<$count; $i++)
{
preg_match_all('!.*?/!', $url_matches[1][$i], $matches);
$last_part = end($matches[0]);
////match image name last part of anything .jpg|jpeg|gif|png
preg_match("!$last_part(.*?.(jpg|jpeg|gif|png))!", $url_matches[1][$i], $matche);
$secons_part = $matche[0];
$info = pathinfo($secons_part);
$image_name = $info['basename'];
//save image url in a variable
$image_url = $url_matches[1][$i];
$image_path = scrapeWebsiteData($image_url);
$file_open = fopen($local_path.$image_name, 'w');
fwrite($file_open, $image_path);
fclose($file_open);
}
?>
Have you tried to load either of these sites in your browser and look at the responses?
nextdoorhub is using angular and atknsn looks to be heavy on jQuery. Long story short, these sites need to run javascript to render the full HTML you're intending to scrape.
Using PHP + cURL alone won't cut it. Look at threads that discuss scraping angular and that will point you in the right direction. (Hint: you need to scrape these sites with node.js)
The code is working fine but i am not able to insert the user data in the mysql database.
<?php
$facebookAppAuthUrl = 'https://graph.facebook.com/oauth/access_token';
$facebookGraphUrl = 'https://graph.facebook.com';
$facebookClientId = ''; // Put your App Id here.
$facebookRedirectUrl = ''; // Redirect url same as passed before.
$facebookAppSecret = ""; // Put your App Secret here.
$code = $_GET['code'];
$url =$facebookAppAuthUrl."?client_id=".$facebookClientId
."&redirect_uri=".$facebookRedirectUrl
."&client_secret=".$facebookAppSecret
."&code=".$code;
$output = urlResponse($url);
$var = strtok($output, "&");
$ApCode = strtok($var, "=");
$ApCode = strtok("=");
// This $ApCode will be used as a token to get user info from facebook.
$url = $facebookGraphUrl.'/me';
echo '<pre>';
$resposeObj = json_decode(processUrl($url,$ApCode));
var_dump($resposeObj);
echo '<pre>';
function urlResponse($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function processUrl($url,$apCode){
if(stripos($url,'?')>0)
$url = $url.'&access_token='.$apCode;
else
$url = $url.'?access_token='.$apCode;
return urlResponse($url);
}
?>
I guess the code below is wrong. I got the user data from facebook in JSON format but unfortunately I am not able to add user data in mysql using the PHP. How could we insert the json format data in mysql using php?
<?php
require('../conn.php');
$name = $url['id']['name'];
$first_name = $url['id']['first_name'];
$last_name = $url['id']['last_name'];
$hometown = $url['id']['hometown'];
{
$sql="insert into user values('','$name','$first_name','$last_name','$hometown')";
mysql_query($sql);
}
?>
<script type="text/javascript">window.location="../index.php"</script>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
Here what you need to do , first the JSON returned data needs to be converted to array as
$response = json_decode($response,true);
Now with this data you have the array and you can use print_r($response) and see how the array looks like and use the data in the query.
Hope this helps
So, apparently I can't comment without 50 rep points - so whatever. I suspect your insert statement is off. Why do you have an empty string at the beginning? I hope that's not your primary key field. I would specify my fields if I were you and leave the auto-inc field out of it so it can auto increment :)
$sql="insert into user (`name`,`firstName`,`LastName`,`homeTown`) values('$name','$first_name','$last_name','$hometown')";
I'm creating an system that uses online compiler. IDEONE give me this feature (throgh an Web Service), but with a price for an high volume of compilations.
Then I'm trying to use codepad, but it doesn't have an Web Service... codepad has an initial page, and clicking it's submit button, apparently the same page loads (the form's action is "/")...
I'm using curl to load page, but I'm getting "Internal Server Error". This is my code: pastebin Code, I'm using 000webhost, I don't know if i did something wrong or if my webserver doesn't support it.
Have you trued removing the TIMEOUT? or maybe extending it?
Try this maybe:
<html>
<div align="center">
<form action="compilador.php" method="POST">
<textarea id="source" name="source"></textarea>
<input type="submit" value="Enviar" />
<?php
if(isset($_POST['source']) && $_POST['source'] != "")
{
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL, "http://codepad.org");
/**
* Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post_data=$_POST;
$post_data['lang'] = 'C';
$post_data['private'] = True;
$post_data['run'] = True;
foreach($post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec ($ch);
/**
* Close cURL session
*/
curl_close($ch);
echo "<br /><br />RESULT: {".$result."}";
}
?>
</form>
</div>
</html>
Hey all I have seen several questions on the topic here, but none of them have solved my problem. I have a script on my site which I want to use to generate several different types of emails to my users. I wanted a way to be able to create template files for the different emails which accept $_POST variables to fill in relevant information, and to simply make a post request to these templates and get back the response to place as the body of the email. I am attempting to write a function which would accept the location of the template file (either relative or absolute would work, but I would prefer relative honestly), and an array of parameters that I would like to send to the template via post. So far I have had no luck. Here is my code so far:
private function post_request($url, $data) {
$output = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
$output['status'] = "ok";
$output['content'] = $result;
} else {
$output['status'] = "failure";
$output['error'] = curl_error($ch);
}
curl_close($ch);
return $output;
}
I have been getting the error "couldn't connect to host" from curl, but after outputting my url to an error log I have been able to verify that copying and pasting the URL into firefox results in seeing the page correctly.
Any ideas? I am not married to the idea of using curl, so if there is a better option I would be more than happy to use it instead. Thanks for the help all!
You should be able to use file_get_contents() for this, so long as your host has not prevented it from accessing remote locations (and the $url script is not looking exclusively for POST data).
private function post_request($url, $data) {
$output = array();
$url_with_data = '';
foreach ( $data as $k=>$v ){ // Loop through data and create request string
$url_with_data .= '&' . $k . '=' . $v;
}
// Remove first ampersand and encode the data
$url_with_data = urlencode( substr( $url_with_data, 1 ) );
// Request file
// Format will be http://url.com?var1=data&var2=data&var3=data
$result = file_get_contents( $url . '?' . $url_with_data );
if ($result) {
$output['status'] = "ok";
$output['content'] = $result;
} else {
$output['status'] = "failure";
$output['error'] = 'Could not open remote file';
}
return $output;
}
Another option: You say that both files reside on the same server. If that is the case, you could simply require() the template builder.
private function post_request($url, $data) {
$output = array();
#require_once('./path/to/template_builder.php');
if ($result) {
$output['status'] = "ok";
$output['content'] = $result;
} else {
$output['status'] = "failure";
$output['error'] = 'Could not open remote file';
}
return $output;
}
Then in template_builder.php:
<?php
unset( $result );
if ( is_array( $data ) ){
// Parse $data ...
$result = $email_template;
}
As it turns out, the issue ended up being a server configuration error. The server was timing out while attempting to contact the file because it was hitting the wrong DNS server. Fixing that solved my problem!