Link to home
Start Free TrialLog in
Avatar of Cosmic_Cake
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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

If CAPTCHA is the issue, this article may help.
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
Avatar of Cosmic_Cake
Cosmic_Cake

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.
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
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/destination.php" method="post">

or  subfolders

<form action="subfolder/destination.php" method="post">

Patricia / pslh
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_uCAsuTpT9LY-RICSj'; // enter your API keys here
$privatekey = '6Lfn18MSAAAAAMyD1u1ayJoBAhxFIEj1-mywB1_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_post_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/recaptchalib.php');
}

function addAnonContent(){
      global $wp_query;
      global $captchaEnabled;
      global $publickey, $enable_css, $display_content_after_post_created;
      
      $form = "<br>";
      
      $this_post_id = $wp_query->post->ID;

      if(get_post_meta($this_post_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($publickey);
                        $form .= '</center>';
                  }
                  $form .= displayFormEnd();
            }
      }
      else{
            return get_the_content();
      }

      if($display_content_after_post_created){
            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($privatekey, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'])->is_valid) {
            //check if user exists, if not, add this user
             if(username_exists($anonUserName)){
                   $user_id = get_profile( 'ID', $anonUserName );
             }
             else{
                  $user_id = wp_create_user($anonUserName, 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['Title'], $_POST['Message']);
            $to_return .= recaptcha_get_html($publickey);
            $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}</textarea><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');
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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."
" 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.
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
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-urlencoded;\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;
}
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 "
Very practical answers, thank you.