PDA

View Full Version : Changing default page with cookie


Jamie
June 12th, 2007, 18:18
Alright, this could be an interesting challenge.

Is it possible to allow a user to choose which page on a Web site they would like to have set as their default page when they visit a top level domain?

For example:

Let's say a visitor comes to MyDomain.com

In the header, there is a drop down menu with three options.
1. Home
2. Video Blog
3. Text Blog

They can choose an option from that drop down menu, and once chosen it sets a cookie on the users computer which determines what URL they are directed to when they next visit http://www.mydomain.com

If they don't choose any option, it doesn't change anything (IE. It stays as "Home" which would be http://www.mydomain.com)

How simple would this be?

Tyler
June 12th, 2007, 21:11
Pretty simple, just set the cookie.

Here's an example, if they want to go home always.

setcookike('location','home',time()+999999,'/');


Or if they want to go to the video blog.

setcookike('location','vblog',time()+999999,'/');


Then you can do a series of ifs on that cookie to determine where they go, you can use header() to redirect.


<?php
if($_COOKIE['location'] == 'home')
{
header("Location: home");
}
elseif($_COOKIE['location'] == 'vblog')
{
header("Location: video blog");
}
?>


And so forth. Does that make any sense?

Jamie
June 12th, 2007, 21:20
Does that make any sense?

It probably does to somebody with a little more experience with PHP and cookies than me, but it doesn't make much sense to me, Tyler. :blush:

In your example, where would I define the URL for each page, and do you have any idea how I would allow a user to select which page they want to set as the default?

Tyler
June 12th, 2007, 21:28
Sorry Jamie!

You would want to define it in the header(), taking from my example:

header("Location: video blog");

Would become:

header("Location: http://www.google.com");

Here's a quick thing I whipped up for the actual selection.


<form action="preference.php" method="post">
<select name="page">
<option value="home">Home</option>
<option value="vblog">Video Blog</option>
<option value="tblog">Text Blog</option>
</select>

<input type="submit" name="submit" value="Change!" />


And the PHP (excuse the non-formatting):


<?php
//Let's first check to make sure they pressed submit.
if(isset($_POST['submit']))
{
//Our select name was "page", so it should be available in the $_POST array.

//They choose home.
if($_POST['page'] == 'home')
{
//Set the cookie for it.
setcookike('location','home',time()+999999,'/');

//You can either echo that preferences saved, redirect them to that page, or whatever you want.
}
//Maybe they choose vblog?
elseif($_POST['page'] == 'vblog')
{
//Set the cookie.
setcookike('location','vblog',time()+999999,'/');

//You can either echo that preferences saved, redirect them to that page, or whatever you want
}
//And so forth for text blog, or any other option you want. Your name in the option will be the value of $_POST['page']
?>


Any better?

Jamie
June 12th, 2007, 21:37
Getting there (Sorry. I often need to be given it on a plate when it comes to PHP. :blush:)

So I would put the PHP code in to preference.php

I guess the only thing I'm not sure about is where the whole cookie code in post #2 in this thread would go. :?

Tyler
June 12th, 2007, 21:39
Yup, or you can just change the HTML to another file.

Well, that's up to you. If you want, you can stick in a index.php, and then redirect to say: video-blog.php, text-blog.php and so forth from index.php, and have nothing else in there. That's the easiest method that I can think of, or how were you planning on having your file structure?

Jamie
June 12th, 2007, 21:54
I'm probably doing this wrong because I'm getting an error when I select an option from the drop down menu:

Here is the error I'm getting:

Parse error: syntax error, unexpected $end in /home/jamieh/public_html/preference.php on line 32

Here is the code I have in files:

On index.php I have the following in the <head>:

<?php
if($_COOKIE['location'] == 'home')
{
header("Location: http://www.myurl.com/index.php");
}
elseif($_COOKIE['location'] == 'vblog')
{
header("Location: http://www.myurl.com/video-blog.php");
}
elseif($_COOKIE['location'] == 'tblog')
{
header("Location: http://www.myurl.com/text-blog.php");
}
?>

I also have the following form on index.php

<form action="http://www.myurl.com/preference.php" method="post">
<select name="page">
<option value="home">Home</option>
<option value="vblog">Video Blog</option>
<option value="tblog">Text Blog</option>
</select>

<input type="submit" name="submit" value="Change!" />
</form>

I then have the following inside preference.php

<?php
//Let's first check to make sure they pressed submit.
if(isset($_POST['submit']))
{
//Our select name was "page", so it should be available in the $_POST array.

//They choose home.
if($_POST['page'] == 'home')
{
//Set the cookie for it.
setcookike('location','home',time()+999999,'/');

//You can either echo that preferences saved, redirect them to that page, or whatever you want.
}
//Maybe they choose vblog?
elseif($_POST['page'] == 'vblog')
{
//Set the cookie.
setcookike('location','vblog',time()+999999,'/');

//You can either echo that preferences saved, redirect them to that page, or whatever you want
}

elseif($_POST['page'] == 'tblog')
{
//Set the cookie.
setcookike('location','tblog',time()+999999,'/');

//You can either echo that preferences saved, redirect them to that page, or whatever you want
}
//And so forth for text blog, or any other option you want. Your name in the option will be the value of $_POST['page']
?>

Have I got all the pieces of code in the correct place?

Tyler
June 12th, 2007, 21:57
Nope, I made a goofy error :oops:

I forgot to close the if statement that checks to make sure submit was pressed.

Added another } right before the ?>.

This is what happens when you try to type PHP code in the quick reply box :oops:

Jamie
June 12th, 2007, 22:03
I added that in to preference.php, Tyler.

However, now I'm getting another error. :lol:

Fatal error: Call to undefined function: setcookike() in /home/jamieh/public_html/preference.php on line 19

Any ideas?

Tyler
June 12th, 2007, 22:04
/me makes note to always use an editor.

All of 'em should be setcookie

Jamie
June 12th, 2007, 22:16
Thanks Tyler.

I had to move the header() info so it was above the doctype of index.php, but once I did that it worked.

However, Opera and Fx don't seem to cope too well with it. Once an option has been chosen with the drop down, the site doesn't seem to load again (I assume both browsers are not coping with the header() redirects).

Unless you have any ideas as to why that happens, I'm going to leave it for the time being. I wasn't expecting to get so far so fast, so I'm well ahead of where I expected to be with this cookie. It's not too important. All it does is take out one click in my visitors experience.

If you don't have any immediate ideas of why Opera/Fx arn't coping with it (IE6 handles it without any trouble), then I'll leave it for now. It's probably best I show y'all a live environment to show you what is happening too, but at this point I can't do that. :)

Thanks Tyler. Much appreciated. Like I say, you got me a lot further than I expected to be by this evening. :)

