Results 1 to 11 of 11

Thread: Help making a counter and redirect

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Help making a counter and redirect

    hello

    I want to make a counter so when someone visit the page it increases by 1
    then when the number can be divided into 5 i want the page to redirect to another page

    how can i do that

    thanks
    adel

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Help making a counter and redirect

    Well any number can be divided by 5. You want to check if it is a multiple of 5?

    5, 10, 15, 20, 25...
    My usual boring signature: Something

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: Help making a counter and redirect

    yah exactly

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Help making a counter and redirect

    ok, well here is something that I came up with...

    PHP Code:
    //Get current page view count and add one
    $count++;

    //Check to see if count is a decimal
    $number $count 5;
    $str strstr($number'.');

    //if it isnt, then it must be a multiple of 5
    if ($str == false) {
         
    //Redirect...
         
    header("Location: http://www.mysite.com");

    basically there is no built in function, so it will check to see if there is a decimal in the number, and if there is no decimal, that means it went into it evenly.
    Last edited by dclamp; Oct 7th, 2007 at 11:39 AM.
    My usual boring signature: Something

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: Help making a counter and redirect

    thanks i will try it

    by the way isnt there a "%" operator that can check the remainder if the division i dunno i am still a noob in PHP

    thanks again i will try it and respond whether it worked or not

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: Help making a counter and redirect

    when it reaches where it should redirect it tells me "Warning: Cannot modify header information, headers already sent"

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Help making a counter and redirect

    ah. header needs to come before any outputted information. So it should be at the top of your page.
    My usual boring signature: Something

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Help making a counter and redirect

    Basic mathematics:
    PHP Code:
    if ($num == 0)
      
    # $num is a whole multiple of 5. 
    String conversion is so inefficient: even if it was a string to begin with, then strpos() would be more efficient than strstr(), which returns the substring rather than the index at which it is found.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Help making a counter and redirect

    Oh, and:

    Quote Originally Posted by dclamp
    PHP Code:
    if ($str false) { 
    That will never execute, since you're assigning false to $str. You need === there.

  10. #10
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Help making a counter and redirect

    haha. oops. thanks pena, i corrected that.
    My usual boring signature: Something

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Help making a counter and redirect

    More acceptable, but still not quite correct. strstr() returns either a string or boolean false. The correct operator to use when checking for an explicit false value is ===, not ==, since there are some other values which can be evaluated to false if type checking is not applied. I don't know off-hand if there are any string values which do, but it's still not a good idea to use == where === is more appropriate.

    As mentioned, though, if you just want to check whether a substring is present within a string then use strpos() instead:
    PHP Code:
    if (strpos($mystring'substring') === 0)
      
    # substring not found in $mystring 
    In this case, the == operator is not just less appropriate, but flat out incorrect: if $mystring begins with 'substring', then strpos() will return zero, which evaluates to false. This is obviously an undesirable effect.

Posting Permissions

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



Click Here to Expand Forum to Full Width