It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

fsockopen

Moderator: Malcolm

fsockopen

Postby flabbyrabbit on Mon Jan 28, 2008 11:50 pm

Does anyone know away of submitting POST variables without using a form? I have narrowed it down to "fsockopen" which requires you to create your own connection with the server and create your own header, but im not to sure how it works. Can anyone help??

Flabby Rabbit
User avatar
flabbyrabbit
500+ Club
 
Posts: 706
Joined: Thu Jan 25, 2007 1:10 pm
Location: Midlands, England

Postby leonard on Tue Jan 29, 2008 8:27 am

fsockopen opens a new port on your physical server. Meaning you would create an own server/service by using it. The data will therefore not go through your webserver (port 80/443) anymore.
This new port could also be blocked by many firewalls.

Depending on what you need, an alternative could be an xmlhttp-request.
See topic AJAX without Javascript for more info on xmlhttp-request: ajax-without-javascript-vt8800.html

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland

Postby knowa on Wed Feb 06, 2008 6:20 pm

why don't u use sessions?
User avatar
knowa
 
Posts: 19
Joined: Sat Dec 08, 2007 4:32 pm
Location: Switzerland

Postby flabbyrabbit on Wed Feb 06, 2008 6:45 pm

I needed to keep reloading a php file, without affecting the rest of the page (e.g. no refresh). I ended up using AJAX and they worked a treat, very happy with them. Use them all the time now, thanks leonard.

Flabby Rabbit
Image
User avatar
flabbyrabbit
500+ Club
 
Posts: 706
Joined: Thu Jan 25, 2007 1:10 pm
Location: Midlands, England

Postby leonard on Fri Feb 08, 2008 11:38 am

Glad that I was able to help. Thanks for the comment flabby.

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland

Postby flabbyrabbit on Fri Feb 08, 2008 4:16 pm

Ok i do need help with one more thing. How do you send POST variables with AJAX? I currently use:
Code: Select all
function ajaxFunction(req)
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById('answer').innerHTML = xmlHttp.responseText;
}
}
var lelele = Math.random();
lelele = "http://www.site.co.uk/page.php?lol=" + lelele;
xmlHttp.open("GET",lelele,true);
xmlHttp.send(null);
}

How can i edit this to send POST variables??

Thank you,
Flabby Rabbit
Image
User avatar
flabbyrabbit
500+ Club
 
Posts: 706
Joined: Thu Jan 25, 2007 1:10 pm
Location: Midlands, England

Postby leonard on Sat Feb 09, 2008 8:05 am

One possibility:

add function:
Code: Select all
function addToPost(argument,value)
{
   var check = postString.search(/[=]/);
   if (check != -1)
   { postString += "&" }

   value = escape(value);
   //value = encodeURIComponent(value);
   postString += argument + "=" + value;
}

then use it like:
Code: Select all
   postString = '';
   addToPost('ADDR',document.myForm.ADDR.value);
   addToPost('TEL',document.myForm.TEL.value);

   http_request = getHTTPRequest();
   http_request.onreadystatechange = myJavaScriptResponseFunction;
   http_request.open('POST', './myFunction.php', true);
   http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   http_request.send(postString);
}


cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland

Postby flabbyrabbit on Sat Feb 09, 2008 2:25 pm

Ok, im confused or just doing somthing completly wrong.
Code: Select all
<script type="text/javascript">
function addToPost(argument,value)
{
var check = postString.search(/[=]/);
if (check != -1){
postString += "&"
}
value = escape(value);
//value = encodeURIComponent(value);
postString += argument + "=" + value;
}
function ajaxFunction() {
postString = '';
addToPost('user','name');
addToPost('pass','word');
http_request = getHTTPRequest(); //This line messes it up!!!!!!
http_request.onreadystatechange = myJavaScriptResponseFunction;
http_request.open('POST', 'post.php', true);
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http_request.send(postString);
}
</script>
<a onClick="javascript:ajaxFunction();">RUN</a>

It runs up until "http_request = getHTTPRequest();" then stops. Did you get this from a tutorial/website that i could have look at?? Or you give me the full script to make this thing work.

Thank you,
Flabby Rabbit
Image
User avatar
flabbyrabbit
500+ Club
 
Posts: 706
Joined: Thu Jan 25, 2007 1:10 pm
Location: Midlands, England

Postby leonard on Sat Feb 09, 2008 5:44 pm

Sorry. Here you go:
Code: Select all

var postString = "";

function getHTTPRequest()
{
     http_request = false;

     if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
             http_request.overrideMimeType('text/xml; charset=ISO-8859-1');
         }
     } else if (window.ActiveXObject) { // IE
         try {
             http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
             try {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (e) {}
         }
     }

     if (!http_request) {
         alert('could not create XMLHTTP-object');
         return false;
     }
     return http_request;
}

I had outsourced the request to a function.

But if you use PHP, don't you want to use the xajax-library? No need to write your own ajax-javascript with that.

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland


Who is online

Users browsing this forum: No registered users and 1 guest