TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-01-2008, 12:44 PM   #1 (permalink)
The Wanderer
 
kokjj87's Avatar
 
Join Date: Oct 2008
Posts: 18
Thanks: 0
kokjj87 is on a distinguished road
Help how to capture and show error in oop php to user?

hi, i am new to oop php programming style.. i am trying to write a image class to process images, but how do i capture the error(for example when you upload a non image file) and show it to the user?

PHP Code:
my image class
----------------------------------------------------
<?php
class image
{
private 
$allowextension = array("image/gif","image/jpeg","image/png");
private 
$source;

        function 
__construct($source)
        {
        
$this->source unserialize($source);

                
//check whether the upload file is the extension that is allow
                
if(!in_array(($this->source['type']), $this->allowextension))
                {
                  
#how do i show an error to the user?.. in a presentable way
                
return false;
                }
        }
}

?>
PHP Code:
my form
---------------------------------------------------
<?php
include("imageClass.php");


if(isset(
$_FILES["file"]) && !empty($_FILES["file"]))
{
$upload serialize($_FILES["file"]);
$image = new image("$upload");
}

else
{
?>
<form method="post" enctype="multipart/form-data" action="<?php echo
$_SERVER['PHP_SELF']; ?>" >
<input type="file" name="file" /><input type="submit" value="Upload"/>
</form>

<?php ?>
I am looking at try and catch but couldn't understand how to show it to the user in a more customizable way(can be use with css).
Thanks in advance!

Last edited by kokjj87 : 10-01-2008 at 01:58 PM.
kokjj87 is offline  
Reply With Quote
Old 10-02-2008, 03:51 AM   #2 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Your code is sort of wrong, you cannot return a value in a contructor or destructor, you can only catch these sort of values if they are staticlly declared, but non of the magic methods may be declared staticlly.

To detect an error you should use Exceptions, instead of the return false part in your constructor you say:
PHP Code:
throw new Exception('<error message goes here>'); 
And you replace this part:
PHP Code:
$image = new image("$upload"); 
With this to catch the exception:
PHP Code:
try
{
    
$image = new image($upload);
}
catch(
Exception $e)
{
    echo 
$e->getMessage();

You can also use a default exception handler using the set_exception_handler() function.

For more information about the methods that the Exception class exposes, then referer to the php.net docs here:
PHP: Exception - Manual
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 10-02-2008, 08:45 AM   #3 (permalink)
The Wanderer
 
kokjj87's Avatar
 
Join Date: Oct 2008
Posts: 18
Thanks: 0
kokjj87 is on a distinguished road
Default

Thank you Kally.

this is what i am looking for.. =)

can you give an idea of how to use the capture message from the exception and display it to the user with style(css).
kokjj87 is offline  
Reply With Quote
Old 10-02-2008, 12:13 PM   #4 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Use the getMessage() from the exception class to print the error message set from the constructor
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 10-02-2008, 04:31 PM   #5 (permalink)
The Wanderer
 
kokjj87's Avatar
 
Join Date: Oct 2008
Posts: 18
Thanks: 0
kokjj87 is on a distinguished road
Default

what about this, let say that i have caught a few error and i only want to show some of it(those that i want to show, those that would not lead to security issues), and hide the rest..
How do you do it?
kokjj87 is offline  
Reply With Quote
Old 10-02-2008, 06:37 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It sounds as though you may wish to use error handling, instead of exception handling, to me.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:15 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design