Redirect if no Cookie set?
I had found this one PHP code on one site before that did exactly what I needed and had thought I had bookmarked it, but now I am unable to find it.
So I thought I'd ask here if someone had or knows how to do the same thing..
So for example of php pages.. index1 and index2..
If a user who has never visited the site before goes to index2 (either by an outside link or some other way) they would be redirected to index1 where a cookie will be set, that will allow them to go to index2 with out being redirected.
But NOT redirect them to index2 if they do have the cookie and go to index1.
So roughly, is another site links to index2 rather then index1, for a user who HAS been to the site before, they can continue as normal, but if they haven't gone to the site before (don't have the cookie) they would be redirected to index1 and have a cookie set.
But still, if they do have the cookie and go to index1, for nothing to happen.. they can go on though the site (and to index2) normally with out being redirected.. (unlike all the codes I have been finding x.x)
So, there.. thats what I'd like.. I hope I explained that the best I could..
"Naku ga yousuru ichidanto tsuyosa kyaku ga icchuu wo yusuru."
MyFreeCounter posted this at 21:02 — 27th January 2006.
They have: 7 posts
Joined: Jan 2006
some info:
setting cookies - http://us3.php.net/manual/en/function.setcookie.php
to delete them, simply reset the cookie only without a value or with an expiration time in the past.
retrieving cookies - http://us3.php.net/manual/en/reserved.variables.php#reserved.variables.cookies
gets the value - $_COOKIE['Cookie_Name']
so basically, you need to have on index 2 a segment that checks for the cookie:
if ($_COOKIE['your_cookie_name']!='some_value') {
header("Location: index1.php");
}
then on index 1, you need to have something that sets your cookie:
setcookie("your_cookie_name","some_value",time()+100000000);
it should work, let me know.
Matt Pegler
MyFreeCounter Owner/Operator
http://www.MyFreeCounter.net - free hit, realtime, and click counters, as well as statistics
Nicholas posted this at 23:52 — 3rd February 2006.
They have: 6 posts
Joined: Feb 2006
Your code is flawed...in the line
header("Location: index1.php");
'you need to have the exact location of the file, atleast it was that way in my experience. For example, instead of index1.php, it would have to be
header("Location: http://www.yoursite.com/index1.php");
'Also, I wouldn't bother with cookies, because they wear off or get deleted eventually. I would use an SQL database to store ip's and scan the list for the person's ip address and if it's not there have it redirect and add their ip address to the list.
Once again, another easier way to tell how long a cookie will last is to use, in the cookie set line, the following format, "time() +60*60*24*7*52" 60 seconds times 60 minutes times 24 hours times 7 days times 52 weeks, obviously this is one year, instead of using 10000000000 and not being able to tell by simply looking at it how long it is.
Saeru posted this at 20:39 — 1st February 2006.
She has: 2 posts
Joined: Jan 2006
I have tried something like that, but it wont redirect. I had also added in a small extra code that would tell me if the cookie did get set (for testing)
I got it to set one time (cookie set on a 1 minute expier time again for testing) I got the cookie to set one time (though I had to manually go to index1,) but after that one time it wont set again.
I've used cookies before so I do know how to work with them.. but never tried to do something like this, so it's really baffleing me.
"Naku ga yousuru ichidanto tsuyosa kyaku ga icchuu wo yusuru."
Busy posted this at 20:41 — 1st February 2006.
He has: 6,151 posts
Joined: May 2001
Can you use a session variable? for those who except cookies the session becomes a cookie, for those who dont it remains a session
Busy posted this at 09:34 — 4th February 2006.
He has: 6,151 posts
Joined: May 2001
Were you able to find a solution?
For the record you don't need the full url in Header, header("Location: index1.php"); would work just fine.
And storing IP isn't fool proof either as proxy hosts (dialup etc) can use random IP range (within a range).
Just remember if you use time() it's the server's time not your local time.
Nicholas posted this at 21:29 — 4th February 2006.
They have: 6 posts
Joined: Feb 2006
I just said use full address in header because mine only worked that way, may be different for others. And yea I didn't think about dial-up, its so old I forgot people use it.
Busy posted this at 22:05 — 4th February 2006.
He has: 6,151 posts
Joined: May 2001
Some of us have no choice about using dial up
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.