Jump to content

OriginalMacBabe

Members
  • Posts

    13
  • Joined

  • Last visited

    Never

Posts posted by OriginalMacBabe

  1. My response was specifically about how you check if the value is set on subsequent pages using the session variable. If you have made a determination as to whether the checkbox was set or not, you should set the session variable to either true/false instead of relying upon isset() of the session variable. This is just good coding practice.

    Yes, thank you.  That is exactly how I read it. 

     

    I already stated that the 2nd to last example you posted should work. You state this is a "theoretical exercise", but I would counter this is a useless exercise.

    I disagree.  In learning how to code, I find it useful to understand the limits.  It doesn't mean I would necessarily use it in its extreme iterations.  Nonetheless, practicing the application of correct syntax options is never a "useless exercise" IMHO, and is particularly useful for facilitating/avoiding future debugging. Those slips of semicolons, for example, are often pretty hard to spot, especially when one's understanding of where they are/not supposed to be used is insufficiently comprehensive.

     

    On a side note, if you order things carefully you can probably perform a session assignment and header() call in the same piece of a ternary operator. ... However you'll probably agree that this does nothing to help with readability and maintainability of the code.

    Thank you for the explanation of isset() and empty().  I will implement as suggested.  It'll take me a bit to decipher the example you provided - proving your point exactly.

     

    I did some testing and setting a session variable after the redirect seems to work. But, any variable setting or output statements (e.g. echo) after a redirect have no effect on the output to the user.

    So in this particular application, it would be fine since the session variable will only be evaluated on subsequent page calls.  That is good to know.  But, are you saying that for best practice, I should add exit() after the session_write_close()?

     

  2. Ok.  So back to the first iteration.  Yes, clearly, the boolean option is a better choice. And, I'm getting the message that I cannot reduce the process to a single ternary operation, although no one has explicitly answered this question.

     

    @rwwd: I agree that checking to see if the form has been properly submitted is generally a good idea, but I thought that since the only item in the form is the checkbox and the form's action consists entirely of the code discussed here (i.e., there is nothing that might be of advantage to a bot) that in this case the test was superfluous.

     

    However, your comment does leave me a bit confused.  I thought that if the checkbox had not been checked it would not be set.  So what is the difference between the variable being set and it being empty?

  3. Thank you for that.  I, too, realize that the last version was extreme, but as I said, it had become a theoretical exercise by that point - the ternary operator is such an elegant solution. Despite the readability issue, did I actually get the code right?  Would it work?

     

    Moving along to the practical, I also think the second last version is more readable, I just wasn't sure I could put the two commands into the statement.

     

    Because the form consists only of the single checkbox and the submit button, if it is not checked, it is not set (as far as I understand).  Thus, I thought using isset would be the most efficient test. 

     

    In the first condensed version, the first line could reasonably be changed (and shortened) to obtain a boolean result:

    $_SESSION['Consented'] = isset($_POST['ConsentCheckbox']);

     

    Sticking with the second condensed (second last) version, am I reading you correctly when you say I should remove the quotes from the assignment and change it to this (remembering that this code represents the form's action)?

    <?php session_start();
            isset($_POST['ConsentCheckbox']) ? $_SESSION['Consented']=true; header('Location: http://www.mySite.com/welcome') :  header('Location: http://www.mySite/errors/NotConsented');
    session_write_close(); ?>

     

    And follow up with this simple test at the top of subsequent pages on the site (actually a template):

     

    <?php session start();
         if($_SESSION['Consented'] !=== true) header('Location: http://mySite.com');
    ?> 
    

     

    Thanx

  4. Please bear with me in the following exercise.  I'm fairly new to php (but not coding) and am trying to push my skills for the future, so I'm going beyond a problem for which I already have a solution to see if I understand the coding principles correctly.

     

    I am trying to understand just how far I can push the ternary operator shorthand.  I know how to do simple tasks, but I'm wondering if I can perform more than one command in the same statement? I'm a little confused because I'd like to both set a session variable and do a page redirect and I have a feeling that the order of these operations matters, so all I've done here may be incorrect.

     

    I have a form on the front page of my site that includes a checkbox that is required in order to enter the site.  The form action goes to a page where this is checked, a $_SESSION variable is set, and the page is redirected.  I am also not sure if I need the  session_write_close(); command, or if it is in the right place.  In the full length version, it could be moved into the set of commands following the IF.

     

    Here is the full length code that I would like to translate into shorthand:

     

    <?php session_start();
    if (isset($_POST['ConsentCheckbox'])
    {
    	$_SESSION['Consented'] = 'true';
    	header('Location: http://www.mySite/welcome');
    } else {
    	header('Location: http://www.mySite/errors/NotConsented');
    }
    session_write_close(); ?>

     

    The if/else conditions translate easily into these two lines of code, which could be one line if I didn't also need to set the session variable:

     

    <?php session_start();
            $_SESSION['Consented'] =  isset($_POST['ConsentCheckbox']) ? 'true' : 'false';
            header('Location: http://mySite.com/'.($_SESSION['Consented'] ? 'welcome' : 'error/NotConsented'));
    session_write_close(); ?>

     

    But, I really only need to set the $_SESSION variable if the $_POST['ConsentCheckbox'] is set.  This makes the test on the following pages of my site much simpler (where I test to see if the $_SESSION variable is set and then either redirect to the consent form or load the desired page). 

     

    SO, I'm wondering if it can be further condensed.  I've come up with these possibilities, but am not totally sure.  This has now become a theoretical exercise in coding for me.  Here is what I think is possible, but I have a couple of outstanding questions, so I'd like some input, please.

     

    I first translated it into the following.  I think this is correct, although I'm not sure if I need brackets around the multiple commands in the TRUE section.

     

    <?php session_start();
            isset($_POST['ConsentCheckbox']) ? $_SESSION['Consented']='true'; header('Location: http://www.mySite.com/welcome') :  header('Location: http://www.mySite/errors/NotConsented');
    session_write_close(); ?>

     

     

    Then, I wondered if I can even further condense it in the following way?  Here is where the order of the commands is likely to break the code - and I'm not entirely sure that my use of double quotes will suffice instead of escaping out the end bracket of the header() command:

     

    <?php session_start();
            header('Location: http://www.mySite.com/'.(isset($_Post['ConsentCheckbox']) ? "welcome')"; $_SESSION['Consented'] = 'true' : "errors/NotConsented')");
    session_write_close(); ?>

     

    Have I gone too far?

     

    Thanks

     

  5. Caveats duly noted ~ keep the included css as html inline styling. 

     

    This makes perfect sense since I am only planning on changing 2 lines of css that are browser dependent. 

     

    In fact, if this is the case, I only need to include ONE new css stylesheets as a result of the "IF" that holds the necessary browser dependent modifications of the otherwise already loaded stylesheet. 

     

    So, I think the final version of the code will read:

    <?php
    
    if((stripos($_SERVER[“$HTTP_USER_AGENT”], “opera”)) || (stripos($_SERVER[“$HTTP_USER_AGENT”], “safari”))) {
    include("operasafari.css");
    } else {
    }
    
    ?>

    Thanx

     

  6. Is it possible to test for more than one value in an "if" statement or must each desired value be checked individually with something like a "switch" or "if-else"?

     

    For example, if I'm checking $_SERVER["$HTTP_USER_AGENT"] and I am only interested in selecting for Safari or Opera and all the rest are "else" ...?

     

    BONUS question (for me ...)

     

    And, can I get a clear answer on the best checker for this: strpos(), strstr(), substr(), or match_preg()??

     

    Thank you

     

    OMB

  7. Ok, no wonder I was having trouble finding it then, I really thought I was losing it.  Why is it that things that seem so logically simple often turn out to be "impossible"?

     

    I am not so concerned about people bypassing the system ... if someone wants to look at my pages that badly, they can.  I would, however, like to make sure that they have at least seen the screening page.  Even if they don't actually click the "agree" button, at least I know that they have seen it, so any attempt they make to circumvent the system and still enter will be the equivalent of having clicked the "accept" button ~ this is an issue of informed consent; it would be hard to argue that they didn't know where they were going if they went to the trouble of deliberately foiling the system.

     

    I'm sure there will be some shmo whose life is filled with the joy of hacking through others' intentions, but I honestly believe that most people would rather just click the button than go to the trouble of circumventing the process.

     

    I'll give what you suggest a try ~ I think I understand it.

     

    I hadn't thought of using session information.  Is there a way to just check if the screen page has been loaded in a session?  If so, this will suffice.  Then I could just check to see if that variable is true or if the refere page is some other page in the site, either of which will be sufficient to have the next desired page load??

     

    Thank you

     

    OMB

  8. Sorry, but I've gone blind from trying to find the answer to what I know is a simple question.  ???

     

    I want all visitors to my site to have to agree to enter from a front screening page by clicking a specific link (akin to how we agree to the terms of use for a forum.)  I want to assign a value=true to a constant when this is done.

     

    I will then put a query at the top of every other page in the site to see if this variable is set.  If it is I want the page to continue to load.  If not, I will redirect them to the screening page.

     

    I found the redirect <?php header ('Location: http://mysite.com/screening-page.php'); ?> and I can use the isset function.

     

    How do I set the constant?

     

    Thank you

     

    OMB

     

×
×
  • 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.