Jump to content

requinix

Administrators
  • Posts

    15,227
  • Joined

  • Last visited

  • Days Won

    427

Community Answers

  1. requinix's post in What did he mean when he wrote this? was marked as the answer   
    He's mostly correct but should have been more precise.
     
    If you try to assign to a variable (or property) that does not exist then PHP will (generally) create it for you no problem. In fact for variables this is how you're supposed to do it because there's no way to define them beforehand, unlike properties.

    <?php class Example { } $e = new Example(); $e->no_such_property = 123; var_dump($e); object(Example)#1 (1) { ["no_such_property"]=> int(123) }If you try to read from a variable (or property) that does not exist then PHP will issue a warning and pretend the value is null.
    <?php var_dump($no_such_variable); Notice: Undefined variable: no_such_variable in... NULL <?php class Example { } $e = new Example(); var_dump($e->no_such_property); Notice: Undefined property: Example::$no_such_property in... NULLIf you define the property in the class then that doesn't happen.
    <?php class Example { public $no_such_property; // defines the property with a default value of NULL } $e = new Example(); var_dump($e->no_such_property); NULL
  2. requinix's post in Does the order of an object's properties mean anything? was marked as the answer   
    Order does not matter. Loose object comparison works like an array comparison.
  3. requinix's post in Order of precedent for accessing properties and ternary operators was marked as the answer   
    Yeah, PHP 7 has a number of syntax improvements. You'll have to use the long form.
     
    And yes, $x ?: $y is shorthand for $x ? $x : $y. PHP 7 also adds $x ?? $y which is short for isset($x) ? $x : $y.
  4. requinix's post in How to check token in form created by password_hash() function was marked as the answer   
    Is this token a password? No? Then don't try to make it a password. All you need is a random value, so

    sha1(uniqid(time(), true))is sufficient. 
    Put the value in the form and in the session, then compare the two values. Regular string comparison.
  5. requinix's post in Total number from XML values was marked as the answer   
    Then do that.
    $value = @$counts["Tornado Warning"] + @$counts["Severe Thunderstorm Warning"] + @$counts["Flash Flood Warning"];I'm using @ here because PHP will warn if the warning doesn't exist in the array and its default behavior of evaluating to null is acceptable.
  6. requinix's post in Dynamic populating rows was marked as the answer   
    You're using the part of Bootstrap's styling that sizes boxes. Don't do that.
  7. requinix's post in Pagination output was marked as the answer   
    $page_nums = range($lower_limit, $upper_limit);range() supports counting down, such as from 1 to 0. Add some logic in there to make sure that doesn't happen; I suggest returning an empty array if num_rows == 0.
  8. requinix's post in php function() help... was marked as the answer   
    Actually the problem is your query:

    mysql_query("SELECT * FROM $datatable")You're using a variable $datatable. Variables defined outside of functions are not automatically available inside functions, so $datatable is undefined and the query you execute is
    SELECT * FROMObviously, MySQL complains because the table name is missing. 
    If you want the best advice for what to do then the answer depends on what this function is supposed to represent. Like, why are you moving everything into a function? Is it really just for appearance's sake? That's not that much of a reason: it's the same code just in a different place. In fact, code like that typically does not go into a function because what you're doing to display the data is highly specific to where you're displaying it, and it's unlikely that you'll want to do the exact same thing in another place.
     
    So without knowing much more, I would say to leave the code where it is. You can make it look a nicer by not doing everything within PHP; I would write that code like

    <?php // ... $result = mysql_query("SELECT * FROM $datatable") or die(mysql_error()); ?> <table border="1"> <tr> <th>Date</th> <th>Dose</th> <th>INR</th> <th>Notes</th> <th>Script</th> <th>Action</th> <th></th> </tr> <?php while ($row = mysql_fetch_array($result)) { ?> <tr> <td><?=$row["date"]?></td> <td><?=$row["dose"]?> mg.</td> <td><?=$row["inr"]?></td> <td><?=$row["notes"]?></td> <td><?=$row["script"]?></td> <td>Edit | Delete</td> </tr> <?php } ?> </table>Visually speaking, I think that's a big improvement upon what you have now. 
    PHP 5.6 is dead. Time to upgrade: 7.1 if you can, 7.0 at a minimum. Better to do it now than have to deal with it later.
  9. requinix's post in PHP issues with mysqli extension and PDO, pdo_mysql extensions was marked as the answer   
    You have 7.0 installed but CentOS has 7.1 in their repos. Try

    yum install php70w-mysqliAs for multiple versions, don't. Until you understand how your OS does packaging, use whatever version they provide. That probably means uninstalling PHP 5.whatever and 7.0 you have now and installing 7.1 instead.
  10. requinix's post in php, ajax, sessions and security was marked as the answer   
    Sessions are managed with cookies and the browser will make that all work automatically with the AJAX.
     
    I wouldn't expect that you'd need to worry about CSRFs for this. It's not like the queries are performing any actions - what CSRFs are really about. But if you still need to have it then pass the token in the AJAX request; how you do that varies but probably means putting the token into the page with your PHP and appending it to the AJAX URL or including it in the request data.
  11. requinix's post in data integrity of data stored using drop down lists. was marked as the answer   
    User ID? I didn't say anything about a user ID...
     

    SELECT whatever you want if anything at all FROM countries co JOIN states st ON co.id = st.country JOIN cities ct ON st.id = ct.state JOIN pins p ON ct.id = p.city WHERE co.id = ? AND st.id = ? AND ct.id = ? AND p.id = ?The query will only return a result if the four IDs exist and they are related to each other.
  12. requinix's post in Search function only return values with exact match was marked as the answer   
    Case-sensitivity in MySQL is dictated by the collation used.
     
    All of yours are

    COLLATE utf8_binutf8_bin, which means UTF-8 (good) with binary comparisons (bad). Your table has that as a default. Your database probably has it as a default too. 
    Change everything that you want to be case-insensitive to use a case-insensitive collation. Do

    SHOW COLLATION WHERE Charset = 'utf8';and pick one of the *_ci collations by reading the names and using common sense. If you're not confident in your choice, post the list. 
    Then use ALTER TABLE statements to change the columns. ALTER TABLE can also do the table's default collation and ALTER DATABASE will do the database's.
  13. requinix's post in Need help in split function utf8 string was marked as the answer   
    Seems .split is not Unicode safe. Seems like ECMAScript says it should be...
     
    Use .match(RegExp) instead. With the /u flag it will match every Unicode character in the string.

    numbers = what.match(/./gu);
  14. requinix's post in Is there any downsides to using this method to randomly select a user id from a table? was marked as the answer   
    Yes, it will be slow.
     
    If you only want one then do

    SELECT COUNT(1) FROM users WHERE active = :active SELECT user_id FROM users WHERE active = :active LIMIT x, 1where "x" is the value from the first query.
  15. requinix's post in How to validate html input strings was marked as the answer   
    The embed snippets all look the same. Just ask the user for the URL to the video, parse_url() it until you reach the video ID, and make the snippet yourself.
  16. requinix's post in Should exceptions ever be thrown with optional second argument for code? was marked as the answer   
    Throwable is an interface with a getCode method. Exception implements Throwable. That's why there are two.
     
    If you have a numeric code to include there then you should use it. Personally I've haven't found it to be useful.
  17. requinix's post in SimpleXMLElement error loading string was marked as the answer   
    Stop touching the raw XML. Leave it alone. If you want to fix the name and... whatever you're doing to the plot... then do it after the XML has been loaded and do it to those variables you're using.

    $data = get_url('http://'.$API_SERVER.'./channels.php?token='.$API_TOKEN); header("Content-Type: text/plain"); echo $data; exit; $API_CHANNELS = simplexml_load_string($data); $title = (string)$API_CHANNEL->name; $title = preg_replace('#(.*?) \(.*?\)#s', '$1', $title);If you do what I said there then you won't see M3U stuff and you will see XML. I'll ask again: what XML do you see?
  18. requinix's post in Following my threads was marked as the answer   
    Easiest way is to go to your profile and use the Topics (topics you've started) and Posts (replies you've made) links in the navigation bit on the left.
  19. requinix's post in Configure Multiple MTAs for Email sender Script using Mail function was marked as the answer   
    You can't "assign" an IP address to an email. What you do is have each of those servers configured to send email, which means installing appropriate MTA software and then doing whatever it takes to get the email message to those servers to be sent.
     
    The MTA software is basically up to you. Postfix and EXIM are two of the big ones that can do it.
     
    The other half depends - you haven't really given any requirements besides "send from multiple servers"...
     
    You mentioned MySQL? And you're posting on a PHP forum? You can set up a PHP-based cronjob on each server that scans the database for emails that it should send and then sends them.

    <?php foreach (/* get list of emails from the database */ as $email) { mail($email["to"], $email["subject"], $mail["message"], $mail["headers"]); }as a very basic example. More appropriate would be to install something like PHPMailer and use that to send the emails instead of mail(), because it can do a better job of it. 
     
    But this whole thing is weird. Why bother with assigning specific servers? If they're all sending on behalf of the same domain then it doesn't really matter.
  20. requinix's post in Why are the informations about a user in reverse order? was marked as the answer   
    Because I thought the site should participate in today's events. I'll put it back tomorrow unless too many people complain about it.
     
    [Apr 2 edit] I'm not sure anybody noticed the rotating buttons
     
    [Apr 3 edit] And I don't think anyone saw I renamed the PHP forum to "PHP Coding Halp". I, for one, totally forgot about it until now
  21. requinix's post in Generating a unique key structure from JSON was marked as the answer   
    function parse_structure($source, $output = null) {
    if (is_object($source)) {
    // objects are easy: recurse over all the properties
    $output || $output = [];
    foreach (get_object_vars($source) as $key => $value) {
    $output[$key] = parse_structure($value, isset($output[$key]) ? $output[$key] : null);
    }
    return $output;
    } else if (is_array($source)) {
    // arrays should contain all of the same data type
    $output || $output = [];
    if (is_scalar($source[0])) {
    // foreach on the array would be wasteful - just check the first item
    isset($output[0]) || $output[0] = gettype($source[0]);
    } else {
    // recurse over all the items and put the merged structure into [0]
    isset($output[0]) || $output[0] = [];
    foreach ($source as $arrayitem) {
    $output[0] = parse_structure($arrayitem, $output[0]);
    }
    }
    return $output;
    } else {
    // if $output already has a type then keep it
    return $output ?: gettype($source);
    }
    } $output = parse_structure(json_decode($json) /* decode with objects */);
  22. requinix's post in cURL login was marked as the answer   
    Do you understand what that 503 block of code does? It will prohibit the code from moving any further. I'm not sure if you need ?curl detection at all (I wouldn't think so from what you're describing) so I'd say to get rid of that whole thing.
     
    Otherwise, if you're sure you have the right form fields then the rest seems correct to me.
  23. requinix's post in CURLOPT_URL Formatting with "()" was marked as the answer   
    Construct a valid URL with something like http_build_query:

    curl_setopt($ch, CURLOPT_URL, 'https://10.10.10.10/vmrest/users?' . http_build_query(['query' => '(dtmfaccessid startswith +1214820)']));
  24. requinix's post in DOMdocument :: getElementsByTagName :: Query was marked as the answer   
    That would be much better.
     
    Templating has two approaches:
    1. Textual. Do stuff like {{token}} and use text replacement. Very powerful but it's easy to make mistakes and create invalid HTML.

    (self-closing tags are deprecated in HTML, by the way) 2. Syntactic. XSLT is an example of this. Uses the syntax of the HTML itself. Has some restrictions, which you can workaround with added complexity, but is always valid markup. Might look like

    #1 is what just about everybody does. #2 is a dated approach that is mostly suited to specific circumstances.
  25. requinix's post in Can not login in AWS putty... was marked as the answer   
    Check the Inbound rules to make sure there's something for HTTP (and HTTPS if you need that) for your IP address.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.