|
-
Oct 6th, 2007, 10:56 PM
#1
Thread Starter
Lively Member
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
-
Oct 6th, 2007, 11:18 PM
#2
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
-
Oct 6th, 2007, 11:24 PM
#3
Thread Starter
Lively Member
Re: Help making a counter and redirect
-
Oct 6th, 2007, 11:39 PM
#4
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
-
Oct 6th, 2007, 11:42 PM
#5
Thread Starter
Lively Member
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
-
Oct 6th, 2007, 11:53 PM
#6
Thread Starter
Lively Member
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"
-
Oct 7th, 2007, 12:05 AM
#7
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
-
Oct 7th, 2007, 02:49 AM
#8
Re: Help making a counter and redirect
Basic mathematics:
PHP Code:
if ($num % 5 == 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.
-
Oct 7th, 2007, 02:52 AM
#9
Re: Help making a counter and redirect
Oh, and:
 Originally Posted by dclamp
PHP Code:
if ($str = false) {
That will never execute, since you're assigning false to $str. You need === there.
-
Oct 7th, 2007, 11:40 AM
#10
Re: Help making a counter and redirect
haha. oops. thanks pena, i corrected that.
My usual boring signature: Something
-
Oct 7th, 2007, 11:02 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|