SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Egypt
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can i stop the execution of the rest of the script

    Hello,
    I have a script thats saves the form data to a database but till now with no php validation

    What i want to do is to stop the execution of the rest of the script after finding that a field is not valid

    I thought about using if..else but I found that the script will loose much of its simplisity and will become more complicated and harder to edit

    So, What can i do to solve such problem?

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    exit();

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I use die(), but i don't know if theres a difference. To output a message after stopping the script:
    PHP Code:
    die("The script has stopped"); //or whatever message you want 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Egypt
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess that is why I did not find a solution when i googled as I am already using die()

    thank you arkinstall and Kailash Badu for your help it is really appreciated

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They are same except that i find the latter a bit ominous

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    die is an alias of exit. btw if you are not outputting a message you can leave off the ()

    PHP Code:
    exit;
    die;

    exit(
    'message');
    die(
    'message'); 
    Same Same.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would say that neither is a very elegant solution. Error handling can be a boring part of coding an app, but it can offer your users a better experience, if you handle your errors a little more gracefully.

    For validation, put together a function or class that does the validation and then returns a boolean to indicate valid or invalid with some way of indicating the fields that need correction. This could be done with a true/false return, and an array of fields populated by passing the array by reference when calling the function. Then do something like the following....

    PHP Code:
    // enter the post values, a set of rules for the required fields
    // and $errorfields array by reference that will be
    // populated with the names of problem fields
    $errorfields = array();
    if(!
    validate($_POST,$rules,$errorFields))
    {
        
    // send them back to the form with a message to display
        
    $msg 'The following fields are required. '.implode(', ',$errorfields);
       
    header('Location: form.php?msg='.urlencode($msg));
       exit();
    }
    else
    {
       
    // do your submission processing and do what you will with it.

    If you use die() or exit all by itself, all output ceases, including any further HTML markup that would normally preserve the look of your site. IMHO it's ugly, abrupt and not very user friendly. return them to the form preferably with what they filled in still there using session variables, tell them what's wrong and give them the chance to correct it.

  8. #8
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hammer65
    I would say that neither is a very elegant solution. Error handling can be a boring part of coding an app, but it can offer your users a better experience, if you handle your errors a little more gracefully.
    Very true. However, die() could well be pretty handy in some cases. For example, stop the execution after the redirect statement has been executed, or while debugging. Additionally, instead of a letting a method/function to handle an error, you should just make it delegate the error handling to main global script which usually is in better position to make application decisions.

  9. #9
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never said it wasn't useful for debugging, I was speaking of usability.

    instead of a letting a method/function to handle an error, you should just make it delegate the error handling to main global script which usually is in better position to make application decisions.
    I don't see anything wrong with calling a function or class method to process input and return a true false answer as to whether the submission is valid. If you write all of it into the global scope, it's tied that application and that page. A good validation class that can be fed a set of rules from a database for instance, for any form and perform validation, will work anywhere, for any form. Putting a lot of validation code directly in global scope just clutters up the main flow of the application.

    The global scope only needs to ask "is it valid?" if not handle the error, if it is, proceed to process the input.

  10. #10
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kailash Badu View Post
    Very true. However, die() could well be pretty handy in some cases. For example, stop the execution after the redirect statement has been executed, or while debugging.
    If you terminate the script and print messages while debugging you should instead get a proper development enviroment.

    Try Zend Studio, together with Zend Platform it has amazing debugging features.

  11. #11
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would agree to a point. i have Zend Studio and I like it, however personally I have gone without that sort of IDE for such a long time, that my debugging techniques are pretty rooted in the old way of doing things.

    Some coders prefer vim or something similar. Zend is also a little pricey for the high end version.

  12. #12
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I can agree that Zend Studio Professional is expensive if your not programming for a living. But if the same software can save you countless hours debugging, it all depends how much a hour of your time is worth.

    To do some efficient debugging you need Zend Studio Professional and Zend Platform, as it allows you to debug on an external server instead of the internal php engine.

    When you get used to it, setting breakpoints and watches allows you to finish debugging the problem faster than it would take you to add the "die, and echo statment and run the script once".
    Another feature which is nice with the debugger, is that you are able to change a variable on the fly while the script runs. Making it simple to provoke an error without changing your code.

  13. #13
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheRedDevil
    If you terminate the script and print messages while debugging you should instead get a proper development enviroment.
    I use DBG which easily lets me step in , over, and out of the code while also letting me set the breakpoints. I have used Xdebug but dumped it when couldn't find a nice client for it. but I absolutely hate Zend specially when it gets annoyingly slow at times. I use PhpEd instead, which is much much faster than Zend because it's written in C using native Win32 API.

  14. #14
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strange, I have heard that about Zend repeatedly and yet I have yet to experience it on XP or OS X. I have quite a bit of RAM in both machines, but I don't know if that is the issue with some or not.

    I love the code hints and autocomplete, even on my own functions and classes. I just haven't been able (mostly due to time) to get up and running with a modern debugger.

  15. #15
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using it in XP either. 1 GB RAM. Pentium 4 Processor 3.6 Ghz.

    PhpEd also has a autocomplete and it also lets you see your own functions and classes. On the top of that it lets you limit the scope of autocomplete to only those functions/variables/classes that have been included in the current page. Thus it doesn't just throw up all the classes/functions/vars in the entire project. It just lets you see what is relevant. (This feature was not available in Zend 4, I haven't checked the version 5). It also lets you extend the autocomplete to include PEAR and other library (which might be stored somewhere far away from web root in you file system) PhpEd comes with built-in debugger(DBG) which lets you do anything you'd expect of a modern debugger (including remote debugging and ability to change values in the fly). It also has embedded profiler, database client, FTP client, IE and Mozilla browser. It also has embedded CVS client but unfortunately doesn't have a SVN client (which Zend does). But it's lets you access other SVN client via shell access (just a right click away).

  16. #16
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had the same problem with Zend Studio, which is why I dropped it. I'm not a particularly fast typer, but when I can type faster than ZS can update the screen on my WinXP system with 1.5GB RAM and a 3GHz Pentium 4 processor then something is terribly, horribly wrong - I haven't been able to out-type my computer since I was using AppleWorks on my old Apple IIc Plus!!

    I'll have to give PhpEd a try - sounds like it does everything I need it to do, and if it can keep up with my mediocre typing then it may very well replace Notepad++ as my PHP editor of choice! I miss nothing more than the autocomplete and code hints that Zend Studio gave me, it just wasn't enough of a feature to make me tolerate the ridiculous slowness.
    PHP questions? RTFM
    MySQL questions? RTFM

  17. #17
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am currently recovering from a hand injury which happened to sync with my purchase of Zend, that probably explains why I didn't notice. I've got a ways to go before my WPM is back up to the way it used to be.

    I would have continued to use notepad++ but I really need the builtin FTP. Despite the danger of getting flamed, I'll mention that I used (and still use for templates) Dreamweaver for years. Just code view not in visual mode (except to find something in very large pages) mind you, and none of the code autogeneration stuff. The builtin FTP allowed me to not store so much on my local machine. I don't find the code view to be that bad, especially for HTML and CSS. I'm not the designer here however just the programmer.

  18. #18
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Egypt
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Since I have been using php for only a few weeks then i do not think i will go to complicated solutions at the moment but i will use hammer65 advice

    return them to the form preferably with what they filled in still there using session variables, tell them what's wrong and give them the chance to correct it.
    i do not know if it is only me but my browsers are already doing this (opera, IE, firefox, konqueror) so all i have to do is to give the users an output of all the errors made when submitting the form instead of one error at a time and give them a javascript link back to the form where the data they entered will still be there

    do you think that i am only dreaming or this can possibly happen


    on the other hand i am using easyeclipse for LAMP which i find comfortable and runs on 2.4 Ghz with 256MB ram without problems

    thank you all for your replies i never thought that error handling could be such a problem but you definitely changed my way of thinking

  19. #19
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just remember that Javascript form validation should be considered a user convenience not a security feature. All someone would have to do is not use your form, or turn Javascript off to trick your script into letting an attack through or just putting crappy misformatted data in your database, or whatever other destination you have for the submission data to go. Server side validation is where you get your true security.

  20. #20
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Egypt
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you hammer65
    i am already using both javascript to set the required fields and server side validation to validate input like email and minimum string lengths

    And I really wish you speedy recovery and to have your WPM much better than it was

    Thanks again

  21. #21
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kailash Badu View Post
    I use DBG which easily lets me step in , over, and out of the code while also letting me set the breakpoints. I have used Xdebug but dumped it when couldn't find a nice client for it. but I absolutely hate Zend specially when it gets annoyingly slow at times. I use PhpEd instead, which is much much faster than Zend because it's written in C using native Win32 API.
    I just tried PhpEd and its debugger, thanks for mentioning it. The software has a few nice features I would like to see in Zend Studio, but I felt it was lacking a few features Zend Studio has.

    It also has a high price tag, though cheaper than Zend Studio. Another possibility is Eclipse, Zend has developed a debugger plugin for it. Then you would get everything for free, though Ive not tried Eclipse myself, Ive heard good things about it.
    http://www.zend.com/pdt

    You might not believe me, but I felt PhpEd actually was slower than Zend Studio on my workstation.

    That mentioned Ive run Zend Studio on several machines, both normal pcs, workstations and laptops and Ive never noticed that its ever slowed down.
    Though I always put a good delay on the auto complete functionality as well as a low max possible matches number. Perhaps its those settings which makes it slower for others?

    Quote Originally Posted by kromey View Post
    I had the same problem with Zend Studio, which is why I dropped it. I'm not a particularly fast typer, but when I can type faster than ZS can update the screen on my WinXP system with 1.5GB RAM and a 3GHz Pentium 4 processor then something is terribly, horribly wrong
    As Ive mentioned above, Ive never had problems with Zend Studio slowing down. I also see myself as a pretty fast typer and never had any problems with it not updating fast enough.

    It would be interesting to see if that issue, could be related to the code complete functionality. If you get time someday, it would be interesting if you could test it again. Just to see if it makes any difference when you turn the auto complete time up.

    Quote Originally Posted by AGazzaz View Post
    i do not know if it is only me but my browsers are already doing this (opera, IE, firefox, konqueror) so all i have to do is to give the users an output of all the errors made when submitting the form instead of one error at a time and give them a javascript link back to the form where the data they entered will still be there
    This would in reality not be possible, not every browser keep the form data populated if you use the "go back" button or a javascript go back link. For example IE users would see an empty form.

    Instead you could include the form again after finding the error, and then populate the form both with the input and the error message directly.


Bookmarks

Posting Permissions

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