-
Posts
15,229 -
Joined
-
Last visited
-
Days Won
427
Everything posted by requinix
-
You can't: prepared statements are about passing data and IS NULL is actually about changing the structure of the query itself. "IS" is an operator like > and = and you can't change operators after you've prepared the statement. PHP doesn't have a good solution for passing an arbitrary number of parameters to bind_param(), but you can still do it. $q = "SELECT user_id FROM users WHERE"; $bind = array(""); // password $q .= " pass = ?"; $bind[0] .= "s"; $bind[] =& $pw; // reference // active if ($a === null) { $q .= " AND active IS NULL"; } else { $q .= " AND active = ?"; $bind[0] .= "s"; $bind[] =& $a; // reference } $r = mysqli_prepare($dbc, $q); call_user_func_array(array($r, "bind_param"), $bind); // easier to call it as a method mysqli_stmt_execute($r);
-
Exec() is a bit more powerful than you may expect. As a demonstration, $com = new COM("WScript.Shell"); $exec = $com->Exec("cmd /c dir C:\\"); // cmd /c because I want dir which is a cmd command, not an actual program // $exec = $com->Exec("tasklist") for a regular program // from here on you should treat it like a regular process // that includes things like closing stdin when you're done sending input (if any) $exec->StdIn->Close(); echo "Listing everything under C:\\ with a command prompt.\n"; echo "Process ID: {$exec->ProcessID}\n"; $stdout = $exec->StdOut; $line = 1; while (!$stdout->AtEndOfStream) { echo " {$line} {$stdout->ReadLine()}\n"; $line++; } Listing everything under C:\ with a command prompt. Process ID: 6708 1 Volume in drive C is OS 2 Volume Serial Number is 842F-D8EC 3 4 Directory of C:\ 5 6 04/04/2013 10:47 PM <DIR> apps 7 05/14/2013 08:13 AM <DIR> dell 8 04/05/2013 12:10 AM <DIR> Drivers 9 04/04/2013 10:38 PM <DIR> Intel 10 05/20/2013 01:19 PM <DIR> msysgit 11 07/13/2009 07:20 PM <DIR> PerfLogs 12 10/23/2013 01:24 PM <DIR> Program Files 13 10/16/2013 12:57 PM <DIR> Program Files (x86) 14 10/08/2013 07:22 PM <DIR> Temp 15 05/15/2013 11:11 AM 31 tmuninst.ini 16 07/05/2013 11:41 PM <DIR> Users 17 10/11/2013 09:20 AM <DIR> Windows 18 1 File(s) 31 bytes 19 11 Dir(s) 711,940,968,448 bytes freeAs this shows, you don't need output redirection at all since you now have direct access to the output streams. $input = "C:\\2.avi"; $output = "C:\\2.mkv"; $com = new COM("WScript.Shell"); $exec = $com->Exec("C:\\ffmpeg -y -i " . escapeshellarg($input) . " -preset slow -crf 25 " . escapeshellarg($output)); $pid = $exec->ProcessID; $exec->StdIn->Close(); $output = $exec->StdOut->ReadAll(); $error = $exec->StdErr->ReadAll(); HOWEVER, after all this work, all you've really gone and done is written a Windows-only version of PHP's built-in, cross-platform proc_open (with proc_get_status() to get the PID). So yes, you should probably be using that instead of all this COM stuff.
-
Arrays are just there to make it easier to have multiple "values" in one form. In your case you can think of that as multiple answers to your questionnaire... however you only have one question, and that one question only has one answer. So actually arrays don't do you any good here. They'd be more suited if you wanted to do multiple questions. <form action="" method="post" /> <label for="question" class="question">I often exercise on a daily basis for at least 20 minutes a day</label> <label for="Never" class="radio"> <input type="radio" name="answer" value="Never" /> Never</label> <label for="Seldom" class="radio"> <input type="radio" name="answer" value="Seldom" /> Seldom</label> <label for="Occasionally" class="radio"> <input type="radio" name="answer" value="Occasionally" /> Occasionally</label> <label for="Often" class="radio"> <input type="radio" name="answer" value="Often" /> Often</label> <label for="Always" class="radio"> <input type="radio" name="answer" value="Always" /> Always</label> </form> if (isset($_POST["answer"])) { echo htmlspecialchars($_POST["answer"], ENT_QUOTES); }So for the purposes of showing how arrays work, let's say you wanted multiple questions at a time. Each "value" from the form will be an answer to each question. Like how arrays normally work in PHP, you want the key to identify what the value corresponds to. Here, the key makes most sense to identify the question: 0 for the first question, 1 for the second, and so on. If you were getting these questions from a database then using the question ID would be better than just counting from zero. "But doesn't that mean each answer for a question has the same name?" Yes. Yes it does. But a user can only choose one answer per question so that's fine. It also works perfectly with how radio buttons work in HTML: only one value can be chosen for each unique name. That's why you don't have to write anything that automatically unselects previous values when a user selects a new one... and if you played around with your form you would have noticed that didn't happen (because all the names were different). I often exercise on a daily basis for at least 20 minutes a day <label for="Never" class="radio"> <input type="radio" name="answer[0]" value="Never" /> Never</label> <label for="Seldom" class="radio"> <input type="radio" name="answer[0]" value="Seldom" /> Seldom</label> <label for="Occasionally" class="radio"> <input type="radio" name="answer[0]" value="Occasionally" /> Occasionally</label> <label for="Often" class="radio"> <input type="radio" name="answer[0]" value="Often" /> Often</label> <label for="Always" class="radio"> <input type="radio" name="answer[0]" value="Always" /> Always</label> <hr /> I like eating fatty and fried foods because they are so damn delicious. <label for="Never" class="radio"> <input type="radio" name="answer[1]" value="Never" /> Never</label> <label for="Seldom" class="radio"> <input type="radio" name="answer[1]" value="Seldom" /> Seldom</label> <label for="Occasionally" class="radio"> <input type="radio" name="answer[1]" value="Occasionally" /> Occasionally</label> <label for="Often" class="radio"> <input type="radio" name="answer[1]" value="Often" /> Often</label> <label for="Always" class="radio"> <input type="radio" name="answer[1]" value="Always" /> Always</label> I also removed the label on the question because it's not really appropriate there: labels match up text and input, and there is no one input to match up with the question text (rather, there's too many). There are more appropriate things to use but that's for another time. Now, $_POST["answer"] will be the array you're expecting. Print it out to see what you got. if (isset($_POST["answer"]) && is_array($_POST["answer"]) && count($_POST["answer"]) > 0) { print_r($_POST["answer"]); }
- 5 replies
-
- phpradio buttons
- form
-
(and 1 more)
Tagged with:
-
WScript.Shell.Run is documented over here. Remember that you're running this in the background: you can't get the output "now" because the command hasn't finished executing, and even if you redirect output to a file (which is how you'd do it here) you can't get it in the next couple statements because the command still hasn't finished executing. That's the downside to running in the background so reconsider whether you actually want to do that. As for output redirection, yes it seems you need cmd /c - I thought it was run inside cmd anyways but apparently not. cmd /c C:\ffmpeg ... 1>output.log 2>error.log
-
& does not work on Windows. if (strncasecmp(PHP_OS, "Win", 3) == 0) { $com = new COM("WScript.Shell"); $com->Run('C:\ffmpeg ...', 0, false); } else { shell_exec('ffmpeg ... &'); }
-
Wouldn't checkboxes be easier? Teacher checks the students who are attending. Regardless, what code do you have now and what doesn't work?
-
It would be a timestamp instead of the status field. Update it to the current time when they play. When you want to know who/whether someone is available, look at that timestamp: if it comes from today then they're not available, otherwise they are.
-
9/10 times if you want to update something in a database at a certain time, like for "resetting" something or updating a counter, there's a smarter way of going about it. In this case the smarter way is just keeping track of the last time they played. If they played played today then they're not available, otherwise (if they played sometime before today) then they are available.
-
You can't know whether someone is still "logged in" on a tab or window. This is one of those situations where you can't really solve it using code. It's a problem with the actual person (people) sitting at the computer. "Problem exists between keyboard and chair" as they say: the two people know damn well that they are both using the same browser and same computer, and if they leave themselves logged in then they can't assume you're somehow going to magically know that it's a different person sitting in front of the computer. What you can do is prevent user A from being able to make changes to user B's profile: require a password to make changes. User A won't know the password so they can't do anything.
-
I don't know how that's even possible. Same browser on the same computer? You can't have two users logged in at the same time unless you specifically allow for multiple people to be logged in at once. The two people will be using the same set of cookies, so after user A logs in, if user B tries to browse (that is, user A gets up out of the chair and lets user B sit down at the computer instead) then they will still be logged in as user A. How is that possible? Because the computer, the browser, and your website doesn't know that it's physically a different person. If user B now tries to log in, either a] that's not possible because they're already logged in and there shouldn't be a way for a second user to log in while a first user is already, or b] the user now logged in will be user B and user A is gone... unless you have a bug in your system where not all the user A information is lost (but I don't see that in the code you've posted).
-
Doing it over AJAX is just as secure/insecure as using a regular form. Really, the only difference between the two (besides the implementation) is whether the form will cause the browser to navigate to a new URL or not.
-
Two things I see. First is that you shouldn't be using a loop: all you care about are the absolute first four bytes, not every set of four bytes in the file. Second is that you're opening the file in just read mode without specifying a "binary" flag too. That basically only matters on Windows, and even then not necessarily, but if it did then reading the file without it may not get you the bytes you're expecting. Try opening in "rb" mode.
-
Looking For A Regex That Validates "öÄßáéóúÁÉÍÓÚüÜñÑ"
requinix replied to phdphd's topic in PHP Coding Help
1. That's bad. Don't do that. The only time you should ever be using htmlspecialchars() is immediately before you're about to output something in HTML. Not any other point before then. Especially not when you're inserting it into your database. 2. That doesn't convert encodings. All you did was tell it that it should interpret the string as if it was UTF-8. If it wasn't to begin with then it still won't be after. var_dump(preg_match('/^[[:alnum:]]+$/u', 'Schönen Tag'));works for me as long as I make sure I put that code into a file and save it using UTF-8. If I don't then it might be ISO 8859-1 by default and I'd have to utf8_encode() (which converts from that encoding to UTF-8 ) the string first. -
Looking For A Regex That Validates "öÄßáéóúÁÉÍÓÚüÜñÑ"
requinix replied to phdphd's topic in PHP Coding Help
You had # before as the delimiter. You don't need to add /s on top of them. It's just #[[:alnum:]]#u. If you want to verify the entire string, and not just that there is an alphanumeric character somewhere in it, then you need to check every character from the beginning to the end. #^[[:alnum:]]+$#u -
Looking For A Regex That Validates "öÄßáéóúÁÉÍÓÚüÜñÑ"
requinix replied to phdphd's topic in PHP Coding Help
You didn't. Your new expression says "the string must either (a) contain an alphanumeric character or (b) contain an non-alphanumeric character". It'll match against anything but an empty string. -
Rather than give up on that, how about we figure out why it wasn't working? What code did you try?
-
die($trainSloppyCheeseMen->turns);If you give an integer to die() then it will not be printed. die If you want to see the value, die($trainSloppyCheeseMen->turns . " turns");make it not an integer.
-
Don't allow people to spend less than the minimum. Two simple ways of tackling the curve: 1. Asymptotes, like how y=1/x never reaches y=0. Normally a steep curve but you can scale it out. 2. Quadratics (or another even power), like how y=x^2 never goes below y=0. How about a sketch of what you're looking for? How much is too much curvature?
-
The errors are in the first few lines of the file. How about posting them so we don't have to download something? You probably have something else around that point that's causing the problem. HTML doesn't support other namespaces (which is what b:include is doing). I bet that's some sort of templating thing that's supposed to get interpreted by something on the server and transformed into normal HTML for the client? In which case it sounds like the templating thing isn't executing (considering how you're validating through the browser, which should have received the correct post-templating output).
-
https://netbeans.org/bugzilla/show_bug.cgi?id=195647 Basically, don't use the HTML validator on PHP files.
-
There is nothing wrong with that line. What are you using that claims the line is bad? Does it know how to understand PHP code?
-
As long as you're talking about reloading the page with the effects of having $a=0, 1. Name the button. Not an id but a name. 2. Look in $_GET or $_POST (depending on the form method) for the presence of the button. That indicates it was clicked. 3. If present, set $a=0. Try that out in your code. If you have problems or questions, post what you have and what's wrong.
-
Trouble getting element values out of XML with namespaces
requinix replied to desmod's topic in PHP Coding Help
Side comment: rather than going by [0] and [1] to get the nodes you want, use a condition to match against the specific Name. So there's two namespaces to care about: the default "" (urn:broadband-forum-org:ipdr:tr-232-1-0) and "ipdr" (http://www.ipdr.org/namespaces/ipdr). Since you don't need to worry about where the BulkData appears in the XML (it's only in one place) you don't need to try to form a complete hierarchy to reach one and you can jump directly to the BulkData nodes directly. 1. Register the default namespace with any name $sxe->registerXPathNamespace('ns', 'urn:broadband-forum-org:ipdr:tr-232-1-0');2. To get to a node in XPath regardless of hierarchy, use //node '//ns:BulkData'3. Use a condition to target a specific node. Let's start with the uptime: '//ns:BulkData[ns:Name="InternetGatewayDevice.DeviceInfo.UpTime"]'4. Having found the BulkData you want, grab the Value node beneath it. '//ns:BulkData[ns:Name="InternetGatewayDevice.DeviceInfo.UpTime"]/ns:Value'As an example of the full process, $xml = new SimpleXMLElement(<<<XML <?xml version = "1.0" encoding = "UTF-8"?> <ipdr:IPDRDoc xmlns:ipdr="http://www.ipdr.org/namespaces/ipdr" xmlns="urn:broadband-forum-org:ipdr:tr-232-1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:broadband-forum-org:ipdr:tr-232-1-0 tr-232-1-0-0-serviceSpec.xsd http://www.ipdr.org/namespaces/ipdr http://www.ipdr.org/public/IPDRDoc3.5.1.xsd" docId="74697373-6f74-7878-7878-746973736f74" creationTime="2013-06-11T05:52:55.153Z" IPDRRecorderInfo="IPDR Collector" version="3.5.1"> <ipdr:IPDR xsi:type="BulkDataReport"> <OUI>124BEB</OUI> <ProductClass>BGW</ProductClass> <SerialNumber>1234567890</SerialNumber> <Suspect>1</Suspect> <BulkData> <Name>InternetGatewayDevice.DeviceInfo.UpTime</Name> <Value>1449</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.ManagementServer.URL</Name> <Value>www.somesite.com</Value> </BulkData> </ipdr:IPDR > <ipdr:IPDRDoc.End count="1" endTime="2013-06-11T05:52:55.207Z"></ipdr:IPDRDoc.End> </ipdr:IPDRDoc> XML , 0, false); $xml->registerXPathNamespace("ns", "urn:broadband-forum-org:ipdr:tr-232-1-0"); $value = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.DeviceInfo.UpTime']/ns:Value"); var_dump($value); // array(1) { // [0]=> // object(SimpleXMLElement)#2 (1) { // [0]=> // string(4) "1449" // } // } var_dump((string)$value[0]); // string(4) "1449" -
destroy method to remove, then re-add when you want it. But enable/disable is still the best option.
-
You should probably be using the enable/disable methods. Set up the sorting at the start and then disable it immediately (since that's the default). Then enable/disable as needed.