I have a project with a forum section which i got most of the code from chipmunk forums because im definatly not good enough to code the whole thing myself.
The code has used the die function a lot to kill the script if the user is not aloud to post in a certain section or so on. Functionality wise it works great. The only thing i dont like is it cuts off the footer.
Is there a way to use die to kill certain parts of code and not others, or kill only the php and not the html? It would actually work great if someone knows a way to make it stop running if statements and run everything else...
Thank you in advance for all replies. I gladly give talkputation... even though i think i can only give one point lol.
die() is generally used in cases where an error occurs. If I were running a mysql query I would use die() if that query failed. Using it in cases that are not necissarily errors might not be good practice. If your footer is stored in a seperate file (ie footer.php) you can try to do something like this:
Here is the code for replying to a post... i deleted a lot of unessisary stuff that doesnt need to be posted cause the file is pretty long. Basically i just posted a bunch of the if statements with the die function in it. I think you should be able to guess what they do.
PHP Code:
if(!isset($_GET['forumID'])) { die("<table><tr><td><center>ERROR: No Forum Parent Selected</center></td></tr></table>"); } if($checkip3) { die("<table><tr><td><center>Your IP address has been banned from posting.</center></td></tr></table>"); } if($isthreadlocked3['locked']=='1') { die("<table><tr><td><center>This thread is locked. Posting is no longer aloud.</center></td></tr></table>"); } if($getid3[lastposttime]>$nowtime-30) { die("<table><tr><td><center>You may post only once every 30 seconds</center></td></tr></table>"); } if($getforuminfo3[permission_reply]>$getid3[status]) { die("<table><tr><td><center>You do not have sufficient privileges to post in this forum</center></td></tr></table>"); } if($getid3[banned]=="Yes") { die("<table><tr><td><center>You have been banned from posting.</center></td></tr></table>"); }
So it checks all of the if statement before it displays the form to reply, and if any of them turn out true it just kills the script before the table to reply is displayed.
Basically i would like to display the message but at the same time i would like to display the footer at the bottom.
My recommendation would be to take the code for your footer and place it in a seperate file and then before each die statement say include('footer.php') like I showed above. This might not be the best solution, but I'm pretty sure it will work and it should be quick to implement. Something like a footer should be stored in a seperate file anyways as it makes your pages easier to manage.
Yeah you could do that. Setup a generic error.php page that accepts error codes as URL parameters (ie error.php?error=1) then display the proper error message by saying something like
The error page should include both your header and footer. It would cost a bit more in bandwidth and load time, but it is a more appropriate solution to your problem I think.