Cosmic_Cake
asked on
Could not open socket error
If a user tries to submit a post on my website they encounter the error message 'Could not open socket'
The user is requested to enter in a CAPTCHA, I'm not sure if that's related.
The user is requested to enter in a CAPTCHA, I'm not sure if that's related.
ASKER
Hi Ray,
Here is the URL http://www.overheardindublin.com/post-a-story/
If you fill out this form it will reproduce the error message. The error message doesn't reveal much, just 'Could not open socket error'
I think I will have to post some php files on here.
Here is the URL http://www.overheardindublin.com/post-a-story/
If you fill out this form it will reproduce the error message. The error message doesn't reveal much, just 'Could not open socket error'
I think I will have to post some php files on here.
Hi Cosmic_Cake,
Your form is in http://www.overheardindublin.com/post-a-story/
Please check the file is right path and file in form action of your script
<form action="/post-a-story/" method="post">
Patricia / pslh
Your form is in http://www.overheardindublin.com/post-a-story/
Please check the file is right path and file in form action of your script
<form action="/post-a-story/" method="post">
Patricia / pslh
ASKER
I cannot find a 'post-a-story' page. I didn't develop the site. I'll keep looking.
Hi Cosmic_Cake
Now I inspect the element of your web page,
It is stated in this line
<form action="/post-a-story/" method="post">
"/post-a-story/" it is not specify any file to post to.
So you get the error.
your web page is inside folder post-a-story/
if the destination file eg. destination.php is in the same folder, you just type
<form action="destination.php" method="post">
Otherwise, if it is in other folder
<form action="../otherfolder/des tination.p hp" method="post">
or subfolders
<form action="subfolder/destinat ion.php" method="post">
Patricia / pslh
Now I inspect the element of your web page,
It is stated in this line
<form action="/post-a-story/" method="post">
"/post-a-story/" it is not specify any file to post to.
So you get the error.
your web page is inside folder post-a-story/
if the destination file eg. destination.php is in the same folder, you just type
<form action="destination.php" method="post">
Otherwise, if it is in other folder
<form action="../otherfolder/des
or subfolders
<form action="subfolder/destinat
Patricia / pslh
ASKER
I think I located the file where the problem could be. See Below:
<?php
/*
Plugin Name: Anon Post
Plugin URI: http://binary10.info/blog/lang/en/2009/10/anonymous-posting-wordpress-plugin
Description: This plugin allows anonymous users to write their own posts using special page. No access to wordpress panel is needed.
Version: 1.3
Author: Wojciech Langiewicz
Author URI: http://binary10.info
*/
/* Copyright 2009 Wojciech Langiewicz (email : wlang(dont spam me)iewicz@gmail.com)
* This script is relased under BSD license.
*/
require_once ( ABSPATH . WPINC . '/registration.php' );
//------------------------ - start of options section -------------------------- -
/*
* You can change this options to suit you needs.
*
* If you want to use reCAPTCHA you need to get API key,
* for more information see: http://recaptcha.net/whyrecaptcha.html
* then click: "Sign up Now!" or log into your account
*/
$anonUserName = 'Anonymous'; // 'name' of anonymous user, don't have to exist
$category = array(1); //this array should contain IDs for categories you want anonymous post to be in, example: array(1,2,4). NOTE: create those categories first, and check their numbers
$captchaEnabled = true; // enable reCAPTCHA (set to: true/false), do not use ' or "
$publickey = '6Lfn18MSAAAAAIaXx1isTq_uC AsuTpT9LY- RICSj'; // enter your API keys here
$privatekey = '6Lfn18MSAAAAAMyD1u1ayJoBA hxFIEj1-my wB1_4';
$enableComments = true; // true if you want to enable comments under posts created with plugin
$my_post_type = 'draft'; // avaiable options: 'publish' for posts to be published directly
// 'draft' if you want to validate posts (by admin for example), post will appear as draft
$enable_css = false; // set to true/false if you want css or not (css can be edited at the end of this file)
$display_content_after_pos t_created = true; // set to true/false if you want to show content of page displayed above the form after post is sucessfully created on confirmation page
$use_anonComment = false; // set to true/false if you want to use Anonymous Comment plugin to allow users to comment posts creted with this plugin to be anonymously commented, remember about installing this plugin first
// ---------- end of options, DO NOT CHANGE CODE BELOW THIS LINE ------------
if($captchaEnabled){
require_once('recaptcha/re captchalib .php');
}
function addAnonContent(){
global $wp_query;
global $captchaEnabled;
global $publickey, $enable_css, $display_content_after_pos t_created;
$form = "<br>";
$this_post_id = $wp_query->post->ID;
if(get_post_meta($this_pos t_id, 'anonPost', true) == 'true'){
//add css:
if($enable_css)
$form .= add_css();
if($_POST){
$form .= enteredByPOST();
}
else{
$form .= displayFormStart();
if($captchaEnabled){
$form .= '<center>';
$form .= recaptcha_get_html($public key);
$form .= '</center>';
}
$form .= displayFormEnd();
}
}
else{
return get_the_content();
}
if($display_content_after_ post_creat ed){
return get_the_content().$form;
}
else{
return $form;
}
//this should not happen
//return get_the_content();
}
function enteredByPOST(){
global $captchaEnabled;
global $privatekey, $publickey;
global $anonUserName, $category, $enableComments, $my_post_type;
$to_return = '';
if ((!$captchaEnabled) || recaptcha_check_answer($pr ivatekey, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challeng e_field'], $_POST['recaptcha_response _field'])- >is_valid) {
//check if user exists, if not, add this user
if(username_exists($anonUs erName)){
$user_id = get_profile( 'ID', $anonUserName );
}
else{
$user_id = wp_create_user($anonUserNa me, wp_generate_password(20, false), 'no@email.com' );
}
$my_post = array(
'post_title' => $_POST['Title'],
'post_content' => $_POST['Message'],
'post_status' => $my_post_type,
'post_author' => $user_id,
'post_category' => $category,
//this thing enables normal comments only when comments are open and using Anon Comment is turned off
'comment_status' => (($enableComments && !$use_anonComment) ? 'open' : 'closed'),
'ping_status' => 'closed'
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
if($post_id === 0){
$to_return .= 'error adding post';
}
else{
//add meta tag, so AnonComment could add its comment field
if($use_anonComment){
add_post_meta($post_id, 'AnonComment', 'true', true);
}
//display new post link
//mod_rewrite will work with normal url's, like ?p=123
$urlArray = explode('?', $_SERVER['REQUEST_URI']);
$url = $urlArray[0];
$to_return .= 'post sucessfully added to database, post page: <a href='.$url.'?p='.$post_id .'>is here</a>';
}
}
else{
//error validating captcha
$error = $resp->error;
$to_return .= $error;
$to_return .= 'reCAPTCHA validation failed, try again';
$to_return .= displayFormStart($_POST['T itle'], $_POST['Message']);
$to_return .= recaptcha_get_html($public key);
$to_return .= displayFormEnd();
}
return $to_return;
}
function displayFormStart($title = '', $message = ''){
$form =<<< FORMSTART
<form action="{$_SERVER['REQUEST _URI']}" method="post">
Post title:<br><input type="text" name="Title" value="{$title}" size="40" maxlength="200"><br>
Your message:<br><textarea name="Message" cols=50 rows=20>{$message}</textar ea><br>
FORMSTART;
return $form;
}
function displayFormEnd(){
$form_end =<<< FORMEND
<input type="submit" name="submit" value="Submit">
</form>
<br>
FORMEND;
return $form_end;
}
function add_css(){
$css =<<< CSS
<style type="text/css">
form {
align: center;
}
textarea {
background-color: white;
color: black;
align: center;
}
</style>
CSS;
return $css;
}
add_action('the_content', 'addAnonContent');
<?php
/*
Plugin Name: Anon Post
Plugin URI: http://binary10.info/blog/lang/en/2009/10/anonymous-posting-wordpress-plugin
Description: This plugin allows anonymous users to write their own posts using special page. No access to wordpress panel is needed.
Version: 1.3
Author: Wojciech Langiewicz
Author URI: http://binary10.info
*/
/* Copyright 2009 Wojciech Langiewicz (email : wlang(dont spam me)iewicz@gmail.com)
* This script is relased under BSD license.
*/
require_once ( ABSPATH . WPINC . '/registration.php' );
//------------------------
/*
* You can change this options to suit you needs.
*
* If you want to use reCAPTCHA you need to get API key,
* for more information see: http://recaptcha.net/whyrecaptcha.html
* then click: "Sign up Now!" or log into your account
*/
$anonUserName = 'Anonymous'; // 'name' of anonymous user, don't have to exist
$category = array(1); //this array should contain IDs for categories you want anonymous post to be in, example: array(1,2,4). NOTE: create those categories first, and check their numbers
$captchaEnabled = true; // enable reCAPTCHA (set to: true/false), do not use ' or "
$publickey = '6Lfn18MSAAAAAIaXx1isTq_uC
$privatekey = '6Lfn18MSAAAAAMyD1u1ayJoBA
$enableComments = true; // true if you want to enable comments under posts created with plugin
$my_post_type = 'draft'; // avaiable options: 'publish' for posts to be published directly
// 'draft' if you want to validate posts (by admin for example), post will appear as draft
$enable_css = false; // set to true/false if you want css or not (css can be edited at the end of this file)
$display_content_after_pos
$use_anonComment = false; // set to true/false if you want to use Anonymous Comment plugin to allow users to comment posts creted with this plugin to be anonymously commented, remember about installing this plugin first
// ---------- end of options, DO NOT CHANGE CODE BELOW THIS LINE ------------
if($captchaEnabled){
require_once('recaptcha/re
}
function addAnonContent(){
global $wp_query;
global $captchaEnabled;
global $publickey, $enable_css, $display_content_after_pos
$form = "<br>";
$this_post_id = $wp_query->post->ID;
if(get_post_meta($this_pos
//add css:
if($enable_css)
$form .= add_css();
if($_POST){
$form .= enteredByPOST();
}
else{
$form .= displayFormStart();
if($captchaEnabled){
$form .= '<center>';
$form .= recaptcha_get_html($public
$form .= '</center>';
}
$form .= displayFormEnd();
}
}
else{
return get_the_content();
}
if($display_content_after_
return get_the_content().$form;
}
else{
return $form;
}
//this should not happen
//return get_the_content();
}
function enteredByPOST(){
global $captchaEnabled;
global $privatekey, $publickey;
global $anonUserName, $category, $enableComments, $my_post_type;
$to_return = '';
if ((!$captchaEnabled) || recaptcha_check_answer($pr
//check if user exists, if not, add this user
if(username_exists($anonUs
$user_id = get_profile( 'ID', $anonUserName );
}
else{
$user_id = wp_create_user($anonUserNa
}
$my_post = array(
'post_title' => $_POST['Title'],
'post_content' => $_POST['Message'],
'post_status' => $my_post_type,
'post_author' => $user_id,
'post_category' => $category,
//this thing enables normal comments only when comments are open and using Anon Comment is turned off
'comment_status' => (($enableComments && !$use_anonComment) ? 'open' : 'closed'),
'ping_status' => 'closed'
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
if($post_id === 0){
$to_return .= 'error adding post';
}
else{
//add meta tag, so AnonComment could add its comment field
if($use_anonComment){
add_post_meta($post_id, 'AnonComment', 'true', true);
}
//display new post link
//mod_rewrite will work with normal url's, like ?p=123
$urlArray = explode('?', $_SERVER['REQUEST_URI']);
$url = $urlArray[0];
$to_return .= 'post sucessfully added to database, post page: <a href='.$url.'?p='.$post_id
}
}
else{
//error validating captcha
$error = $resp->error;
$to_return .= $error;
$to_return .= 'reCAPTCHA validation failed, try again';
$to_return .= displayFormStart($_POST['T
$to_return .= recaptcha_get_html($public
$to_return .= displayFormEnd();
}
return $to_return;
}
function displayFormStart($title = '', $message = ''){
$form =<<< FORMSTART
<form action="{$_SERVER['REQUEST
Post title:<br><input type="text" name="Title" value="{$title}" size="40" maxlength="200"><br>
Your message:<br><textarea name="Message" cols=50 rows=20>{$message}</textar
FORMSTART;
return $form;
}
function displayFormEnd(){
$form_end =<<< FORMEND
<input type="submit" name="submit" value="Submit">
</form>
<br>
FORMEND;
return $form_end;
}
function add_css(){
$css =<<< CSS
<style type="text/css">
form {
align: center;
}
textarea {
background-color: white;
color: black;
align: center;
}
</style>
CSS;
return $css;
}
add_action('the_content', 'addAnonContent');
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ahh - just saw your post, I'll take a look at that now.
Here is an additional symptom to consider while you're looking for the string "Could not open socket." If you do not put anything into the reCaptcha box, the site says that Captcha validation failed. If you put something in, right or wrong, the site says "Could not open socket."
ASKER
" I am slightly suspicious of the reCaptcha implementation" - I agree
I think it also slows down user input.
Thanks for your suggestion, I will use Textpad and do a scan. Let's see if anything comes up.
I think it also slows down user input.
Thanks for your suggestion, I will use Textpad and do a scan. Let's see if anything comes up.
10-4. Post back here with the script that contains the culprit. And if you want something that is effective but more client-friendly than reCaptcha see this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_9849-Making-CAPTCHA-Friendlier-with-PHP-Image-Manipulation.html
Best, ~Ray
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_9849-Making-CAPTCHA-Friendlier-with-PHP-Image-Manipulation.html
Best, ~Ray
ASKER
I found it. It's in a files called recaptchalib.php.
Below is a segment. For the moment I would like to remove the captcha option on the website as all posts are monitored and hen put live, so no damage would be done.
/**
* Submits an HTTP POST to a reCAPTCHA server
* @param string $host
* @param string $path
* @param array $data
* @param int port
* @return array response
*/
function _recaptcha_http_post($host , $path, $data, $port = 80) {
$req = _recaptcha_qsencode ($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-url encoded;\r \n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
$response = '';
if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
die ('Could not open socket');
}
fwrite($fs, $http_request);
while ( !feof($fs) )
$response .= fgets($fs, 1160); // One TCP-IP packet
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response;
}
Below is a segment. For the moment I would like to remove the captcha option on the website as all posts are monitored and hen put live, so no damage would be done.
/**
* Submits an HTTP POST to a reCAPTCHA server
* @param string $host
* @param string $path
* @param array $data
* @param int port
* @return array response
*/
function _recaptcha_http_post($host
$req = _recaptcha_qsencode ($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-url
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
$response = '';
if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
die ('Could not open socket');
}
fwrite($fs, $http_request);
while ( !feof($fs) )
$response .= fgets($fs, 1160); // One TCP-IP packet
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response;
}
ASKER
I finally got rid of the horrible CAPTCHA and the posting is working now.
I changed true to false
$captchaEnabled = false; // enable reCAPTCHA (set to: true/false), do not use ' or "
I changed true to false
$captchaEnabled = false; // enable reCAPTCHA (set to: true/false), do not use ' or "
ASKER
Very practical answers, thank you.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_9849-Making-CAPTCHA-Friendlier-with-PHP-Image-Manipulation.html
To have any hope of understanding the issue we would need to see the complete error messages, and we may need to see the code, too. Please post the URL that shows the failure and give us instructions on how to reproduce the failure.
Thanks, ~Ray