rickphp Posted April 10, 2009 Share Posted April 10, 2009 Hi, When the form has passed all validation I am using the die function to display a page saying the form has been sent etc. However only half the page loads, it seems that the footer file is not loaded although the header file is loaded. If i paste the contents of the footer file within the die command the page appears as normal. This is a temp fix though as this causes a problem because when I update my footer file I would need to keep manually updating my contact forms footer data. Is there a solution to this? Can i somehow include the footer file as part of the die command or something like this? Hope some one can help Thanks Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 die(include('footer.html')); Quote Link to comment Share on other sites More sharing options...
.josh Posted April 11, 2009 Share Posted April 11, 2009 so...why have the die at all? Or just do the footer.html include before it... Quote Link to comment Share on other sites More sharing options...
iarp Posted April 11, 2009 Share Posted April 11, 2009 I always thought you put "or die("Error")" after items to see whats wrong rather then wrapping it around potentially working items... isn't die() only activated on PHP errors Quote Link to comment Share on other sites More sharing options...
.josh Posted April 11, 2009 Share Posted April 11, 2009 no. that's just the most common (and wrong) use for it. die (and exit) just tell php to stop the script. You can optionally have it do something on exit by putting it as an argument. The idea is that if something goes wrong, stop everything and display an error. The reason why that's the wrong thing to do, is that simply stopping your script will more than likely give your user virtual blue balls. In other words, that is not a way for your system to gracefully handle errors. For instance, let's say you have an online store. It's a multi-step process. halfway through the process, some error happens. So instead of your script saying "oh crap, something happened, let's save what the user has done so far, redirect to something else, try something else, notify someone, etc..." it just dies. User is like "wtf..." and leaves. You just lost a sale and even worse, you have no idea why. Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted April 11, 2009 Share Posted April 11, 2009 die() and exit halts the script execution meaning that everything after such a statement doesn't get processed or outputted, even if it's just HTML. Generally, I wouldn't recommend using them except for debugging, etc. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 I f*cking love the die() function. I use it all the time - generally to display errors to the user if they try and access something they're not supposed to. rather than doing this: if($user == 'allowed') { //do whatever } else { echo 'denied'; } You can do it with much less, neater looking code like this: if($user != 'allowed') { die('denied'); } //rest of script Quote Link to comment Share on other sites More sharing options...
.josh Posted April 11, 2009 Share Posted April 11, 2009 You would abstract the error handling/logging into functions or a class or something and just invoke it. With your code, you have no idea what caused the error. Was it something with the script? Did the user cause it? On accident or purpose? It could be a legitimate bug in your system that caused your script to do that. User complains to you and you have nothing to look at, so you're stuck trying to reproduce the error with no info except for what the user happens to remember/tell you. Which, if you are still relying on just dumping a die statement to the screen like that, no offense, but you must not have much experience trying to get info out of users.. Same thing if user did it on purpose. Let's say he's trying to crack your system. You have no idea what exactly he's trying and where, because you are not creating any kind of paper trail with that. That paper trail can mean the difference between you finding a hole before he does and plugging it, and picking up the pieces from whatever havoc he caused, because he had all the time in the world to poke at your stuff and you were blissfully unaware. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 That was just an example, what I use is a function that I call whenever I need to display an error, which in turn dies, displaying the appropriate error message. I don't really see how you can display a more specific error message than one that's written specifically for that error. Quote Link to comment Share on other sites More sharing options...
.josh Posted April 11, 2009 Share Posted April 11, 2009 The point is not to just display an error message. The point is to log everything you can about what happened and where, and instead of just displaying an error message and halting the script completely, you do something else like redirect the user somewhere else, have your script try to complete some action a different way, etc... something other than just saying "you had an error" and then quitting. Going back to the online store: The goal is to get the user to buy something, right? Well if in the middle of shopping some error occurs, and all you have is a die('there was an error') ...what do you think the chances of that visit turning into a successful purchase is? I personally would immediately go somewhere else. How can I trust that your script won't somehow mess up my personal info, especially my cc info, if it's just dying on some random other error? And even if I somehow wasn't worried about that. What am I supposed to do next? Am I supposed to start over? What happened to the items in my shopping cart? Are they still there? Do I tell someone about it? Did my order go through or do I need to resend it? I don't want to get charged twice. There are a million wtf happens next questions, depending on what happened where, that simply dumping a "there was an error" and then quitting the script, just isn't good enough. Websites aren't built for the sake of being built. Not if you want to make money off it, anyways. So you can't just say oh well, something happened, display a message, the end. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 Yeah, but the only place I actually have errors is when the user is attempting something they shouldn't. My scripts work fine, I've never had an unintended error. And for all E_USER_ERRORs I have a custom error handler which logs the error and prompts the user to continue. I'm unsure as to what else I can do to improve error handling... Quote Link to comment Share on other sites More sharing options...
.josh Posted April 11, 2009 Share Posted April 11, 2009 Yeah, but the only place I actually have errors is when the user is attempting something they shouldn't. My scripts work fine, I've never had an unintended error. And for all E_USER_ERRORs I have a custom error handler which logs the error and prompts the user to continue. I'm unsure as to what else I can do to improve error handling... Okay well that's a step in the right direction. As far as whether you should use die for when users are attempting something they shouldn't...well it really depends on the site, how much money is being invested in it/flowing through it, as to how much effort you want to put into it. For instance, with a site that collects user's personal info, especially financial info like cc numbers etc.., I would never let the user know that they are trying to do something they shouldn't do. All you are doing is giving them a clue of where your script's boundaries are. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 Well, I'm 16 and I made my own forum/profile/music/blog site; I'm not really doing it for the money, and I don't collect anyone's bank account numbers But yeah, I see your point. Yeah, back on topic though, I think the best thing you could do is make a custom error function, with some of the elements that Crayon Violent suggested, such as a redirect or something. Or maybe say something like "There was a problem, can you please fill in these details again". Quote Link to comment Share on other sites More sharing options...
rickphp Posted April 11, 2009 Author Share Posted April 11, 2009 die(include('footer.php')); Works a treat, but I also need to include some text. I.e. Form subimitted bla bla bla. and then have the footer file included. Having two die functions doesnt work, is there another way of achieving this? die ("<br /><b>Thank you $name1.<br/ ><br />Your message has been received and will be answered shortly.</b><br /></div></div>"); die(include('footer.php')); Thanks Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 That's because die() causes the script to cease parsing. You can do this: echo 'text'; include('footer.php'); die(); Quote Link to comment Share on other sites More sharing options...
rickphp Posted April 11, 2009 Author Share Posted April 11, 2009 Thats worked perfectly! Job done. Now I've created myself another problem. Ideally if the user doesnt pass the form validation, i.e. they miss a required field I want it to display an error on a page without the form showing (like the thank you msg), I've done this, but when there is an error the page shows fine but when you try to go back it doesnt let me go back. I am using a php session to remember the contents of the entered fields, so I assume this is causing the issue. I have also noticed that when the form is sent, if you leave the page and go back to the page it shows the thank you page (until the session expires anyway). Is there a way to overcome this? Im using the following to retain the data: <?php session_start(); ?> if ($submit) { foreach($_POST as $key=>$value){ $_SESSION[$key]=$value; } <input class="form" name="name1" type="text" size="26" maxlength="20" value="<? print("$_SESSION[name1]");?>" /> Any further help would be appreciated. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 Why not display the thank you page only if the form has been submitted rather than if the session exists? And what do you mean by "doesn't go back"? Quote Link to comment Share on other sites More sharing options...
rickphp Posted April 11, 2009 Author Share Posted April 11, 2009 Well it says, error... bla bla bla.. and then theres a go back button, when you press that it seems to load the exact same page, i.e. the error... you cant get the form to re-appear. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 Is that not just because the session still exists? Like I said,you should display stuff depending on whether the form has been submitted, not whether the session exists. Quote Link to comment Share on other sites More sharing options...
rickphp Posted April 11, 2009 Author Share Posted April 11, 2009 I wasn't aware that I was doing that, any idea how I can do that? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 I don't know, I thought you said you were! You can do it like this: if(!isset($_POST['any_form_element'])) { echo '<form ...... } else { form validation...... } Quote Link to comment Share on other sites More sharing options...
rickphp Posted April 12, 2009 Author Share Posted April 12, 2009 I've decided having the error echoed is actually better for me. As for the thank you message showing up after the form was submitted when leaving the page and going back it was because i forgot to close the session, doh! Closing the session after succesful completion of the form has resolved the problem. Thank you for all your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.