Jump to content

gizmola

Administrators
  • Posts

    5,878
  • Joined

  • Last visited

  • Days Won

    139

Community Answers

  1. gizmola's post in Wordpress cross-plugin refering was marked as the answer   
    My educated guess is that requinix is right.   I also think the code snippets reflect that the order object should be available in plugin2 via $this->object.
    So I would suggest you try adding this code to plugin2
    $this->placeholders['{order_service_time}'] = get_post_meta( $this->object->get_id(), '_wfs_service_time', true);
  2. gizmola's post in using array_diff on 2 arrays was marked as the answer   
    He literally gave you code that works, only missing a semicolon:
    $result = array_udiff($table, $newdata, fn($a, $b) => $a <=> $b); var_dump($result); If you are familiar with Javascript ES6, it is a similar type of anonymous function shorthand, equivalent to this:
    $result = array_udiff($table, $newdata, function($a, $b) { return $a <=> $b; }); You need to have a current supported PHP version ( >= 7.4) although the syntax I showed works with any version 7.x or >
    The "arrow function" version was introduced with php 7.4
  3. gizmola's post in When user tries to download .iso, tar.gz, or .deb from my website php is opening the file instead of downloading it was marked as the answer   
    One pro tip:
    Never have a php end tag (ie. ?> ) in your scripts.  It is not needed, and can lead to bugs like this  remove all php end tags whenever they are the last thing in a .php file.
    You can start with looking at PSR-1, and perhaps graduate to PSR-12 if you are looking for some standards to help guide you.
  4. gizmola's post in Cant find the old thread about wrapper/container (CSS2-3). was marked as the answer   
    I don't have any specific recollection of such a thing, but a lot of things have changed in the css world, most notably the standardization of flexbox and grid that make older techniques and tricks of css layout obsolete.  You just don't need those things anymore when flexbox or grid can take care of your layout needs with simple, consistent and easy to understand syntax.  
    There was a time when you needed to know the ins and outs of floats and clear fix, and other arcane tricks of css, but that's basically obsolete knowledge.  People also use to use tables inside tables inside tables to get their "pixel perfect" layouts, but that also has given way to a focus on creating layouts that adapt from desktop to mobile.  
     
    This guy (Kevin Powell) has become well known in the css/web design world, and he really knows his stuff.  This video covers flexbox.  If you work through the examples with him, you will learn what you need.  He also has a corresponding Grid video.
     
    If you want something more interactive, lots of people love Scrimba, and in particular Per Borgen, who is one of the Scrimba founders.  He happens to have a free scrimba course covering grid and flexbox, so that is another way you can learn flexbox, if you want something more interactive.  The free Scrimba Grid/Flexbox course is here:   https://scrimba.com/learn/cssgrid
     
     
  5. gizmola's post in How to override trait methods from another trait? was marked as the answer   
    In the PHP manual section Traits section.
    You have 2 options:
    Alias the conflicting method so it doesn't conflict Specify an "insteadof" method statement in the use section of the class
  6. gizmola's post in PHP Variable Variables & Loops with Forms was marked as the answer   
    No worries - we all agree on using PDO so that is a good investment of your time.
    If you want to goto a new page that just has the signup form, then have all the buttons in the game list inside anchor tags.  Then you can just pass the game_name as a url parameter to the page that generates the form.  The signup page can just set the hidden game attribute using the $_GET parameter.
    I personally would opt for the usability of not having a separate signup form, but if it's simpler for you to go from a->b->c and get things working that way, do that then.  It makes the javascript event stuff simpler, although you will definitely need to learn that sooner than later.  I like Scrimba as a platform to learn html/css/javascript concepts, although there are many other freemium platforms.  Freecodecamp also does a good job.
    Simpler solution for you, again rendered buttons inside your foreach loop that lists the games (ie. one button per game listed):
    echo "<a href='signup.php?game_name=\"{$game['game_name']}\"'><button class='btn_game_signup'>Signup for this game</button></a>"; Clicking on the signup page will send a GET request to your signup.php script, which will include the $_GET parameter of 'game_name'.
    Then you just set the hidden form attribute of that page in your signup.php script to the value of $_GET['game_name'].
  7. gizmola's post in PHP+MySQL Unique Article Styling was marked as the answer   
    Yes there is a way to do this, but it involves a solid foundational understanding of relational database design concepts and how to write code.  It would be a significant amount of work for a professional programmer.  An alternative to reinventing the wheel from scratch would be to use a php based CMS like Drupal or Bolt  These projects start you out with a lot of functionality and systems that already have templating, much of which can be tweaked without programming.  They also have "taxonomies" which are extensible.  You can utilize existing templates to learn how templating works, and how to customize them without already being an advanced developer.
    Not to sound pessimistic here, but creating a blog database is not difficult at all, for something simple, but you still need to know what you are doing.  With that said, even though it might be fairly simple to do so, providing you a custom CMS database structure and instructions on how to create different templates against it is the topic of many an article, book and online course.   If you have a better idea of what approach you might take, people can provide you better advice.
  8. gizmola's post in Widespread Exploitation of Unauthenticated RCE in Apache Log4j (CVE-2021-44228) was marked as the answer   
    To be absolutely clear:  The Apache HTTP server that is commonly used in the AMP,LAMP, MAMP,WAMP stack, is not java based, does not integrate or use any java components, and is not at risk.
    This is a common enough misconception, in regards to Apache vs the Apache Software Foundation.
    The Apache webserver is one of the oldest Free Open Source web servers.  For the record it is written in c.  
    The original core contributors to the Apache HTTPD server, went on to create the Apache Software Foundation(ASF), which was incorporated as a non-profit to support the continued development of FOSS software.  The ASF also authored their own License, that is comparable and competes with the GPL and MIT licenses often used for FOSS.
    Over the years, the ASF has sponsored and brought many different types of software under their umbrella.  These projects benefit from funding and support services provided to them by the ASF.  At present there are something like 180+ individual software projects under the ASF umbrella.  Here is a list by programming language.
    Apache is a big player in the Java development space, with many important tools and java based projects under their umbrella.  Log4j is just one of these projects, but it is often integrated into other java based software and systems via their java logging project.  
    Here's one quote to help understand the potential scope of the problem:
    Anyone running an Enterprise or Java based system is a potential target, but there are also many pieces of java based software that are used by companies for the services they provide.
    For example, note the mention above of "Solr" which is a popular full text indexing engine that many sites who aren't otherwise java based, may use.  Apache also provides the Lucene engine, which is also Java based.  Basically if you are running any sort of java based software server, there is a good chance that the log4j vulnerability is a concern.  
    For any company with sysadmin/devops/developers with a degree of competency, the log4j exploit vector can be quickly and easily closed using several techniques that vary by log4j version and environment.  The problem is that there are many packages of java based software like Lucene & Solr, that the users may not realize are java based.  They may come with a packaged JRE, or be running via an independently installed JVM.
    The actual exploit requires crafted input that gets logged containing instructions which trigger java JNDI to download and run remote code.  So it's a "remote code exploit". 
    If a server with a vulnerable installation or version of log4j doesn't have some sort of publically available listener, there isn't a vector for exploitation.  For example, depending on the integration, your Solr or Lucene engine might not be at risk.  It could depend on how or what you are indexing, and where the data being indexed comes from.
    Many people are running java based editors like Eclipse or Netbeans, or any of the Jetbrains editors, in particular the very popular PHPStorm.  What does Jetbrains have to say about this?  https://blog.jetbrains.com/blog/2021/12/13/log4j-vulnerability-and-jetbrains-products-and-services/
    I hope this helps clarify some things for people.
  9. gizmola's post in Cookies and checkbox was marked as the answer   
    Yes, although again the UI is incorrect.  Checkboxes are not mutually exclusive.  So you are just doing this UI in the wrong way.  Your form code is also wrong. 
    You have a label that is hardwired to dark, yet you have variables toggling the value from light to dark.  When a form is rendered, it should always be checked, because you are using the checkbox to toggle from light to dark.   This means that a value for the checkbox will never be set in the way you using this.  A newly rendered form will always have a value (light or dark) and will be checked.  Once you uncheck it, you'll submit the form, and the checkbox will not be passed.
    However, to your question, you have 2 choices:
    Write a little js that checks the check/uncheck state and sets a hidden element, which you use on submit Just have php check for isset() or use array_key_exists.  If ! isset() then it was unchecked.  You want to check the request method to make sure the form was actually posted. Not that I'd advise this, but in this case, and again you have to be logical and clear, if the form is submitted, then you are looking to toggle the value.  
    <php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $mode = empty($_COOKIE['mode']) ? 'Light' : $_COOKIE['mode']; //Toggle it $mode = ($mode == 'Light') ? 'Dark' : 'Light'; setCookie('mode', $mode); } else { $mode = empty($_COOKIE['mode']) ? 'Light' : $_COOKIE['mode']; setCookie('mode', $mode); } // Now can use $mode to render ?> <input type="checkbox" onChange="this.form.submit()" value="<?= $mode ?>" name="mode" checked> <label for="mode"><?php echo "$mode Mode"; ?><label><br>  
    Again when you think about it, the actual value of the checkbox is irrelevant as you are really going to toggle the cookie value on any submit.
     
    The other option is to use some js.  Probably what I would do is use a hidden field, so that you could rely on the submission, but then when you have a cookie you are also relying on, this whole scheme makes less sense
     
    Something like this could work:
    <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $mode = empty($_COOKIE['mode']) ? 'Light' : $_COOKIE['mode']; //Toggle it $mode = ($mode == 'Light') ? 'Dark' : 'Light'; setCookie('mode', $mode); } else { $mode = empty($_COOKIE['mode']) ? 'Light' : $_COOKIE['mode']; setCookie('mode', $mode); } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Form Test</title> <script type="text/javascript"> function toggleMode() { let cb = document.getElementById("cbmode"); let newMode = document.getElementById("new_mode"); newMode.value = (cb.value == "Light") ? "Dark" : "Light"; console.log(newMode.value); document.getElementById("modeForm").submit(); } </script> </head> <body> <form id="modeForm" method="post"> <input type="checkbox" onChange="toggleMode()" value="<?= $mode ?>" id="cbmode" name="cbmode" checked> <label for="cbmode"><?php echo "$mode mode"; ?></label> <input id="new_mode" type="hidden" value="<?= $mode ?>"> </form> </body> </html>  
     
  10. gizmola's post in Only one select statment instead of two was marked as the answer   
    There is no way to combine the queries,  other than to union them, and that has essentially no significant value to you.  You can write the count query in a slightly more compact manner if you like:
    $total_recs = $conn->query("SELECT count(*) FROM table1")->fetchColumn(); Your pagination limit variables should use bind variables:
    // You'll set your $limit and offset variables from your pagination calculation $res = $conn->prepare("SELECT * FROM users LIMIT ?,?"); $res->execute([$limit,$offset]); $data = $res->fetchAll();  
  11. gizmola's post in Undefined Index if ID added to text field was marked as the answer   
    An undefined index occurs when you try and reference a key in an array that doesn't exist. Based on the info you provide that was the attempted referencing of "$_POST['company_address']". Clearly, prior to the api actually returning something, that variable didn't exist in the POST data.
     
    You can improve your error checking by using functions like array_key_exists or trying:
     

    if (isset($POST['some_key'])) { } else { // Deal with error, it's not going to work }
  12. gizmola's post in ANALYZE/ OPTIMISE in MYSQL was marked as the answer   
    The answer really depends on the engine, but the basic idea is that analyze looks at key distribution in the table and updates some statistics in the data dictionary so that the query optimizer can make informed decisions in regards to when it should use an index or ignore it.
     
    Optimize is basically analyze+ some table restructuring, but this again is highly dependent on the storage engine.  Innodb doesn't typically need analyze, as it has statistical updates baked in.  By the same token the way data is stored in innodb vs myisam is completely different, so there's literally nothing common between engines, and that certainly is true of the other engines that have come along in Maria etc.
  13. gizmola's post in same table delete and insert was marked as the answer   
    Yes you can enclose whatever you want in a transaction.  That is the point of them -- and they are of course not limited to one table.  You could insert in table A, Delete from Table B, and update table C, all inside one transaction.
     
    The important thing to know is that with mysql your tables need to be of an engine type that supports transactions.  Most people have traditionally used Innodb engine tables for this requirement.
×
×
  • 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.