How to Redirect to the Same Page in PHP

How to redirect to the same page in PHP

There are a number of different $_SERVER (docs) properties that return information about the current page, but my preferred method is to use $_SERVER['HTTP_HOST']:

header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location);

where $location is the path after the domain, starting with /.

PHP: Redirect same page after click the submit button

Here are two example to redirect to a form.
Lets say that your filename is main.php

<form action="main.php">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>

Or you can use this

<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>

Did that answer your question?

PHP : How to redirect to the same page while retaining data from another page

When you submit the form, the $_GET['gameID'] is lost. Change your form URL to include the gameID.

Example:

<form action="/action_page.php?gameID=<%= $_GET['gameID'] %>">
...

However as @Ajeenckya already said. You should use something like the session. By using sessions, you don't have to pass the parameters all the time.

php: How can I redirect to the same page with a error message

Remove the return from the else chunk after the $failure = "Email must have an at-sign @" line.

I think, without knowing much of how the request is being handled, that according to the example when validation fails the process must not return. It only returns on success because in that case you would have a new header and so when the page loads it gets you somewhere, on failure you need to only change the $failure variable and that is it.

The result removing the return

Make a link redirect to the same page

To make your custom PHP blog post template be able to display a title link that points back to the page, one way is to make use of your $row[.... variable.

Provided that,

  • your URLS will look like your screenshots, such as http://localhost/viewpost.php?id=8 when running locally and for example http://www.yourwebsite.com/viewpost.php?id=8 when online
  • you know how to refer to the post's id that is used in the ...viewpost.php?id=8, for example $row['postID']
  • you don't yet have any variable or means to refer to your current domain http://localhost when local or http://www.yourwebsite.com when online

Then, I recommend a two-part approach:

Somewhere at the top of your code, or perhaps in an include you might use for such code-reuse purposes, define for example $host:

$host='http://' . $_SERVER['SERVER_NAME'];

Then, for your actual title link::

echo '<a href="' . $host . '/viewpost.php?id=' . $row['postID'] . '"> <h1 class="entry-title">' . $row['postTitle'] . '</h1></a>';

Explanation

  • Separated HTML from the concatenation dots . with spaces, to be easier to read, as well as to support any helper programs such as fmt that you might use for wrapping long lines, so they have spaces to use for wrapping lines.
  • Uses PHP's predefined $_SERVER variable's SERVER_NAME , which, combined with the http://, the $host will be http://localhost when local and http://www.yourwebsite.com when online.
  • Define $host as a variable once at the top of the page, because it is clearer that way and likely you will have a use for it elsewhere on the page, so this helps avoid having to repeat yourself writing 'http://'.$_SERVER['SERVER_NAME'] everywhere else you may need to start forming the absolute URL
  • $host is then combined with the pattern for the rest of the URL, to assemble the absolute URL
  • Absolute URL is helpful so that if a user saves your article to their computer, then later clicks the title link on their locally saved article, the user can still correctly reach the original online page
  • As the article author, setting a link this way also means it can serve as a permalink, which helps with search engine optimization (SEO)


Related Topics



Leave a reply



Submit