Re-direct user when parameter query incorrect - php

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.

Related

How to make certain URL's not work (even if the page exists)

For example, I have a page called profile_page.php. This page is only functional if data is written after the ?u= in the URL, for example, data for Alice's profile page can only be seen when the URL reads http://localhost/profile_page/alice.
Loading http://localhost/profile_page will give me undefined variable errors as most of my variable's are depending on the URL to have a value after the ?u=. For example, the variable $firstname can only be gathered when I get her username in the URL.
In such a case, when http://localhost/profile_page, I would rather have it redirect the user to their own profile_page, but I don't know how I can test the URL and parse it through an if statement.
I understand you can use $u=$_GET['u']; to obtain the current page URL? but I don't think doing this, is the best way to go about it:
$u=$_GET['u'];
if ($u == "http://localhost/profile_page/"){
// redirect to logged in users page code here
}
First, if you are using some parameter for your page to build, the url would looks like httlp://localhost/profile_page.php?firstname=alice&lastname=brown, with $_GET['firstname'] you will get alice in this case. If you want to test if the parameter is set first and redirect to another page if it is not set, you could use
if(!isset($_GET['firstname'])
{
header('Location:redirected_page.php');
}

how to remove a parameter from url on page refresh php

I am sending error values in the url.For example if i have a website named
www.example.com
and the url for login page is
www.example.com/login.php.
If the user enters wrong credentials url will be
www.example.com/login.php?invalid.
So everytime i refresh url remains
www.example.com/login.php?invalid.
How to remove invalid from url on refresh???
I think that by using the invalid GET variable you try to determine whether or not to display the error message to the user. This isn't really a good way to do so, due to the number of reasons, one of which made you ask this question.
You have a number of options instead, one of which would be using the session variables to store the error message. E.g., if the user login fails, you could store the message in your session:
if (badLogin()) {
$_SESSION['errorMessage'] = "Something's wrong";
}
and then on the login.php page you could try and see if it exists:
// ...your HTML...
if (!empty($_SESSION['errorMessage'])) {
echo $_SESSION['errorMessage']; // show it to the user
unset($_SESSION['errorMessage']); // so as not to display it every time
}
// ...your HTML continues...
This is not the perfect way either, but without knowing your application structure it's hard to suggest anything else.

Acquiring referral URL after a PHP redirect

This is the scenario: a user land on a page which redirects him at certain conditions with the following php lines
header("Location: /access/details/index.php");
die();
The problem is that the page /access/details/index.php should receive the referral URL correctly. I cannot insert an input tag because of the PHP redirect. What is the simpliest way to pass the URL to the redirect destination page, possibly without using other languages such javascript?
There is no way to tell the browser what URL to use for the referrer. Instead, you can pass the referrer as a get parameter in the redirect.
header("Location: /access/details/index.php?referrer=" . urlencode($_SERVER['HTTP_REFERER']));
Retrieve the previous referrer on your /access/details/index.php script by accessing the $_GET super global
$referrer = $_GET['referrer'];
Another option would be to skip the redirect altogether and do a forward. This keeps the current referrer intact.
include("/access/details/index.php");
die();

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.

how to redirect wrong entered url

My current url is http://domain.com/example.php/link=eg But if someone plays with url and puts url as http://domain.com/example.php/abcdefgh97654 - anything, my all functions all links from that page become inactive.
I tried using <?=HTTP_SERVER;?> before all links in my php files but as my website has user registration and sign in, when user signs in and clicks on any menu (link from php script). It throws on index.php page. But if user logs in again it works perfectly. In short user needs to log in twice to work everything perfect.
Basically I want two solutions one I want to redirect www dot
domain dot com/example dot php/abcdefgh97654 - anything (wrong url
page) to errorpage (I already done this in htaccess) but does not
work for above type.
List item
And want to solve two time log in problem.
If anyone has solution for this will be appreciated.
For you to do this, you have to know what values are supposed to be passed via $_GET variable to your page. Then you can create filter for the values and user header(); function.
Here is my suggestion:
<?php
$var=$_GET['val']
//get expected length if you need.
if(strlen($var)>9 or strlen($var)) {
$redirect=true;
}
//if you know what you are expecting
$vals=array('val1', 'val2, 'val3');
if(!in_array($var, $vals)){
$redirect=true;
}
//continue or replace your filters in this manner and asign your value to $redirect
if($redirect==true) {
header("Location:url"); // do this before anything else is printed to the page or else it will say headers already sent.
}
?>

Categories