Solved

Could not open socket error

Posted on 2012-04-07
14
1,903 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:Cosmic_Cake
  • 7
  • 5
  • 2
14 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37819215
If CAPTCHA is the issue, this article may help.
http://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
0
 

Author Comment

by:Cosmic_Cake
ID: 37819236
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.
0
 
LVL 12

Expert Comment

by:Patricia Siu-Lai Ho
ID: 37819347
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
0
 

Author Comment

by:Cosmic_Cake
ID: 37819415
I cannot find a 'post-a-story' page. I didn't develop the site. I'll keep looking.
0
 
LVL 12

Expert Comment

by:Patricia Siu-Lai Ho
ID: 37819425
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
0
 

Author Comment

by:Cosmic_Cake
ID: 37819497
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');
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37819508
Here is the response page.  It looks like the programmer put in something like die('Could not open socket');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>
Overheard in Dublin -   Post a story</title>


<link rel="stylesheet" href="http://www.overheardindublin.com/wp-content/themes/oceanmist/style.css?10" type="text/css" media="screen" />


<style type='text/css' media='screen'>body {
	font-family: Verdana, Arial, Times New Roman, sans-serif;
	font-size: 75%;
}

h1, h2, h3, h4, h5, h6 {
   font-family: Lucida Grande, Verdana, Tahoma, Trebuchet MS!important;
}

#rotateimg { height: 200px!important; }</style>


<!--[if !IE]>deleted this if you want the indexing for archives<![endif]-->


<link rel="alternate" type="application/rss+xml" title="Overheard in Dublin RSS Feed" href="http://www.overheardindublin.com/feed/" />
<link rel="pingback" href="http://www.overheardindublin.com/xmlrpc.php" />

<link rel="alternate" type="application/rss+xml" title="Overheard in Dublin &raquo; Post a story Comments Feed" href="http://www.overheardindublin.com/post-a-story/feed/" />
<link rel='stylesheet' id='A2A_SHARE_SAVE-css'  href='http://www.overheardindublin.com/wp-content/plugins/add-to-any/addtoany.min.css?ver=1.3' type='text/css' media='all' />
<link rel='stylesheet' id='wdfb_style-css'  href='http://www.overheardindublin.com/wp-content/plugins/facebook/css/wdfb.css?ver=3.2.1' type='text/css' media='all' />
<script type='text/javascript' src='http://www.overheardindublin.com/wp-includes/js/l10n.js?ver=20101110'></script>
<script type='text/javascript' src='http://www.overheardindublin.com/wp-includes/js/jquery/jquery.js?ver=1.6.1'></script>
<script type='text/javascript' src='http://www.overheardindublin.com/wp-includes/js/comment-reply.js?ver=20090102'></script>
<script type='text/javascript' src='http://connect.facebook.net/en_US/all.js?ver=3.2.1'></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.overheardindublin.com/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://www.overheardindublin.com/wp-includes/wlwmanifest.xml" /> 
<link rel='index' title='Overheard in Dublin' href='http://www.overheardindublin.com/' />
<link rel='canonical' href='http://www.overheardindublin.com/post-a-story/' />

<script type="text/javascript"><!--
var a2a_config=a2a_config||{},wpa2a={done:false,html_done:false,script_ready:false,script_load:function(){var a=document.createElement('script'),s=document.getElementsByTagName('script')[0];a.type='text/javascript';a.async=true;a.src='http://www.overheardindublin.com/wp-content/uploads/addtoany/page.js';s.parentNode.insertBefore(a,s);wpa2a.script_load=function(){};},script_onready:function(){if(a2a.type=='page'){wpa2a.script_ready=true;if(wpa2a.html_done)wpa2a.init();}},init:function(){for(var i=0,el,target,targets=wpa2a.targets,length=targets.length;i<length;i++){el=document.getElementById('wpa2a_'+(i+1));target=targets[i];a2a_config.linkname=target.title;a2a_config.linkurl=target.url;if(el)a2a.init('page',{target:el});wpa2a.done=true;}}};a2a_config.tracking_callback=['ready',wpa2a.script_onready];
a2a_config.static_server="http://www.overheardindublin.com/wp-content/uploads/addtoany";
a2a_config.onclick=1;
a2a_config.show_title=1;
//--></script>
<script type="text/javascript">var _wdfb_ajaxurl="http://www.overheardindublin.com/wp-admin/admin-ajax.php";</script><script type="text/javascript">var _wdfb_root_url="http://www.overheardindublin.com/wp-content/plugins/facebook";</script>Could not open socket

Open in new window

Here is what I would do.  Since this site appears to be built on a WordPress platform, it is probably 100% PHP.  I would get a code scanner like Textpad "find in files" functionality and scan all of the scripts for the case-sensitive string Could not open socket and with each script that contains that string, I would look for conditional code - something testing a return value, perhaps from the fsockopen() function.  I am slightly suspicious of the reCaptcha implementation, but that's just an instinctive reaction.  Once you find the string in the script or scripts, we can dig a little deeper since we will be able to see the code that is in play.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37819511
Ahh - just saw your post, I'll take a look at that now.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37819535
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."
0
 

Author Comment

by:Cosmic_Cake
ID: 37819671
" 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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37819679
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_9849-Making-CAPTCHA-Friendlier-with-PHP-Image-Manipulation.html

Best, ~Ray
0
 

Author Comment

by:Cosmic_Cake
ID: 37819777
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;
}
0
 

Author Comment

by:Cosmic_Cake
ID: 37819908
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 "
0
 

Author Closing Comment

by:Cosmic_Cake
ID: 37820190
Very practical answers, thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Creating and Managing Databases with phpMyAdmin in cPanel.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now