• Advertisement
Sign in to follow this  

HTML proper redirection status code?

This topic is 1978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello - I'm working on some features for my website, and I have a few questions about redirection.

Whenever the user visits a certain page, I want the following things to happen:
A) A (hidden) counter is incremented, so I know how many times that specific page was visited.
B) The user gets redirected to a new page.

Since the page wasn't "moved", status codes 301 and 302 don't really make sense.
Am I understanding that 303 is the best code to send to the user's web browser?

Something like:

header("HTTP/1.1 303 See Other");
header("Location: http://www.example.com/otherpage.php");


The redirection destination page may be on a different website, depending on which page is visited.

I'm not supposed to put any PHP or HTML code before the header() stuff... but can I add PHP code after the header() calls, to increment the counter?
[Edit:] Ah, I'm not supposed to put any PHP code that outputs text (or other content), right? Non-outputting PHP is fine?

Also, should I save user visits to the MySQL database, or to just a text file? Either would work, but which would you personally use? Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Advertisement
I would use the database for that.
Specifically, insert an entry per visit into a table that you can aggregate from time to time.

[s]Also I think you shouldn't have to redirect at all if it is just for counting page visits.[/s]

As to your question: Yes you can't output text before the headers are set because in html the headers always have to be sent before any content.

Edit: Didn't read your reasons for redirecting Edited by Madhed

Share this post


Link to post
Share on other sites
You could do something like

<?php

// Connect to DB

// Increment hits by 1

// Redirect to page
header(...);

?>


or redirect then on the otherpage.php you can increment and put that in the db

I would personally use MySQL for keeping track of page hits. Edited by wicked357

Share this post


Link to post
Share on other sites
Thanks guys, working on implementing it. So HTTP status code 303: is that the correct one for my needs? Redirection not because something moved, but because html file A's purpose is to point to html file B?

[hr]

[s]Unrelated question: How do I set up a domain name (otherDomain.com) to point to another domain's sub-directory (mainDomain.com/location/), but continuing to have URL listed as "otherDomain.com" as the user is exploring that site?

I have my main site, [color=#008080]www.myname.com, and I'd like [color=#008080]www.myname.com/[color=#8b4513]myGameName to be the URL of my game's website. (The reason why I want it to be "contained" within the first site, is because the site is powered by the same files, and it's really one site). However, I want the domain name to look nice for users, so they see: "[color=#8b4513]www.myGameName.com/about-this-game.html" instead of "[color=#008080]www.myname.com/[color=#8b4513]myGameName/about-this-game.html".

Any ideas?[/s]

[Edit:] Second question resolved, using this article. Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
3xx codes refer to redirection. 303 "see other" means that the response to request can be found in different URI and should be retrieved with the GET method. Though some older browsers might not support 303, then you should use 302 "found".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement