PHP redirect script - php

I want to redirect my site visitors form page B using my custom PHP script following this rule: If they come from page A, will be redirected to url 1, else i will display content/or url 2. I use this:
<?php
$referer = $_SERVER['HTTP_REFERER'];
$rest = substr("$referer", -8);
if($rest == "send.php")// if they come from my website page "send.php"
{
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://google.com\">";
}
else
{
echo "$rest";
include 'content.php';
}
?>
.
Anyone can help?

<?php
$referer = $_SERVER['HTTP_REFERER'];
if (strpos($referer,'send.php') !== false)
{
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://google.com\">";
}
else
{
echo "$rest";
include 'content.php';
}
?>
Instead of meta redirect you can also use header location.
header("Location:http://www.google.com");

Related

Redirect in PHP doesnt load my link?

I have 2 php files.
The first one handles the link and stuff. Here is a preview my code:
<?php
session_start();
$link=$_GET['redirect'];
if ($link == '1'){
$_SESSION['link'] = 'https://www.google.com/';
header('Location: selection_handler.php');
exit;
}
if ($link == '2'){
$_SESSION['link'] = 'https://www.facebook.com/';
header('Location: selection_handler.php');
exit;
}
if ($link == '3'){
$_SESSION['link'] = 'https://www.twitter.com/';
header('Location: selection_handler.php');
exit;
}
?>
This is my selection_handler.php
<?php
session_start();
$link= $_SESSION['link'];
if(isset($_SESSION['user_id']))
{
header("Location: " .$link);
exit;
}
else
{
echo $_SESSION['link'];
}
?>
user_id would be handled later so by default, i would get the echo of the link from the session, right? that works properly but when i try to test setting a value for the user_id, i see my browser trying to load the link. It says 'Resolving host... Waiting for [insert link selected]' but doesnt fully continue to the site but instead im shown a blank page?
UPDATE: I tried changing the header to ('Location: https://www.google.com') instead of getting from the variable but im still getting a blank page

meta redirect with php inside

I have a php file. In it I want an error redirect like this:
<?php
$ponka = somerandomsite.com;
if (1=1)
{echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://$ponka/error.php">'}
?>
How can I achieve it?
You php contains lots of errors, here's a working sample
<?php
$site = $_GET['site'] // will get the variable $site form a url like script.php?site=somerandomsite.com
$ponka = "somerandomsite.com";
if ($site == $ponka){
echo "<META HTTP-EQUIV='Refresh' Content='0; URL=http://$ponka/error.php'>";
}
?>
Also, remeber that php as a function called header
<?php
$site = $_GET['site'] // will get the variable $site form a url like script.php?site=somerandomsite.com
$ponka = "somerandomsite.com";
if ($site == $ponka){
header("Location: http://$ponka/error.php");
}
?>

Write text with echo() after reloading page with header()

I have page called account_settings.php and it's consist of change password, change profile pic, change user details (name, bio etc.). My question is how to write message with echo() after redirecting page with header().
Something like this:
if (true)
{
Do_Some_MySQL();
header("Location: account_settings.php");
echo "Success!";
}
else
{
echo "Error!";
}
Thank you for all replies. ;-)
You can't actually do something after sending a Location header - it is impossible.
Instead, you could use $_SESSION array value to perform your task. Like:
if (true)
{
Do_Some_MySQL();
$_SESSION['message'] = 'Error!';
header("Location: account_settings.php");
}
else
{
echo "Error!";
}
And then on your account_setting.php:
<?php echo $_SESSION['message'] ?>
This would be nice if the account_settings.php is not the same page as you currently are. Otherwise, you could use the following code:
if (true)
{
Do_Some_MySQL();
$error = 'Success!';
header("Location: account_settings.php");
}
else
{
$error = "Error!";
}
And on the same page:
<?php if($error) echo $error; ?>
Also don't forget to include session_start() on both pages if you didn't it yet.
I would use a SESSION variable:
on redirect-page:
<?php
#session_start();
if(true){
$_SESSION['success'] = 1;
header("Location: account-settings.php");
}
?>
and on account-settings.php:
<?php
#session_start();
if(isset($_SESSION['success'])){
echo "Success!";
unset($_SESSION['success']);
}
You cannot echo anything after you just redirected. The browser is already processing the request to redirect to another page, so it doesn't bother about displaying the message anymore. What you seem to be looking for is something called flash message. You can set a temporary message in the session and have it display on the new page. For example, in your account_settings.php page:
// Make sure you have an actual session
if (!session_id()) {
session_start();
}
if (true) {
Do_Some_MySQL();
$_SESSION['flashMessage'] = 'Success!';
header('Location: account_settings.php');
}
Then in your template file for account_settings, check if there is any flash message and display it accordingly (and unset it to avoid a loop):
if (isset($_SESSION['flashMessage'])) {
echo $_SESSION['flashMessage'];
unset($_SESSION['flashMessage']);
}
These people are correct...you can't send headers after a redirect. Although I think this would be a beneficial alternative. To send a GET request in your header and process it on the receiving page. They are suggesting to use $_SESSION vars, but you can use GET vars. Ex:
if (true)
{
//Do_Some_MySQL();
header("Location: account_settings.php?message=success");
//above has GET var message = Success
}
else
{
header("Location: account_settings.php?message=error");
}
On your account_settings.php page have this code:
if (isset($_GET['message'])) {
$message = $_GET['message'];
if ($message == "success") {
echo "Success";
} else {
echo "Error";
}
}
This removes the need of CONSTANT SESSION vars. and gives you plenty of flexibility.
header("Location: account_settings.php?message=No%20results%20found");
//%20 are URL spaces. I don't know if these are necessary.
If you need you can add more then one.
header("Location: account_settings.php?message=error&reason=No%20Results&timestamp=" . Date());
then account_settings.php can be:
if (isset($_GET['message'])) {
$message = $_GET['message'];
$reason = $_GET['reason'];
$time = $_GET['timestamp'];
if ($message == "success") {
echo "Success";
} else {
echo "Error: <br/>";
echo "Reason: $reason";
}
}
But remember GET exposes your messages in the browsers URL. So DON'T send sensitive information unless you secure it. Hope this helps.

