SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    writing a simple class: redirect doesn't work.

    hi,
    i'm writing one of my first classes.
    when a problem occurs, the class is escaped and the page reloaded with an error message thank to the querystring.

    I always get a message that tells me that the redirect can't be done etc.

    Does anyone have a clue why?

    PHP Code:
    class Validator {

    var 
    $redirectLink;
    var 
    $idata;
    var 
    $minsize;
    var 
    $maxsize;
    var 
    $pattern;
    var 
    $different;
    var 
    $same;

      function 
    Validator () 
      {


          
    $this->redirectLink  $_SERVER['PHP_SELF'];


      }


    function 
    validateData($idata$minsize$maxsize$pattern=false$different=false$same=false) {


          
    $this->idata         $idata;
          
    $this->minsize       $minsize;
          
    $this->maxsize       $maxsize;
     
        
    $this->checkMinSize();
        
    $this->checkMaxSize();
    /*
        if ($this->pattern) {
            $this->checkPattern();
        }

        if ($this->different) {
            $this->compareArrayElementsDifferent();
        }

        if ($this->same) {
            $this->compareArrayElementsSame();
        }
    */
      
    }


    function 
    checkMinSize()
    {

            if(
    strlen($idata) < $this->minsize)
            {
                
    $this->redirect('minsize');
            }


    }


    function 
    checkMaxSize() 
    {

            if(
    strlen($idata) > $this->maxsize) {
                
    $this->redirect('maxsize');
            }

       

    }

    function 
    redirect($error_reason)
    {

        
    header('Location: ' $this->redirectLink '?error=' $error_reason);
        exit;

    }



    }

    $validate = & new Validator();
    $validate->validateData('asdasdasdfsdfsdfsdfsd'2526); 
    thanks in advance for your time and your help

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you echo something before issuing the header() command, the headers have already been sent to the browser, so you get an error when you try to send them again.

    Make sure you have nohting sent to the browser before you use header()

    like echo, print, etc statements

  3. #3
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Throw an ob_start(); at the top of your script.

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no space and nothing echoed. There definitly is not output before the header() function.

    I tested it in IE and it worked (ironically).

    It was said in Firefox that there is an interruption, that it might due to cookie blocked... Weird thing is that I don't use Cookies....

    I am a bit confused.

    On another note, no matter what I try, the Validator class always return a minchar error...

    You can cut and paste the above code to try it.



    thanks for our time fellas.

    PHP Code:
    class Validator {

    var 
    $redirectLink;
    var 
    $idata;
    var 
    $minsize;
    var 
    $maxsize;
    var 
    $pattern;
    var 
    $different;
    var 
    $same;

      function 
    Validator () 
      {


          
    $this->redirectLink  $_SERVER['PHP_SELF'];


      }


    function 
    validateData($idata$minsize$maxsize$pattern=false$different=false$same=false) {


          
    $this->idata         $idata;
          
    $this->minsize       $minsize;
          
    $this->maxsize       $maxsize;
     
        
    $this->checkMinSize();
        
    $this->checkMaxSize();
    /*
        if ($this->pattern) {
            $this->checkPattern();
        }

        if ($this->different) {
            $this->compareArrayElementsDifferent();
        }

        if ($this->same) {
            $this->compareArrayElementsSame();
        }
    */
      
    }


    function 
    checkMinSize()
    {

            if(
    strlen($idata) < $this->minsize)
            {
                
    $this->redirect('minsize');
            }


    }


    function 
    checkMaxSize() 
    {

            if(
    strlen($idata) > $this->maxsize) {
                
    $this->redirect('maxsize');
            }

       

    }

    function 
    redirect($error_reason)
    {

        
    header('Location: ' $this->redirectLink '?error=' $error_reason);
        exit;

    }



    }



    class 
    ValidatorError {

    var 
    $error_reason;

    function 
    ValidatorError($error_reason)
    {

        
    $this->error_reason $error_reason;

    }

    function 
    displayErrorMessage() {


        switch (
    $this->error_reason) {
        case 
    'minsize':
           
    $error_message 'min chars';
           break;
        case 
    'maxsize':
           
    $error_message 'max chars';
           break;
        default:
           
    $error_message '';

        }

        return 
    $error_message;

    }

    }

    $validate = & new Validator();
    $validate->validateData('asdasdadasdasdasdasdasd'58);

    if (isset(
    $_GET['error'])) {
    $errors = & new ValidatorError($_GET['error']);
    echo(
    $errors->displayErrorMessage());


  5. #5
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    Don't worry, you don't have a problem with Firefox

    No one noticed that your script is creating a loop. You're instantiating the object after aech redirection, thus repeating the validation error, thus redirecting, thus reloading, thus repeating........

    Fix that problem. In a real life situation, you would check if the form has been submitted, then validate data. Here you're validating your stuff everytime the page is loaded and since you force it to reload in case of problem.. well... Some browsers freak out.

    'hope that helped.


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
  •