PHP redirect with URL variables - php

I'm very new to php and trying to build a redirect which uses parameters from a URL which will differ from person to person.
The URL a person will access looks like: www.website1.com/redirect.php?p=p123&r=4&s=567
The p, r, and s will change for each person
I then want to redirect them to some other site that looks like this:
www.website2.com/p123.aspx?r=4&s=567
Here is what I have so far, but it's giving me an error "Cannot modify header information - headers already sent by ..."
<html>
<?php
$fpvalue = $_GET['p'];
$frvalue = $_GET['r'];
$fsvalue = $_GET['s'];
header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
?>
</html>
I would really appreciate the help for a beginner.
Thanks!

You can't do a redirection when you have been sent a "content" (text, html, space, wherever).
You should NOT do this before calling the header() function.
As you can see, you have a "" before calling the header() function.
Change that:
<html>
<?php
$fpvalue = $_GET['p'];
$frvalue = $_GET['r'];
$fsvalue = $_GET['s'];
header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
?>
</html>
For that:
<?php
$fpvalue = $_GET['p'];
$frvalue = $_GET['r'];
$fsvalue = $_GET['s'];
header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
exit;
?>
<html>
</html>
And remember: Check if there is another previous space or "new line" before the "< ?php " tag.

The error "Cannot modify header information - headers already sent by ..." caused when you place session_start or php header below other codes, e.g. html
then you should change into:
<?php
//your php codes
//....
?>
<!DOCTYPE html>
....etc.
This must work

This instructions works for me to fix seo redirects, but you use for all
example url: http://yourdomain.com/index2.php?area=xpto
example index2.php file:
<?php
$fpvalue = $_GET['area'];
if ($fpvalue == 'xpto') {
header("Location: http://newurl.com/", true, 301);
}
else if ($fpvalue == 'loren') {
header("Location: http://otherurl.com/", true, 301);
}
else if ($fpvalue == '') {
header("Location: http://otherurl.com/", true, 301);
}?>

Related

Php redirect after successful login

I'm having an issue with a simple verification file, it doesn't redirect to index page after successful login.
Basically the login.php file has the html form for login, the form calls auth.php file which already has the login data and decides if your login and password is correct or not. Now it should redirect to index.php after successful login but it doesn't , instead it just cleans up the form in the login.php file and you keep trying , BUT if you refresh the page ( after successful login ) you get auto redirected to index page.
Fixed! changed the code to something even simpler than that.
if($logindata[$_POST["username"]]==$_POST["password"])
This bit doesn't look correct; maybe you were looking for:
if($logindata[$_POST["password"]]==$_POST["password"])
Sometimes headers does not work well for some reasons, instead try to use a simple html redirect like this:
<?php
$usernames = array("user1", "user2");
$passwords = array("pass1", "pass2");
$page = "index.php";
for($i=0;$i<count($usernames);$i++){
$logindata[$usernames[$i]]=$passwords[$i];
}
$found = 0;
for($i=0;$i<count($usernames);$i++) {
if ($usernames[$i] == $_POST["username"]) {
$found = 1;
}
}
if ($found == 0) {
$redirect_url = "./login.php?login_error=1"
}
if($logindata[$_POST["username"]]==$_POST["password"]) {
session_start();
$_SESSION["username"]=$_POST["username"];
$redirect_url = "./index.php"
}
else {
$redirect_url = "./login.php?login_error=2"
}
echo "<center><br><br><br><p>You will be redirected in about 2 seconds. If not, click this link: <a href='$redirect_url'>Back</a></p></center>";
?>
<html>
<head>
<meta http-equiv="refresh" content="2;url='<?php echo "$redirect_url"; ?>'/>
<title>Redirecting...</title>
</head>
</html>
<?php
exit;
?>
I presumed the redirect location is in the same folder of the php file. Adjust the var $redirect_url path of they aren't.

php header location not redirected from 404

