Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    Firepages is right, logic like that is behind a lot of your problems. Consider your idea: You set up everything like it's an error, and only do the final outcomes depending on if it actually is. Hopefully, for the vast majority of times, your result will not be an error. You will not need an error set up in these cases, but you will still burdening your script with the assumption that it will be. Meaning this script could be running a lot smoother for pages not generating an error. You shouldn't need to assume anything in a good script.

    It should be this simple..

    In a main file:
    PHP Code:


      
    if( !isset($_GET['section']) || strlen($_GET['section']) == 0) { 

      
    $_SESSION['Error'] = 2422// Some error number, or something. 

       
    header("Location: "BASE_URL ."/account/results.php");  } 
    In the results file:
    PHP Code:


      
    // Check if an error has been set.
      
    if( isset($_SESSION['Error'])) && $_SESSION['Error'] != 0) {

         switch(
    $_SESSION['Error']){ // Find what error it is.

          
    case 2422: echo "Error: No section defined in "$_ENV['REQUEST_URI']; break;

      
    // Add new cases for any self-defined error you may use.

          
    default: echo "No error was defined."; break;

      } } 
    1 ). You wouldn't echo the message like that, that's just for simple example. If it was me I would call a function there that would then handle logging and emailing. Well, if it was me I wouldn't be doing anything like this, lol.

    2 ). Your calculation on $_GET['section'] is completely ineffective, and wouldn't trigger your error handling if it was a empty string (but still set), or actually existed. Meaning your check is doing next to nothing to actually catch what you're trying to catch. I could change your url to /your_file.php?section=, or even /your_file.php?section=anything and your check would do nothing to stop that. Depending on how you're using $_GET['section'] after this check you may have a massive security hole there.

    3 ). If I recall right, using header(Location: ) will return a redirect response code, and will preserve the original page location in the environment variable $_ENV['REQUEST_URI']. You should be able to use that instead of remembering $_SERVER['SCRIPT_NAME'] from the last page.

    4 ). You are doing a lot of writing and asking a lot of questions. This isn't necessarily bad, but you're making it hard for anyone to help you. If you are serious about fixing your problems you should consider taking some of the previous advice to heart, and going to the PHP documentation or Google and doing a bit of reading. A little more effort to understand what is being suggested could go a long way, and save you more time in the end.
    Last edited by Custard7A; 11-14-2012 at 12:42 PM. Reason: No brackets on switch. Whacked this together bit hastly.

  2. #17
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,075
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Custard7A View Post
    Firepages is right, logic like that is behind a lot of your problems. Consider your idea: You set up everything like it's an error, and only do the final outcomes depending on if it actually is. Hopefully, for the vast majority of times, your result will not be an error.
    Not so.

    I created a script called "results.php" to display a "result" or "outcome" message on the screen based on what a User did.

    This includes "Error" messages like...
    Comment Not Found
    The Comment you chose was not found.
    Please try again. (2328)

    <Return to Article>
    And "Success" messages like...
    Comments Changed
    Your article comments have been changed. (2330)

    <Return to Article>

    However, I will agree that the majority of my messages tend to deal more with when something goes wrong.



    You will not need an error set up in these cases, but you will still burdening your script with the assumption that it will be. Meaning this script could be running a lot smoother for pages not generating an error. You shouldn't need to assume anything in a good script.
    Maybe part of the problem here is "What constitutes an 'Error'?"

    To me, an "Error" is anytime the User doesn't get their expected outcome, or when the script fails.

    Typically my scripts deal with the following "errors"...

    - No Value (i.e. NULL) was passed
    - No record can be found for the submitted Value
    - The Value has an Invalid Format
    - User is trying to do something that is not allowed

    All of my "Error-Handling" is not dealing with Compile or Fatal Errors, and in fact, I'm not sure I know how to do that?!

    What I am calling "Error-Handling" involves going through every branch of my code and making sure I have some "Results/Outcome" message for *every* scenario possible, so that my code *always* ends gracefully with some Custom Message.

    If my code is invalid, then it wouldn't run, so I guess I catch "Compile" errors during testing?

    And maybe I am being naive, but I guess I assume that by going through every branch in my code, combined with doing obvious things like checking for Nulls and checking for proper Data-Types, that my code has a branch - and message too - to handle everything that can happen.

    Does that make sense?

    (I was hoping that the code I posted before would give all of you a good idea of how my entire scripts look...)


    It should be this simple..

    In a main file:
    PHP Code:


      
    if( !isset($_GET['section']) || strlen($_GET['section']) == 0) { 

      
    $_SESSION['Error'] = 2422// Some error number, or something. 

       
    header("Location: "BASE_URL ."/account/results.php");  } 
    In the results file:
    PHP Code:
      // Check if an error has been set.
      
    if( isset($_SESSION['Error'])) && $_SESSION['Error'] != 0) {

         switch(
    $_SESSION['Error']){ // Find what error it is.

          
    case 2422: echo "Error: No section defined in "$_ENV['REQUEST_URI']; break;

      
    // Add new cases for any self-defined error you may use.

          
    default: echo "No error was defined."; break;

      } } 
    But that is almost identical to what I have?!


    1 ). You wouldn't echo the message like that, that's just for simple example. If it was me I would call a function there that would then handle logging and emailing. Well, if it was me I wouldn't be doing anything like this, lol.
    Then what would you do??


    4 ). You are doing a lot of writing and asking a lot of questions. This isn't necessarily bad, but you're making it hard for anyone to help you. If you are serious about fixing your problems you should consider taking some of the previous advice to heart, and going to the PHP documentation or Google and doing a bit of reading. A little more effort to understand what is being suggested could go a long way, and save you more time in the end.
    And I said I didn't understand what was suggested above, so RE-reading the PHP Manual won't do a whole lot of good...


    Debbie

  3. #18
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    Quote Originally Posted by doubledee View Post
    But that is almost identical to what I have?!
    Kinda, but most importantly you're not checking if an error has occurred or not at the results.php.

    Quote Originally Posted by doubledee View Post
    Then what would you do??
    Perhaps I would send bad GET queries a HTTP 404, or 400; Script errors might get a HTTP 500. I could make use of existing database framework, I would be using OOP.. etc. It's not that important what I would do, since that doesn't make what you're doing wrong.

    Quote Originally Posted by doubledee View Post
    And I said I didn't understand what was suggested above, so RE-reading the PHP Manual won't do a whole lot of good...
    Yeah, the manual can be a bit harder to get your head around. Google can usually come up with some better entry-level tutorials though. I think my point is we're here to encourage and assist better web development, not to re-write people's scripts and send them on their way. There's enough of those requests from the Wordpress crowd, and it's actually counter-productive in the long run when they meet the same problem again.

  4. #19
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,075
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Custard7A View Post
    Yeah, the manual can be a bit harder to get your head around. Google can usually come up with some better entry-level tutorials though. I think my point is we're here to encourage and assist better web development, not to re-write people's scripts and send them on their way. There's enough of those requests from the Wordpress crowd, and it's actually counter-productive in the long run when they meet the same problem again.
    I never asked anyone to re-write my script.

    I did ask for help understanding what was being suggested, as well as asking several questions for clarification that people haven't responded back to though...

    There is a big difference between that and what you are implying...


    Debbie

  5. #20
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Quote Originally Posted by doubledee View Post
    But that is almost identical to what I have?!
    yes because you simply switched results to error , you need to set either and check both
    e.g.
    PHP Code:
       if (mysqli_stmt_affected_rows($stmt3)==1){
            
    // Insert Succeeded.
            
    $_SESSION['resultsCode']['result'] = 'COMMENT_MEMBER_COMMENT_ADDED_2052';

            
    // Set values for Success Message.
            
    $_SESSION['firstName'] = $firstName;
            
    $_SESSION['heading'] = $heading;

            
    // Notify Other Subscribed Members
            
    notifySubscribersOfNewComment($dbc$articleID$commentID$sessMemberID);

        }else{
            
    // Insert Failed.
            
    $_SESSION['resultsCode']['error'] = '2';
            
    // Set Error Source.
            
    $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

            
    // Redirect to Display Outcome.
            
    header("Location: " BASE_URL "/account/results.php"); 
        }
    // End of VERIFY INSERT. 
    & in results.php e.g.
    PHP Code:
    $errors=array(
     
    2=>array('msg'=>'you did this wrong','ID'=>'COMMENT_MEMBER_COMMENT_ADDED','gen_email'=>false),
     
    4=>array('msg'=>'you did that wrong','gen_email'=>false),
     
    8=>array('msg'=>'you did the other wrong','gen_email'=>true),
     
    //etc
    )

    if(
    $_SESSION['resultsCode']){
        if(
    $_SESSION['resultsCode']['result']){
             
    //do whatever you want to do here
        
    }
        if(
    $_SESSION['resultsCode']['error']){
             
    //its an error you may have other things to do now?
             //check $errors[$_SESSION['resultsCode']['error']]; for what
        
    }

    as for what is an error... whatever you class as an error but you posted here because you were getting emails for every member action, something I assume you want to avoid... the above is one (simplistic) way of modifying your existing code.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •