How to go to top of page with php năm 2024

14 years ago

`I strongly recommend, that you use

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

instead of

header("HTTP/1.1 404 Not Found");

I had big troubles with an Apache/2.0.59 (Unix) answering in HTTP/1.0 while I (accidentially) added a "HTTP/1.1 200 Ok" - Header.

Most of the pages were displayed correct, but on some of them apache added weird content to it:

A 4-digits HexCode on top of the page (before any output of my php script), seems to be some kind of checksum, because it changes from page to page and browser to browser. (same code for same page and browser)

"0" at the bottom of the page (after the complete output of my php script)

It took me quite a while to find out about the wrong protocol in the HTTP-header.

`

14 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`

16 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`

9 months ago

`If you use header() to allow the user to download a file, it's very important to check the encoding of the script itself. Your script should be encoded in UTF-8, but definitely not in UTF-8-BOM! The presence of BOM will alter the file received by the user. Let the following script:

$content

= file_get_contents('test_download.png') ; $name = 'test.png' ; $size = strlen($content) ;header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: 0'); header('Content-Disposition: attachment; filename="'.$name.'"'); header('Content-Length: ' . $size); header('Pragma: public');

echo

$content ;?>

Irrespectively from the encoding of test_download.png, when this PHP script is encoded in UTF-8-BOM, the content received by the user is different:

  • a ZWNBSP byte (U+FEFF) is added to the beginning of the file
  • the file content is truncated!!! If it's a binary file (e.g. image, proprietary format), the file will become unreadable.

`

18 years ago

`When using PHP to output an image, it won't be cached by the client so if you don't want them to download the image each time they reload the page, you will need to emulate part of the HTTP protocol.

Here's how:

That way foo.png will be properly cached by the client and you'll save bandwith. :)

`

15 years ago

`If using the 'header' function for the downloading of files, especially if you're passing the filename as a variable, remember to surround the filename with double quotes, otherwise you'll have problems in Firefox as soon as there's a space in the filename.

So instead of typing:

you should type:

If you don't do this then when the user clicks on the link for a file named "Example file with spaces.txt", then Firefox's Save As dialog box will give it the name "Example", and it will have no extension.

See the page called "Filenames_with_spaces_are_truncated_upon_download" at for more information. (Sorry, the site won't let me post such a long link...)

`

4 years ago

Since PHP 5.4, the function http_​response_​code() can be used to set the response code instead of using the header() function, which requires to also set the correct protocol version (which can lead to problems, as seen in other comments).

2 years ago

`Please note that there is no error checking for the header command, either in PHP, browsers, or Web Developer Tools.

If you use something like "header('text/javascript');" to set the MIME type for PHP response text (such as for echoed or Included data), you will get an undiagnosed failure.

The proper MIME-setting function is "header('Content-type: text/javascript');".

`

8 years ago

`According to the RFC 6226 (), the only way to send Content-Disposition Header with encoding is:

Content-Disposition: attachment; filename*= UTF-8''%e2%82%ac%20rates

for backward compatibility, what should be sent is:

Content-Disposition: attachment; filename="EURO rates"; filename*=utf-8''%e2%82%ac%20rates

As a result, we should use

I have tested the code in IE6-10, firefox and Chrome.

`

6 years ago

The header call can be misleading to novice php users. when "header call" is stated, it refers the the top leftmost position of the file and not the "header()" function itself. "

15 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`0

8 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`1

11 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`2

6 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`3

20 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`4

14 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`5

15 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`6

7 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`7

14 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`8

6 years ago

`Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:

here.'; ?>

Hth someone

`9

15 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`0

13 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`1

14 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`2

14 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`3

9 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`4

4 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`5

20 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`6

16 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`7

8 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`8

15 years ago

`A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it's a good idea to set the status code at the same time. Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely. Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header('Location:') defaults to 302.

`9

5 years ago

`If you use header() to allow the user to download a file, it's very important to check the encoding of the script itself. Your script should be encoded in UTF-8, but definitely not in UTF-8-BOM! The presence of BOM will alter the file received by the user. Let the following script:

$content

= file_get_contents('test_download.png') ; $name = 'test.png' ; $size = strlen($content) ;header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: 0'); header('Content-Disposition: attachment; filename="'.$name.'"'); header('Content-Length: ' . $size); header('Pragma: public');

echo

$content ;?>

Irrespectively from the encoding of test_download.png, when this PHP script is encoded in UTF-8-BOM, the content received by the user is different:

  • a ZWNBSP byte (U+FEFF) is added to the beginning of the file
  • the file content is truncated!!! If it's a binary file (e.g. image, proprietary format), the file will become unreadable.

`0

5 years ago

`If you use header() to allow the user to download a file, it's very important to check the encoding of the script itself. Your script should be encoded in UTF-8, but definitely not in UTF-8-BOM! The presence of BOM will alter the file received by the user. Let the following script:

$content

= file_get_contents('test_download.png') ; $name = 'test.png' ; $size = strlen($content) ;header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: 0'); header('Content-Disposition: attachment; filename="'.$name.'"'); header('Content-Length: ' . $size); header('Pragma: public');

echo

$content ;?>

Irrespectively from the encoding of test_download.png, when this PHP script is encoded in UTF-8-BOM, the content received by the user is different:

  • a ZWNBSP byte (U+FEFF) is added to the beginning of the file
  • the file content is truncated!!! If it's a binary file (e.g. image, proprietary format), the file will become unreadable.

`1

13 years ago

`If you use header() to allow the user to download a file, it's very important to check the encoding of the script itself. Your script should be encoded in UTF-8, but definitely not in UTF-8-BOM! The presence of BOM will alter the file received by the user. Let the following script:

$content

= file_get_contents('test_download.png') ; $name = 'test.png' ; $size = strlen($content) ;header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: 0'); header('Content-Disposition: attachment; filename="'.$name.'"'); header('Content-Length: ' . $size); header('Pragma: public');

echo

$content ;?>

Irrespectively from the encoding of test_download.png, when this PHP script is encoded in UTF-8-BOM, the content received by the user is different:

  • a ZWNBSP byte (U+FEFF) is added to the beginning of the file
  • the file content is truncated!!! If it's a binary file (e.g. image, proprietary format), the file will become unreadable.

`2

How do I redirect to the top of the page?

To make the page jump on the top instantly can be done with the native JavaScript method, namely — window. scrollTo(x-coord, y-coord) . This property of the window interface scrolls to a particular set of coordinates in the document.

How do you go to the top of the page in HTML?

As defined in the HTML specification, you can use href="

top" or href="#" to link to the top of the current page. Yes, it is easy to scroll to the top by using the html's tag.

How do you return to the top of the page in HTML?

Add the top of page link using

top as the value of the href attribute, as shown below. For example, clicking this link takes you back to the top of the page. All modern browsers understand the "

top" value, meaning the id or anchor is unnecessary to return to the top.

How do you go back to the top of the page in JavaScript?

Setting the position parameter to 0 scrolls the page to the top. Syntax: $(window). scrollTop(position);