Sign in to follow this  
Servant of the Lord

HTML proper redirection status code?

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");

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?
[b][Edit:][/b] Ah, I'm not supposed to put any PHP code that [u][i]outputs text[/i][/u] (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
Madhed    4095
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
wicked357    2424
You could do something like


// Connect to DB

// Increment hits by 1

// Redirect to page


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 [b]HTTP status code 303[/b]: 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?


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

I have my main site, [color=#008080][i][/i][/color], and I'd like [i][color=#008080][/color]/[color=#8b4513]myGameName[/color][/i] 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][i][/i][/color]" instead of "[i][color=#008080][/color]/[color=#8b4513]myGameName/about-this-game.html[/color][/i]".

Any ideas?[/s]

[b][Edit:][/b] Second question resolved, using [url=""]this article[/url]. Edited by Servant of the Lord

Share this post

Link to post
Share on other sites
ToniKorpela    271
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this