Hi friends i have one problem with my redirect page php section.
This is my php redirect section:
<?php
session_start();
if (!(isset($_SESSION['uid']) && $_SESSION['uid'] != '')) {
header('Location: '.$base_url.'index.php');
exit;
}
include_once 'includes.php' ;
if($_GET['user_name']){
$user_name=$_GET['user_name'];
include_once 'public.php';
}
if(empty($_GET['user_name'])) {
header(location:$url404);
}?>
$url404" is in includes.php
$url404=$base_url.'404.php';
The problem is page not redirect if username empty
You forgot to include ", so it's not seeing the header as a string.
Change header(location:$url404); to header("Location:" . $url404);
Use this code:
if(!isset($_GET['user_name']) or $_GET['user_name'] == "") {
header("location:".$url404);
Hopefully this will help you.

PHP Header redirect and echo out session on next page?

i am submitting a form and then using header redirect to take the user to a new page. how can i add a session to my header redirect to say once user has been redirected echo out a div within a session saying something like form submitted?
heres what i have tried to do but can not get it to work, can someone please point me in the right direction, thanks.
submit_form.php:
header("Location: ../index.php?success=$success");
index.php:
<?php echo $_SESSION['success']; ?>
<?php $success= "<div> CONGRATULATIONS!!!!!</DIV>"; ?>
A session value is something stored in a session started with session_start().
What you have is a URL query parameter, which you can access with $_GET['success'].
submit_form.php:
session_start();
$_SESSION['success'] = true;
header("Location: ../index.php?success=$success");
index.php:
session_start();
if (isset($_SESSION['success']) && $_SESSION['success']) {
//Echo your div
}
You appear to be mixing up $_SESSION and $_GET
Try below code:
On submit_form.php page:
$_SESSION['success'] = "YOUR SUCCESS MESSAGE";
header("Location: ../index.php");
On index.php page:
if(isset($_SESSION['success']) && $_SESSION['success']!=""){
echo $_SESSION['success'];
unset($_SESSION['success']);
}
On both page, on top, put below code:
session_start();

SESSION isset, redirect

This is my code for userslist.php. I put it above the head of this page so if this link is clicked, only admin can enter the page as filtered that is why I have redirections.
session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(
isset($loggedInfo['username']) && $loggedInfo['username']==="admin" &&
trim($loggedInfo['username']) != "guest"
)
{
header('Location: userslist.php');
}
else {
header('Location: ../index.php');
}
This is my php script and I got a problem with redirecting. On the header(location ...) when I changed it to echo true or false, the echo returns the value correctly. But when I put a redirect/location, it does say:
This webpage has a redirect loop
Why is that? :(
Put this code in top of the userlist.php.An try what you got
<?php session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(isset($loggedInfo['username']) && $loggedInfo['username']!="admin"){
header('Location: ../index.php');
exit();
}else if(isset($loggedInfo['username']) && $loggedInfo['username']=="admin"){
?>
You page code here goes
<?php } ?>
You're probably including this code in all pages. Thus on userslist.php it will also redirect to userslist.php. This causes permanent redirects, which is a redirect loop.
This conclusion is however difficult to support without seeing all the code you are using.

Redircting to a page in PHP [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I am trying to make a redirect to page based on its referrer. the layout is like
<?php
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
header("Location: url of another site");
exit();
}
else {
header("Location: my home page");
exit();
}
?>
I am getting the error : Cannot modify header information - headers already sent by.
I have to modify my php.ini to output_buffering = On but I can't do this as I have a shared hosting account in hostgator.
can anyone suggest me what options now I have? can I do it by changing my code? If yes, than what changes?
<?php
function custom_redirect() {
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
header("Location: url of another site");
exit();
}
else {
header("Location: my home page");
exit();
}
}
add_action('init','custom_redirect');
?>
Try hooking your function to INIT HOOK
Hi actually header("Location: url of another site"); will not support in wordpress. Instead of this you can use
<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>
and for home page just use <?php wp_redirect( home_url() ); exit; ?>
this will solve your problem of redirection.
try using javascript instead
<?php
$URL_REF = $_SERVER['HTTP_REFERER'];
if($URL_REF == "url of my site") {
?>
<script type="text/javascript">window.location = "http://www.yoururl.com";</script>
<?php
}
else {
?>
<script type="text/javascript">window.location = "http://www.anotherlocation.com";</script>
<?php
}
?>
check the reference here
Header Information already sent is a wordpress's common error, like as you mentioned you want to redirect the web site then you have to options to follow.
Create a wordpress Template file do not call header in that file
<?php /* Template Name:Redirect*/ ?> <?php //get_header();?> <!-- Do not un comment this function -->Write your code here Write your code here Write your code here
Write your code here <?php get_footer();?>
Or you can redirect with java script's window.location function
<script>document.write(location.http://google.com);
</script>

Categories