Problem 1: Browsers can do POST but only from FORMs, not from links. This is less compact HTML, can't have a single (URL) expression to define the call; much messier JAVA if you try to use java to re-assign the specs for the call., etc. (Still, FORMs may be invisible; and you could have an Crs 2) Use my postredirect.php script: Crs ==================================================================== PROBLEM 1 ============= The problem. I'd like to have a working PHP method for doing POST; partly for future toolkit, partly to improve myteachtable4.php Case: https://web1.psy.gla.ac.uk/teaching/timetable.php?weekID=16 Ditto for CRs signup page Marc's code here just reads POST input vars, no others. So I can't do href: HREFs just do GET, and so do ordinary (php) redirects. I have a workround by using an HTML FORM to do POST, but it stops back button working simply in end-user's browser, and involves an extra msg. Possible problems in my code to do PHP post: https cookies password Try contexts. See pwfr2.php? *See myfilegetcontents ============= Method 1 function post_it($datastream, $url) { $url = preg_replace("@^http://@i", "", $url); $host = substr($url, 0, strpos($url, "/")); $uri = strstr($url, "/"); $reqbody = ""; foreach($datastream as $key=>$val) { if (!empty($reqbody)) $reqbody.= "&"; $reqbody.= $key."=".urlencode($val); } $contentlength = strlen($reqbody); $reqheader = "POST $uri HTTP/1.1\r\n". "Host: $host\n". "User-Agent: PostIt\r\n". "Content-Type: application/x-www-form-urlencoded\r\n". "Content-Length: $contentlength\r\n\r\n". "$reqbody\r\n"; $socket = fsockopen($host, 80, $errno, $errstr); if (!$socket) { $result["errno"] = $errno; $result["errstr"] = $errstr; return $result; } fputs($socket, $reqheader); while (!feof($socket)) { $result[] = fgets($socket, 4096); } fclose($socket); return $result; } // end function ============= Method 2 $arr = parse_url($targeturl); $path = $arr['path']; $host = $arr['host']; //echo "path: $path"; echo "host: $host"; exit; $data = "weekID=23"; $data = urlencode($data); header("POST $path HTTP/1.1\r\n" ); header("Host: $host\r\n" ); header("Content-type: application/x-www-form-urlencoded\r\n" ); header("Content-length: " . strlen($data) . "\r\n" ); header("Connection: close\r\n\r\n" ); header($data); ============= Method 3
============= Method 3b: Hidden form to automatically do POST -- BUT don't need to have the form in HTML, nor even to add it to DOMtree. Just create it, and exec it, in response to a javascript call (from a link/btn). function post_to_url(path, params, method) { method = method || "post"; // Set method to post by default var form = document.createElement("form"); form.setAttribute("method", method); form.setAttribute("action", path); // Could / don't need to set style to invisible (display='none') // ?OR put in code to decode args from path in string, and use them. // there isn't a built in for this; but is code on web. for(var key in params) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", key); hiddenField.setAttribute("value", params[key]); form.appendChild(hiddenField); } document.body.appendChild(form); // Not necessary ?? form.submit(); } ============= Comment 4 //Alternative methods here: //Get Marc's page, edit it, display it momentarily before auto-send. //This one: display only a tiny page; but use page and FORM to achieve POST //Jump direct using URL: but Marc ignores GET from URL-use. //Use php Header() to do POST. Couldn't get this to work. // See work on this in myteachtable3.php