-
Posts
46 -
Joined
-
Last visited
Everything posted by MutantJohn
-
Okay, quick question : Am I feeding this code the wrong URL? <html> <head /> <body> <?php function index() { echo "PHP async test...<br />"; $params = array("one" => "1111", "two" => "2222", "three" => "3333", "four" => "4444"); // is this part wrong? curl_post_async("http://127.0.0.1/longone", $params); } function longone() { $one = $_POST["one"]; $two = $_POST["two"]; $three = $_POST["three"]; $four = $_POST["four"]; echo uniqid("You won't see this because your PHP script isn't waiting to read any response"); sleep(5); $fp = fopen("data.txt", "w"); fwrite($fp, $one); fwrite($fp, $two); fwrite($fp, $three); fwrtie($fp, $four); fclose($fp); } function curl_post_async($url, $params = array()) { // declare array for $_POST parameters $post_params = array(); // iterate the parameters passed to this function foreach($params as $key => &$val) { // if the parameter is an array... if (is_array($val)) $val = implode(",", $val); // push back the key-value pair $key_val = $key."=".urlencode($val); array_push($post_params, $key_val); var_dump($key_val); echo "<br />"; } // create one string to hold all parameters $post_string = implode("&", $post_params); var_dump($post_string); echo "<br />"; $parts = parse_url($url); var_dump($parts); echo "<br />"; $fp = fsockopen($parts["host"], // hostname $parts["port"] ? $parts["port"] : 80, // port $errno, $errstr, 30); $out = "POST ".$parts["path"]." HTTP/1.1\r\n"; $out .= "Host: ".$parts["host"]."\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; $out .= "Content-Length: ".strlen($post_string)."\r\n"; $out .= "Connection: Close\r\n\r\n"; if (isset($post_string)) $out .= $post_string; var_dump($out); echo "<br />"; if (fwrite($fp, $out) === false) echo "Failed to write to socket<br />"; fclose($fp); } index(); ?> </body> </html>
-
I gotta admit, PHP is pretty cool. Alright, I'm gonna research this method some more. Thanks for the help, you guys! Edit : And I have heard a little bit about headers and web servers. Basically, when the web server receives a request, headers come before all else which is why you can't have any output if you're going to be forwarding headers. Is there anything else I should know or read up on?
-
I appreciate your advice. Aright, I want to decode this function line-by-line to make sure that I'm understanding this all correctly. function curl_post_async($url, $params = array()){ // declare simple array to store $_POST parameters (makes sense) $post_params = array(); // iterate the parameter array, using $val as a // reference for each element foreach ($params as $key => &$val) { // if $val is an array type, implode it into one argument if (is_array($val)) $val = implode(',', $val); // not sure about the syntax for this // my guess is that this is similar to C++'s push_back feature // and we are pushing back the index of the array and // and encodes the argument as a url? $post_params[] = $key.'='.urlencode($val); } // the post string is then the collapse of all parameters, // using & as a delim $post_string = implode('&', $post_params); // we then parse the url but I'm not sure why we want this $parts=parse_url($url); // we open up a socket // $hostname = ??? where does the 'host' come from? // is that a side-effect of the parse_url()? // $port : if 'port' or parts is set, we return the port else 80 // $errno, $errstr, $timeout : default variables for error and timeout $fp = fsockopen($parts['host'], isset($parts['port'])?$parts['port']:80, $errno, $errstr, 30); // we then create a string $out full of what seems to me // to be random info required for some reason $out = "POST ".$parts['path']." HTTP/1.1\r\n"; $out.= "Host: ".$parts['host']."\r\n"; $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; $out.= "Content-Length: ".strlen($post_string)."\r\n"; $out.= "Connection: Close\r\n\r\n"; if (isset($post_string)) $out.= $post_string; // we then write the $out string to the socket resource fwrite($fp, $out); // we then close the socket but won't this function // cause the socket to immediately close? fclose($fp); }
-
That code looked complicated and it scared me lol T_T
-
Oh ****, you are so right. Omg, I'm dumb. Omg. Okay, I'ma try that.
-
Oh, I meant executing the JavaScript and the PHP code in the same action field. But I did talk about with my boss and the idea was suggested that we just put a disclaimer on the page that the PDF generation can take a chunk of time. This is a good workaround but it's exactly that, a work around. I'll continue looking at the links posted in this thread though.
-
Can I have both in the same action?
-
Lol I literally just tried with my page and you're right, it definitely does not work.
-
Interesting. What about pcntl_fork()? This seems like something that I might want. <?php $pid = pcntl_fork(); if ($pid == -1) { die("Could not fork process"); } else if ($pid) { echo "Parent process is waiting...\n"; pcntl_wait($status); echo "Alright, parent's done waiting. Let's go home.\n"; } else { echo "Child process!\n"; sleep(10); } ?>
-
Okay, so I have a neat little website and I'm having some issue with some quality-of-life improvements. Namely, the user clicks a button which starts a server-side operation that can take up to 20 to 30 seconds. I want a little message to appear below the button that says, "Operation started. This may take upwards of 20 to 30 seconds depending on traffic." As of now, I have the typical <form action="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> ... <input type="submit" name="submit" value="Make PDF" /> </form> <?php if (isset($_POST["submit"])) { ... } ?> The only problem is, part of my PHP code must communicate with a Java server that I have running on the server itself. So how the site works is, there's the computer I'm renting out and it's running Apache and a custom Java server I wrote myself. Apache handles the web request and upon form submission, PHP opens a socket with the Java server and begins the task. PHP then waits for the connection to close. This code looks like : fwrite($sock, $data); $pdf_to_download = ""; // wait for write-back from Java server while (!feof($sock)) $pdf_to_download = fgets($sock, 4096); fclose($sock); The only problem is, I can't seem to get PHP to write a message until the entire script is done running so if put an echo statement at the beginning of the script (the "This takes a long time" message), it's not generated until the entire script is done running. How do I make it so that when the user clicks the submit button, an initial PHP message is echoed back and the task I need accomplished is started? Do I need multiple threads for this? Do I need PHP to make two independent socket requests with Apache? One request to echo the message at the proper location in the HTML and then another socket connection to begin my time-intensive task?
-
So, I'm trying to read a file from a different filepath than the current working directory. I have some simple PHP like this : <?php $input_map = "readfile.php"; $map_contents = file_get_contents($input_map); echo $map_contents."<br />\n"; ?> And this'll work fine because it's in the same directory but if I try to set $input_map = "/home/...", the browser will return an empty string. This script will work from the command line though. So how do I get file_get_contents() to read from the server's file structure instead of just the working directory? Edit : For example, this will not work through the browser : <?php $input_map = "/home/..."; $map_contents = file_get_contents($input_map); echo $map_contents."<br />\n"; var_dump($map_contents); ?>
-
I'm not sure if this is what you're looking for but there are ways of profiling PHP code : http://stackoverflow.com/questions/21133/simplest-way-to-profile-a-php-script
-
Having trouble sending a PDF to user's browser...
MutantJohn replied to MutantJohn's topic in PHP Coding Help
Yup. Son of a ... Okay. Okay, I'm calm. I'm cool. This is all cool. I put the pdf in the same directory as the PHP script which is all owned by http and yeah... It works now. *sigh* Thank you so much for your help. You're awesome. -
Having trouble sending a PDF to user's browser...
MutantJohn replied to MutantJohn's topic in PHP Coding Help
This is my ls -l output : -rw-rw-rw- 1 http http 46991 Jan 13 10:44 /tmp/c51d96c8f5fe46d21fc7aa232c04d096.pdf User and group http can read/write and all other users can read/write as well. File size is 46,9991 bytes I have no idea why this is happening. ls -l output for /tmp folder : drwxrwxrwt 18 root root 840 Jan 13 11:47 tmp Is it because /tmp is owned by the root? I -
Having trouble sending a PDF to user's browser...
MutantJohn replied to MutantJohn's topic in PHP Coding Help
Scary, this isn't working for me either. Is there something wrong with my php.ini file? I'm trying to think of what I could be doing wrong and this is quite the head scratcher. Especially because it works for you. This must be something weird on my side. Apache is up and running and everything. Hmm... -
Having trouble sending a PDF to user's browser...
MutantJohn replied to MutantJohn's topic in PHP Coding Help
No dice. One thing I noticed is that the file I'm sending is 0 bytes. My ls -l output is : -rw-r--r-- 1 my_username users 0 <---- Looks like 0 bytes are being sent... -
Having trouble sending a PDF to user's browser...
MutantJohn replied to MutantJohn's topic in PHP Coding Help
Right, I was starting to figure that. But even this doesn't work <?php // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('/tmp/c51d96c8f5fe46d21fc7aa232c04d096.pdf'); ?> I can open this PDF fine (the /tmp/c51... file is good). But the browser seems to think it's a plain text file if I try to open downloaded.pdf. I'm using Chrome at the moment and I'll just be stuck loading it forever. -
Hey guys, So, I have some complicated code, kind of. I'm using a Java server to generate a PDF. The actual user interacts with a HTML/PHP page and they click a submit and everything is good so far. I generate a PDF to my /tmp directory (I'm using Linux) and it's a perfectly fine PDF. I can open it and read it and it's perfectly as it should be. But when I try to send it to the user's browser as a download, the PDF I get doesn't open. The error I get is "File type HTML document (text/html) is not supported" and that I'm unable to open the document. My biggest question is, why? Is it something to do with the permissions of my directories and files? I've had a lot of errors be caused because of permissions so it wouldn't surprise me. My output to the user page is : Successfully connected to Java server. Socket is closed. File to send to user : /tmp/27410c981769c34ea07c3575beebd2a2.pdf This is odd because I'm missing an echo statement. Is this because I'm not using output buffering? Here's the relevant code snippet : if (($sock = fsockopen($host, $port, $errno, $errstr, 3)) == false) echo "$errstr ($errno)"; else { echo "Successfully connected to Java server.\n<br/ >"; fwrite($sock, $data); $pdf_to_download = ""; while (!feof($sock)) $pdf_to_download = fgets($sock, 4096); fclose($sock); echo "Socket closed successfully\n<br/ >"; echo "File to send to user : ".$pdf_to_download."\n<br />"; echo "Attempting to send PDF...\n<br />"; header("Content-type: application/pdf"); header('Content-Disposition: attachment; filename = "flowers.pdf"'); readfile($pdf_to_download); }
-
Unique rand() directory names in temp folder
MutantJohn replied to MutantJohn's topic in PHP Coding Help
Hey, that's a much better idea. I also read up on flock() but I couldn't get it to work. Just using PHP's native rand() function, I was planning on doing something like this : $outdir = "/tmp/"; $fp = fopen("/tmp", "w+"); // this works if (is_readable("/tmp")) echo "Can read directory.<br />\n"; // this works if (is_writable("/tmp")) echo "Can write directory.<br />\n"; // this always returns false if (flock($fp, LOCK_EX)) { // initial hash $dir_hash = rand(); // if the subdirectory/file exists, loop while (file_exists("/tmp/".$dir_hash)) { $dir_hash = rand(); } // append random number $outdir .= $dir_hash; // release lock flock($fp, LOCK_UN); } else echo "Was unable to lock directory <br />\n"; fclose($fp); echo $outdir."<br />\n"; -
Okay, so I'm kind of a PHP noob. But out of context, for this site that I'm designing, it's easiest if I make a directory in the temporary folder PHP uses. In my case, /tmp/ because I am on Linux. I want to use rand() to generate a random name for the page. But I then realized something, rand() could produce duplicates. How do I prevent PHP from trying to make the same directory at the same time? I know there are functions that will check if a file exists but I'm assuming it'll fail if that directory is currently being created, right? How do I assure thread safety? I willing to change my idea and not use rand(). Is there a way to get a unique key for each anonymous user on my site?