Public Group

# HTML proper redirection status code?

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

## 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 on other sites
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.

##### 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 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 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".

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633697
• Total Posts
3013407
×