I have made this redirect depending on a condition.
I have a CMS, and I'm developing a mobile version of the site. The main CMS is linked to site_url.com while the mobile version is located on a subdomain m.site_url.com. Below function is an edited logout function in the mentioned CMS.
CodonModule is included as a php include in the index.php of the mobile site. This loads all of the CMS' functions basically.
Does the solution I've came up with allow such a redirect that I've done below, or do I require something else to find the subdomain, and based on that information, complete a redirect? At the moment, it will always redirect me to the else argument's link, regardless of where I trigger the script, whether on the subdomain, or the main domain. I'd like therefore to fix the script below, and for the condition to work.
Auth::LogOut triggers the logout process. I then add a redirect for the user to be redirected either to a desktop page (else argument) or mobile version (if they are on the subdomain).
When I did $die(subdomain) just below the $subdomain line, it would print out the name of the domain. Shouldn't it print out m instead for the subdomain, or have I made a mess of this code?
class Logout extends CodonModule
{
public function index()
{
$sub_domain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
if($sub_domain == 'm')
{
Auth::LogOut();
header('Location: http://m.site_url.com/index.php');
}
else
{
Auth::LogOut();
header('Location: '.url('/'));
}
}
}
Try to add session_start(); at the top of the File right below <?php where u defined the index() function, If I'm Right the Sessions didn't even start (IDK), But Try to add it..
Maybe it will work..
Related
Dears,
I create a function that permits to redirect the user to a specific URL. The function is named redirect and user the header function:
public function redirect($url, $code = null)
{
if($code == 301){
header("HTTP/1.1 301 Moved Permanently");
}
header('Location: '.Router::url($url));
die();
}
I use it to protect some pages to be reachable for a none authenticated users. I put a condition, permits to check if user is logged, if not will be redirected to the login form:
if(!$this->Session->isLogged()){
$this->redirect('users/login');
}
The function isLogged is:
public function isLogged()
{
return isset($_SESSION['User'][0]->id);
}
I tried to do the same things, for BO admin pages and it works. But I need to protect the front pages too. The problem is when I try to access to a front-page, I'm redirect to the login form, but I got the ERR_TOO_MANY_REDIRECTS error on my Chrome browser.
I tried to delete cookies, but I have the same problem. When I try to ignore the following line, I can see my login form:
if(!$this->Session->isLogged()){
$this->redirect('users/login');}
You should not attempt to redirect to login in case you are already on that page.
if(!$this->Session->isLogged() && !$this->isLoginPage()){
$this->redirect('users/login');}
}
PHP raises this error in two main cases:
Infinite redirecting: you're probably invoking your mentioned function for an infinite number of times
Too many redirects in a row: the "Header" native PHP's function handle only one header per instance.
In a nutshell: the error related to your case is quite surely related to an infinite redirect, otherwise there could be other redirects invoked moreover the wanted one in the page where you're calling your function.
I am using wp-login with some modifications for a project.
Originally I was using this code in the theme's functions.php to have users redirected to the main page after login and everything worked like a charm:
/* redirect users to front page after login */
function redirect_to_front_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = get_option('siteurl');
}
}
add_action('login_form', 'redirect_to_front_page');
Then I began testing the site and realized it would be better if people were redirected to the last page they visited. That way if they were viewing content other than the main page and decided to log in to comment they wouldn't get booted back the main page.
I then altered the code to read like this:
/* redirect users to last page viewed after login */
function redirect_to_last_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = $_SERVER['HTTP_REFERER'];
}
}
add_action('login_form', 'redirect_to_last_page');
It works perfectly except in the case of a failed login. In that case they get sent back to wp-login with an error message, which is fine. But when they try to log in a second time and are successful, instead of redirecting back to the site content page they came from they are being redirected back to wp-login with no explanation.
I can see it remembers the instance of wp-login without the error as the referer and that this is creating my problem. But I don't have a good mental picture of how to make this situation conditional, nor has anything I've read made any sense in the context of my problem.
UPDATE:
I got it to work by starting a session and using the strstr() function. This code was added at the very top of the wp-login.php file:
session_start();
$lpage = $_SERVER['HTTP_REFERER'];
if(strstr($lpage, 'login.php') == false)
{
$_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];
}
Then I changed the code in functions.php to this:
/* redirect users to last page viewed after login */
function redirect_to_last_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = $_SESSION['referrer'];
}
}
add_action('login_form', 'redirect_to_last_page');
However, I would rather not be making changes to Wordpress core files. So if anyone can suggest a way to move all this into functions.php it would be appreciated. I tried to just add the code into functions.php instead of wp-login but it throws an error.
If someone spots a potential security problem that would also be helpful.
Hey so I have create a login system to a website and I wish to have this login appear when I type in my address. When I have typed in details and logged in, I wish to be redirected to another PHP file, but with the same address.... this way, All I need to do is type in my address if I am allready logged in and I will go to the site which requires login.
I have made a transaction happen identifing if the session is created, if it is, it redirects me to another page, but also to another URL. I tried googleing it, but couldn't find anything exact and straight forward.
Currently:
Login page:
www.example.com
Member page:
www.example.com/members
What I wish for:
Login page:
www.example.com
Member page:
www.example.com
The program structure should look like this.
index.php
if (user is logged in)
display dashoard
else
display login page
Since you are using PHP, make use of session functions. Thus, URL rewriting is no longer necessary.
Update
Assuming if you have file structure in PHP like this:
- index.php
- login.php
+ template
- login.php
- dashboard.php
You can do the following structure in index.php file.
define('IN_FILE', true);
if (isset($_SESSION['user'])) {
require 'template/dashboard.php';
} else {
require 'template/login.php';
}
In template/dashboard.php
if (!defined('IN_FILE')) {
exit;
}
// Then your HTML, PHP and whatnot
And in login.php
if (!isset($_SESSION['user'])) {
require 'template/login.php';
} else {
header('Location: index.php');
}
Change the code according to your needs.
This can be achieved using several approaches.
a) Use session to determine the current page, so if a user click on a link, create a session store the value and on page load read the session data and include the file accordingly.
b) Use URL parameter to determine the page (this is the most common approach). for example in index.php you can add more parameters like index.php?page=somepage and by reading the value using $_GET and including the PHP file accordingly.
There are some more way to achieve what you want to, for instance using javascript/jQuery this is possible.
I want to create a function that will check if the user is logged in. If so, the user should be navigated to userActive, otherwise the loginForm should be displayed.
I also want to carry out a check on the loginForm page to check if the user is logged in. If so, they should be taken to userActive, or kept on the loginForm page.
Why issue is why the following code doesn't work. I keep hitting a loop.
$_COOKIE['user_id'] = "user_id: 1";
activeUser();
function activeUser()
{
if($_COOKIE['user_id'] == "user_id: 1")
{
header('location: index.php?a=activeUser');
}
else
{
header('location: index.php?a=loginForm');
}
}
switch($_GET['a'])
{
case 'loginForm':
include_once('loginForm.php');
break;
case 'activeUser':
include_once('activeUser.php');
break;
}
That is not the proper way to set cookies in PHP. See the documentation for details.
If you are putting this on your index page... you are redirecting to your index page with a GET variable.... so... loop after loop to the same page.
You are not setting cookies properly as well.
You may want to research SESSION variables as well as that may be easier to grasp when implementing a login system.
This is how most frameworks do it :
Redirect all of the request to a single page : index.php
In this file load all the functions/classes you need (bootstrap), this could be :
Is the user logged in (restricted access)?
what country is he from (show content near him) ?
what language does he speak (site in english or spanish...) ?
...
finally : where does he want to go (the page), can he go there? if not redirect.
Building all of this from scratch could be tricky, sessions, cookies, redirects, layouts, views, 404, 500, databse queries (safe ones)...
Most people use frameworks, here is a list of some of them :
CakePHP
CodeIgniter
Symfony
Zend Framework
Hope it helps.
I'm creating a SaaS web app in codeigniter and i'm trying to determine how to route to a specific controller based on whether a subdomain exists or not.
Currently I have it so if you put a url of subdomain.example.com, my default controller checks whether the subdomain in the url exists in the database, and if it does not, it displays the error page.
public function __construct()
{
parent::__construct();
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2); //creates the various parts
$subdomain_name = $subdomain_arr[0]; //assigns the first part
//echo "subdomain_name = $subdomain_name";
// if the subdomain does not exist, redirect to error page
if(!$this->subdomainExists($subdomain_name)) {
redirect('error/index');
} else {
$this->subdomain = $subdomain_name;
}
}
This works great for urls where a user enters a subdomain, but now if the user enters a url without a subdomain such as example.com, I want a different controller to be used.
What is the best way to achieve this? I was thinking of doing the following, but it doesn't seem best to have a redirect occuring every time someone goes to example.com.
// if no subdomain was entered, redirect to controller 'aDifferentController'
// else if a subdomain was entered, check that it exists in the database and if not redirect to an error page.
if(the url entered does not contain a subdomain) {
redirect('aDifferentController');
} else if(!$this->subdomainExists($subdomain_name)) {
redirect('error/index');
} else {
$this->subdomain = $subdomain_name;
}
Why not conditionally declare routes based on the subdomain.
in routes.php, do this
if (strstr($_SERVER['HTTP_HOST'], 'some-subdomain.mydomain.com')) {
$route['uristring'] = "controller/method";
}
You need to work out what the most likely scenario is, are more people likely to go to the subdomains, are are they more likely to go to the main site?
If they are more likely to go to the main site then do a check if the sub domain exists, if it does then redirect to the other controller, else, continue.
If they are more likely to go to the sub domains then check if a sub domain exists, if it does not then redirect to the other controller, else continue.
The other option (and possibly better in my opinion) is to do the redirect using .htaccess before it even hits code igniter, but that depends on what level of access you have to your server.