I have function within my login script that is causing me some problems. I believe this is the issue.
function load( $page = 'login.php')
{
$url = 'http://' . $SERVER['HTTP_HOST'] .
dirname( $_SERVER ['PHP_SELF'] );
$url = rtrim( $url , '/\\' );
$url = '/' . $page ;
header ( "location: $url" );
exit();
}
My users are in the db and can register fine - when using my login form I have a login form a script to check they have entered the info and also another one to validate the users. The problem being that when I/They attempt to login the action script or the logic dosnt move on it sits on that blank page...
Thats the white page of death :) There is an error, but it doesnt get shown. You can try adding this to the top:
error_reporting(E_ALL|E_STRICT);
That should give you the error on line 5 in this code, the $SERVER needs to be $_SERVER
To improve your code, this is valid too:
header('Location: /');
Relative url's. That piece of code will go to home, you can just to to your $page directly:
header('Location: /'.$page);
function load($page = "login.php") {
header(sprintf("Location: http://%s/%s/%s", $_SERVER["HTTP_HOST"], trim(dirname($_SERVER["PHP_SELF"]), "/"), $page));
exit();
}
Related
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.
I realize there are many question on PHP headers() however I could find nothing which specifically addresses my question based on my very simple piece of code.
My Problem
So I have this very simple block of code
$url = $_SERVER['REQUEST_URI'];
$profile = explode('/', $url);
if ($profile[3] == 'index2.php') {
// echo $profile[3] prints index2.php
header('Location: profile.php');
exit();
}
As can be seen on the image below the code results in an infinite redirect loop.
when I set an absolute url like header('Location: http://localhost:63342/mvc2.0/users/profile.php'); I still get the exact same problem. When I go incognito or use a different browser I still get same problem...
If anyone can provide me with some info or suggestions to what I am doing wrong it would be greatly appreciated.
You could probably simplify that to
$profile=basename( $_SERVER['REQUEST_URI'] );
if ( $profile == 'index2.php' ) {
exit( header( 'Location: profile.php' ) );
}
I played around with this and I'm having trouble getting it to work. I've added it at the bottom of the functions.php file in my themes folder.. no dice.. It should be redirecting to mySecretString=foobar... When i go to wp-admin it just takes me to the admin page to login.. that page should be accessible anymore, it should only be accessible by whatever the variable $QS equals.
// Simple Query String Login page protection
function example_simple_query_string_protection_for_login_page() {
$QS = '?mySecretString=foobar';
$theRequest = 'http://' . $_SERVER['SERVER_NAME'] . '/' . 'wp-login.php' . '?'. $_SERVER['QUERY_STRING'];
// these are for testing
// echo $theRequest . '<br>';
// echo site_url('/wp-login.php').$QS.'<br>';
if ( site_url('/wp-login.php').$QS == $theRequest ) {
echo 'Query string matches';
} else {
header( 'Location: http://' . $_SERVER['SERVER_NAME'] . '/' );
}
}
add_action('login_head', 'example_simple_query_string_protection_for_login_page');
Can anyone help me get this working, it seems like it should be so easy but nothing is working for me.
Thanks in advance.
I'm not understood your requirements fully where you want to exactly redirect, it would always helpful to you when you use .htaccess files in root folder, where you can do necessary redirection instead of writing functions.
Redirect 302 /about http://www.example.net/our-company-info/
http://www.woothemes.com/2013/11/htaccess-url-redirects/
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')
I have a problem with the following code:
<?php
session_start();
require_once("config.php");
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
$location = $_SESSION['location'];
$url = ABS_PATH . $location;
unset($_SESSION['location']);
header('Location: ' . $url);
}
The value of $url is:
http://www.domain.eu/somepage
and value passed to header() is:
Location: http://www.domain.eu/somepage
It is ok under Opera, IE (7,8,9) and Firefox but unfortunately it is not working under Chrome and I have no idea why. Mentioned code sample comes from file index.php and it is execuded every time You load a page. I have other file (display.php) to which I send url via AJAX request. File display.php sets value of $_SESSION['location'] and returns content of page. In browsers other than Chrome when I refresh page it redirects me to proper URL. During debuging I noticed that even when I put die(); after
unset($_SESSION['location']);
it does not execute it, but when i put it before it works. Can anyone have any idea how to solve my problem?
Thanks in advance for help.
<?
session_start();
$_SESSION['location'] = 'http://www.google.com';
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
$location = $_SESSION['location'];
header("location: ".$location);
}
?>
Works for me. Expand on that code and see when it breaks.
Add a die; right after your header:location. This should do the trick.
Chrome needs an http status first:
header("Status: 200");
header('Location:' . $url);