Redirecting a page Based on URL Value - php

I am trying to redirecting my register page to https:// based on parse url value.Below is my code
$url = $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$parts = parse_url($url);
parse_str($parts['query'], $query);
if ($query['view']=='register') {
$porthttp = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header("Location: " . $porthttp);
exit();
}
But it is giving error 'server is redirecting the request for this address in a way that will never complete.'
what's wrong i am doing?

You are redirecting on same page which you accessing so it will not work.
I.e http://localhost/demo/test.php?view=register and you are checking condition like if ($query['view']=='register') {} then you are redirecting on same page using $_SERVER['REQUEST_URI'] so it will go to infinite loop.

Related

I want to redirect website visitor from "expl.com/... "to "expl.com/index.php/..."

This is my domain http://cdtr.cf it lands on index.php
Where I wrote some code which pulls the URL from URL box and trim its suffix to identify visitor's identity.
The Problem is when someone opening http://cdtr.cf/.... it shows error 404 page not found
while just http://cdtr.cd/ is working fine. All I need is When someone visit with http://cdtr.cf/.... they must be redirected to index.php and their request should be processed like http://cdtr.cf/index.php/.....
I want to keep the suffix anyhow for some purpose :-http://cdtr.cf/suffix.
It will be great if all this happen without any visible change in URL box.
Thanks in advance.
$link= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=== 'on'? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $link; //It is working fine on localhost but not when i put it live
This code will give you the rest of the url as per your requirement
<?php
$link = $_SERVER['REQUEST_URI'];
echo $link = ltrim($link, '/');
//if you want you can trim last / too using $link = rtrim($link, '/');
?>
If you want to get middle stuff like this yoursite.com/stuffs/removedthis
then you have to use below code
<?php
$link = $_SERVER['HTTP_HOST'];
$link .= $_SERVER['REQUEST_URI'];
$link = explode('/', $link);
echo $a = $link[1];
?>
Second example:
$link = "example.com/stuff/removethis/....blah";
$link = explode('/', $link);
echo $a = $link[1];

Variable php redirect based on url

i'm creating a index.php file for redirect all website to specific host
I'd like create a little php script that read url and redirect based on specific filter.
for example:
if url = (everything).domain1.com redir to default1.php
if url = (everything).domain2.com redir to default2.php
in all other case that not like first or second redir to default3.php
this is possible with php? i must use $_SERVER['HTTP_HOST'] or can I use other method?
i resolved with:
<?php
$domain = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($domain,'domain-name.com') == true) {
header('location: /index2.php');
exit();
} else {
header('location: /index3.php');
}
?>
but not redirect...

PHP redirect with final URL as a parameter

I'm trying to send traffic to my site, then have it redirect to a domain that is specified in the campaign's URL. What I've tried so far is this php on a directory "redirect" within my site:
I would like to send the traffic to this URL, where the redirect takes it to the sample domain:
http://www.example.com/redirect?link="http://www.sampledomain.com"
<?php
// accepts a url as a query argument, checks to be sure request came from
site
//
if (!isset($_SERVER['HTTP_REFERER']) ) die('not authorized');
$host = $_SERVER['HTTP_HOST'];
$referer = explode('/', $_SERVER['HTTP_REFERER']);
if ($referer[2] != $host) die('not authorized (2)');
$transfer = filter_input( INPUT_GET, 'link', FILTER_SANITIZE_URL );
header('location: ' . $transfer);
exit();
?>
With this method I keep getting a 403 error. Please help with any suggestions.
Thanks!

Redirect visitor based on referral URL

I want to redirect visitors to one page on my website if they have come from a certain URL and another if they don't.
<?
$referer = $_SERVER['HTTP_REFERER'];
if ( $referer != "http://URL1" ) {
header('Location: page1');
exit;
} else {
header('Location: page2');
}
?>
Whatever referrer I come to the page on it goes to page 1 and never to page 2. I have used this code in a index.php file so its the first page the visitor is directed too.
UPDATE: alright, so from the discussions, it seems that the reason why your code won't work is that you are checking the referer string using the "now-www" url, while the actual referer string has "www" in the url. Please, make sure to use the exact referer string. Otherwise, if you are redirecting based on the hostname of the referer you can use the updated answer below.
<?php
$referer = str_replace("www.", "", parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
switch($referer) {
case "johnchow.com":
header("Location: page1");
break;
case "domain2.com":
header("Location: page2");
break;
default:
header("Location: page3");
}
exit;
For starters change this to
if ( $referer != "http://URL1" || $referer != "http://URL2" )
Secondly, page1 and page2 are likely giving the error because they are invalid. Include the path and extension. For example:
header('Location: http://www.yourlocation/page1.php')
Looks like the error has been clarified...

PHP Redirect to Selected Page After Login

I'm trying to find a way to redirect a user to the page they selected if they have been forced to log in again after a session timeout.
Right now, after the user logs in, they are redirected to index.php. But, if a user received a link in an email to a different section of my site and they have not logged in all day, they are obviously asked to log in but end up on the index.php instead of the page the link was for.
Here is a snippet of my code in the login page:
if (mysql_num_rows($result_set) == 1) {
// username/password authenticated
// and only 1 match
$found_user = mysql_fetch_array($result_set);
$_SESSION['user_id'] = $found_user['id'];
$_SESSION['username'] = $found_user['username'];
$_SESSION['last_activity'] = time();
$_SESSION['time_out'] = 7200; // 2 hours
redirect_to("index.php");
Any ideas would be helpful.
I want to thank everyone who answered my question. The solution was a combination of a few suggestions I received here. I'd like to share with you how I solved this:
Part of the reason why I was having trouble saving the url the user tried to go to before being forced to log in again was that my sessions are handled by an external php file which takes care of confirming login and expiring current session. This file is required by all pages on my website. HTTP_REFERER would not work because it would always be set to the login.php. Here's what I did on my session.php file:
session_start();
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')
=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;
if ($currentUrl != "http://domain.com/login.php?") {
$expiryTime = time()+(60*5); // 5 mins
setcookie('referer',$currentUrl,$expiryTime,'/');
}
Essentially, I saved the referring url in a cookie that is set to expire in 5 minutes, giving the user enough time to login. Then, on login.php I did this:
if(isset($_COOKIE['referer'])) {
redirect_to($_COOKIE['referer']);
} else {
redirect_to('index.php');
}
And, BINGO! Works every time.
Try this:
$actual_link = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
if($actual_link == 'the email link'){
header('Location: '. $actual_link);
}else{
header('Location: index.php');
}
Try to save the URL in session whenever a user hit any url like http://www.abc.com/profile.php
once the user has successfully logged in redirect the user to saved URL(which is in session)
it the previous page is in the same directory and then you can try header('Location : .') or else if you if you need to redirect somewhere else.save the path before that situation occurs and in $url then u can redirect using header('Location: $url') or header('Location $url?values')

Categories