PHP Redirect with Cookies

How do I redirect to a splash page once with cookies?
I'm setting the cookie on my splash.php page to this:
<?php
$expire = time()+60;
setcookie("no_splash", "1", $expire);
?>
On that page there's a link to my index.php with this:
<?php
if($_COOKIE['no_splash']== '1') {
header("Location: index.php");
echo "it works";
} else if($_COOKIE['no_splash']!= '1') {
header("Location: splash.php");
};
?>
I keep getting a redirect loop error but can't figure why.
You are redirecting to index.php from the index.php file, hence the loop.
Change your code to be simply
if($_COOKIE['no_splash'] != '1') {
header("Location: splash.php");
exit;
}
or indeed
if(!$_COOKIE['no_splash']) {
header("Location: splash.php");
exit;
}
which is the same thing.
$expire = time()+60;
header("Set-Cookie: no_splash=1; expires=$expire; path=/");
header("Location: index.php");
Have you tried simply:
<?php
if($_COOKIE['no_splash']== '1') {
echo "it works";
} else {
header("Location: splash.php");
};
?>
Maybe isset($_COOKIE['no_splash']) rather than `$_COOKIE['no_splash']== '1'?
Also, not sure it this is what you want, but you can set simply not set the expiration time (or set it to 0), and it will delete the cookie when the browser is closed, so if they keep it open, they won't have to go back to the splash.

PHP Redirect Loop

On my index page I have a link to my login.php page with this code:
<?php
if(isset($_SESSION['username'])) {
echo "<div id='logout'><a href='logout.php'>Logout (".$_SESSION['username'].")</a></div>";
} else {
echo "<div id='login'><a href='login.php'>Login (Regular)</a></div>";
}
?>
On the login.php page I have
<?php
include('check.php');
$ref = getenv('HTTP_REFERER');
if (isset($ref)) {
header("Location: " . $ref);
exit;
} else {
header("Location: index.php");
exit;
}
?>
check.php is the code for the login form and it checks the users level to make sure they can access the page. I was told that I need to add a check to see if the referral is login.php, otherwise it will go in an infinite loop and I am of course getting "This webpage has a redirect loop". However, I have no clue how to do this and I can't find any information on how to fix it. Anyone know a quick solution?
You should be able to just do
if (isset($_SERVER['HTTP_REFERER']) && end(explode('/',$_SERVER['HTTP_REFERER'])) != 'login.php') {
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
} else {
header("Location: index.php");
exit;
}
Note that this is a simplified code - you may need to be a bit smarter than that.

Categories