Tyler
June 12th, 2007, 22:21
Is it at the very top of your document? The thing that comes to mind is that it may be an infinite loop for one reason or another.

Can you use something to see if the cookie was set correctly? There's Web developer for Fx and a developer extension that Gerrit linked to here, both will let you see a cookie and its' value.

Other than that, I can't think of a reason, I would have to see it.

No problem at all Jamie. :)

Jamie
June 12th, 2007, 22:24
Yup, it's at the very top of the document. I had to put it there because when it was just before my </head> tag it wasn't working. Maybe putting it just after the opening <head> tag would work.

I'll try that tomorrow. :)

I think the cookie is certainly being set correctly, because it works perfectly in IE6. It redirects me to the correct page.

In Opera and Fx the page just hangs when trying to load the redirected page. :)

Tyler
June 12th, 2007, 22:26
Odd. There goes my idea of an infinite loop as well.

Let me know on the cookie. :)

Oh, and header() works in an odd way. But basically, there can be no output whatsoever for it function correctly, without doing some rather advanced stuff.

Jamie
June 12th, 2007, 22:28
One of the other reasons I've decided to put this on hold for now is because I don't want the cookie to function when "index.php" is in the address bar.

For example, if a visitor comes straight to http://www.mydomain.com I want the cookie to activate and redirect them to the correct place.

But when they go to http://www.mydomain.com/index.php (Which is the same page as is initially loaded when a visitor goes to http://www.mydomain.com) I don't want the redirect to take place, because I still want visitors to be able to get to index.php (from a link on my site, for example) even if their default page is /video-blog.php.

As a result, I don't have the patience to figure out how to do that right now, nor do I want to go asking you to spend all your day figuring it out. :lol:

Tyler
June 12th, 2007, 22:31
I don't mind at all, in fact, this is keeping me entertained. :)

The first thing that popped into my mind was mod_rewrite. You could have your true index.php to handle the cookie. But then you could do something such as home.php (internally) and rewrite that to say index.php publicly, and they wouldn't know the difference. :)

Jamie
June 12th, 2007, 22:37
Would that mean having two sets of content, Tyler, or would it be possible for home.php to copy the content on index.php to a point where I would only need to update one of the pages to update them both?

The reason I ask is because a lot of the content on index.php is dynamic, database driven and some that changes manually on a regular basis, so it would be a pain having to make sure both pages are updated.

Tyler
June 12th, 2007, 22:46
What I was thinking was index.php would only check for a cookie, and then just redirect them, nothing more, nothing less.

Home.php (publicly index.php) would be just a normal page, just that to the public it would be saw as index.php.

There are many ways you can do this. You could also set it so your index.php would be just your content. Then have something like redirection.php that serves as the index of the domain, so it get hit only on domain.com.

Jamie
June 12th, 2007, 22:49
Ah, I see what you're saying. That makes sense, I guess. :)

Thanks Tyler! I'll probably revisit this thread later in the week. Right now I have enough on my plate before I get this Web site launched tomorrow. :)

Thanks!

Tyler
June 12th, 2007, 22:52
No problem Jamie, y'know me and how I'm always around. :)