php header location not redirected from 404 - php

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.

Related

ERR_TOO_MANY_REDIRECTS in PHP

My logout.php file is like this. Is there any mistake in my code
logout.php
<?php
session_start();
session_destroy();
header('Location:index.php');
exit;
?>
Here is my index.php file. If I am set $_SESSION['s_activId'] then it is working properly but when I am trying to put condition if $_SESSION['s_activId'] is not set at that time I want to pass header on index page sometimes it works sometimes it does not work.
<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
else
{
$wrong = '';
if(isset($_POST['submit']))
{
$checkLogin = "SELECT userName,password,userType
FROM user
WHERE BINARY userName = '".$_POST['userName']."'
AND BINARY password = '".$_REQUEST['password']."'";
$checkLoginresult = mysql_query($checkLogin);
if($userLoginRow = mysql_fetch_array($checkLoginresult))
{
$_SESSION['s_activId'] = $userLoginRow['userName'];
$_SESSION['s_password'] = $userLoginRow['password'];
$_SESSION['hg_userType'] = $userLoginRow['userType'];
if(!$_SESSION['s_urlRedirectDir'])
{
header("Location:index.php");
}
else
{
header("Location:reminder.php");
}
}
else
{
$wrong = "UserId And Password Is Not Valid";
}
}
}
include("bottom.php");
$smarty->assign('wrong',$wrong);
$smarty->display("index.tpl");
?>
The problem arise in the condition below in index.php:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
When you logout, you are calling session_destroy() on logout.php and redirecting on index.php and the condition above gets true as s_activId is not set in session and again you are redirecting on index.php (without setting s_activId in session). The above condition will be true until the variable s_activId set in session and because of this you are getting ERR_TOO_MANY_REDIRECTS error.
The solution is, on index.php set the variable s_activId in session before calling the header method. Refer the code below:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
$_SESSION['s_activId'] = true;
header("Location:index.php");
}
Dont redirect index.php to index.php. you having redirects loop. Also
if you have code below that also can fire add die in if because after
redirect code below still executes. I didnt read your code, maybe
there isnt problems with this but after
header("Location: lalala"); always add die(); or exit();

How do I Redirect to Login page using PHP

I am trying to redirect my php login page so that if user is authorised, it goes to a page (r_index.php) and if the user isn't authorised they go back to the login page (login.html).
This is my code:
<?php
if ("password"=="$password") { // Start the condition ?>
Manage classes
<?php } // End the condition ?>
<?php if ("password"=="") { ?>
Login
<?php }
?>.
What am I doing wrong? How should I resolve it?
replace your code with this:
<?php
if ("password"== $password) {
header("location:r_index.php");
}
else if ($password=="") {
header("location:login.html");
}
?>
If you want to redirect you should use:
header('Location: http://www.example.com/r_index.php');
in your code.
<?php
$accessGranted = false;
if($password == 'password') {
$accessGranted = true;
}
if($accessGranted) {
header('Location: r_index.php');
}
else {
header('Location: login.html');
}
exit;
Actually your syntax is wrong, else there is no problem of using HTML inside php. It will work well and good.
Just make sure not to put your variable inside quotes, and change the statement as follows:
if($password=="password")
and
if($Password==" ")

PHP redirect with URL variables

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);
}?>

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.

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