HTTP Header not correct in PHP - php

I have links that add a row to the database and then redirect to another page. In that page I want to show a message of success if it came from the insert page however the HTTP_REFERER doesn't acknowledge that page as the referer and instead shows the previous page.
So page-one.php contains a hyperlink:
http://example.com/add.php?c=359
and on add.php
header('Location: http://example.com/rows.php');
on rows.php I am expecting add.php to be the referer but it isn't instead page-one.php is.
How do I make add.php to be the referer cos that's where it is being redirected from?

The usual way it is with $_SESSION variables. Whenever you need to show a message add it:
$_SESSION['messages'][] = "your message";
Then, when you are on a page (any non-redirected page), show all of them and erase the content with:
$_SESSION['messages'] = array();

Related

how to redirect the user to Page 404 from URL

I have a php script that redirect the user to a specific page based on a record id (e.g. example.com/page.php?id=4)
My question is: How can I redirect the user to the 404 Error page if he type in the browser a record id that doesn't exists? (e.g. example.com/page.php?id=59542)
Although, putting an id that doesn't exists in the DB shows no data, but the user still can see the page template.. but with empty data...
Thanks
Using if statements check if there is such ID in the database, if it does not exist, do:
header("Location: 404.php");
You can change 404.php to your 404 file location.
You should send a 404 header, and maybe display a custom not found page:
<?php
header("HTTP/1.0 404 Not Found");
include("404.php");
?>
You can create a page, (a complete page, with CSS, ...) and redirect to that page every 404.
Example: Look what google does https://www.google.com/adlkfjaoie43 they created a page to redirect to.

php - how to redirect to be 100% sure that REFERRAL cleaned after redirect

How do I redirect users and be sure that the HTTP_REFERER does not transfer to the next page after reaching a page called redirect.php.
The program flow is as follows:
1) On page at http://example.com/index.php (contains a link to redirect.php)
2) User click on the link to redirect.php and it sends the header('Location: http://otherlocation.com/index.php');
3) I need to prevent otherlocation.com from seeing the HTTP_REFERER from http://example.com/index.php
I have tried:
header('Location:redirect.php');
This does not work as HTTP_REFERER is populated with the value from the first page (http://example.com/index.php).
Fill up HTTP_REFERER depending by browser, not server-side
You may try redirect user by
<meta http-equiv="refresh" content="2;url=http://otherlocation.com/index.php" />
<script>document.location = 'http://otherlocation.com/index.php';</script>
browser not fill up HTTP_REFERER at this moment (IMHO)
At firefox this not work :(
You can use
header("Location:redirect.php");
or if you want some delay or countdown, you can use.
// 5 is the seconds of the delay to go to the page you've entered.
header("refresh: 5; redirect.php";
to redirect using PHP send a header request to the browser. try this.
header('Location:redirect.php',true,302);
exit;
The above code will set the HTTP_REFERRER trace to current page. hence deleting the trace from previous page.

Re-direct user when parameter query incorrect

So I have a script (written in PHP) that posts the user input of a reference number and postcode and then redirects the user to a page with the URL parameters like so:
http://mydomain.com/data.php?reference=REF1234&postcode=LE1FEH
The data.php script will then fetch the data from the database according to the URL parameters, but what would I do if the reference number or postcode does not actually exist or match any row in the database - how would I send the user to an error page?
Because when they don't exist right now, the user is still directed to the data.php page - there is just no data on the page.
// after your query:
if( mysql_num_rows($res) < 1 )
{
header('Location: /errorpage/');
exit;
}
Preferably, that page will have some details on the error, you can tack those pieces on to the header if you'd like. Or, you can just show an error on the page that loads the data right there.
header("Location: error.php");
die("We attempted to redirect you to error.php but could not.");
Possibly add javascript redirect to the die() statement. It is a good idea to add die() after redirect because bots sometimes don't follow header redirects, and will be able to see the rest of the page.

How to hide php actions from history?

Is it possible to tell the browser that he should not remember the action scripts page?
So when you delete a headline for example. The get action will be ?d=ID where ID is the id of the headline. After removing the headline go to the page without the get variable in the url (the header part).
<?php
if(isset($_GET['d']) && preg_match('{^[0-9]{1,3}$}',$_GET['d'])){
$hid = $_GET['d'];
$deletesql = "DELETE FROM headlines WHERE hid = $hid";
mysql_query($deletesql);
header('Location: panel.php');} ?>
But now you browser history shows the link panel.php?d=23
Can prevent the browser from remembering the page? Maybe a 303 header?
You should use POST instead of GET for this. That way the browser will prompt the user if he wants to send the information again. (typically the id argument)
Make the ID hidden in the form and then get it in the form action part using $_POST and then do the delete action and redirect once delete is successful to avoid user from re posting the form.

Display message only for the first page load

I would like to display thank you message after adding comment only for the first page load... The comment form is processed using an external php file and than redirected back to the page. I would like to display some message after the redirection only... What would be the best way to do this using php?
Assuming you have access to the external php file that processes the file you could do something similar to the following on the processing file:
$_SESSION['flashMessage'] = 'Thank you for posting.';
header("Location: your-page.php');
And then add the following to the redirect page:
if ($_SESSION['flashMessage']) {
echo $_SESSION['flashMessage'];
$_SESSION['flashMessage'] = NULL;
}
Save the mesage into a session. Display it, and after just unset the session variable.
On the page where the comment is processed:
if($success)
{
$_SESSION['userMsg'] = "<p>Your comment has been added. Thank you.</p>";
}
In any/all pages (but mainly the one you're redirecting to):
if($_SESSION['userMsg'] != '')
{
print $_SESSION['userMsg'];
unset($_SESSION['userMsg'];
}
This is assuming you're using Sessions and have therefore previously called the session_start() function
When you redirect send via $_GET array a variable something like this:
header("LOCATION: index.php?msg=1" );
On index check if $_GET['msg']==1 then display your message
You may want to apply PRG pattern.
Basically you post the comment and the server replies to the client to perform a redirection to your page with additional info in Query string as Vadim argued.
"Elegant", sessionless and functional.

Categories