Gerner Christensen
Gerner Christensen

Reputation: 43

going back 1 page using php

In my footer I have

header("Location: galleries.php");

Is it possible to use php code (or something) to just go 1 page back instead of using a specific page like galleries.php?

Using something like history.go(-1) won't work since the header("Location: galleries.php"); is in PHP.

Upvotes: 0

Views: 1669

Answers (4)

Balagopal
Balagopal

Reputation: 1

You can use this

<a href="#" onClick="javascript:history.back();">Go back</a>

Upvotes: 0

Bud Damyanov
Bud Damyanov

Reputation: 31839

Put in short - not really. PHP is not client-side (like JavaScript) language, but server-side one, the header("Location: ..."); is used for that purpose - redirecting to another location. Keep in mind, that redirection must be done before any output, otherwise you'll get errors/warnings of type

Warning: Cannot modify header information - headers already sent (output started at...).

Using the $_SERVER['HTTP_REFERER'] is not an reliable option however.

The meaning of the HTTP_REFERER is the address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted, because it CAN be missing.

Read here for more details.

Upvotes: 1

greW
greW

Reputation: 1338

you can do it in 2 ways: Option one is to use JS,

echo "<a href=\"javascript:history.go(-1)\">back</a>";

option 2 is to get the previous link by checking where the user came from with

[HTTP_REFERER]

http://www.php.net/manual/en/reserved.variables.server.php and then send him to there via header("location: .."),

but keep in mind if the user gets in direct link into that page both ways aren't useful.

Upvotes: 0

Ranjith
Ranjith

Reputation: 2819

Try this for go to previous page,

header('Location: ' . $_SERVER['HTTP_REFERER']);

Upvotes: 2

Related Questions