I have one header file for the home page and the login page
in that header. I wrote this code on top of it
<?php
if ($this->session->userdata('logged_in')==true ){
redirect('home','refresh');
}else{
redirect('home/login','refresh');
}
?>
but it keeps redirecting me to the same page and it shows nothing
Remove the else redirect on login page, its already in login page
if ($this->session->userdata('logged_in')==true ){
redirect('home');
}
except login, you can redirect the page :)
to differ the login page and home page just send the
$this->data["pagename"]="login"; in login function and
$this->data["pagename"]="home"; in home function
and in header
if ($this->session->userdata('logged_in')==true ){
redirect('home');
} else {
if($pagename!="login")
{
redirect('home/login');
}
}
$user_logged_in=$this->session->userdata('logged_in');
if (!$user_logged_in)
{
redirect('home/login','refresh');
}else{
redirect('home','refresh');
}
<?php
if ($this->session->userdata('logged_in')!==FALSE){
redirect('home','refresh');
}
else{
redirect('home/login','refresh');
}
?>
try the above code..
Try this
put these line in every controller's __construct function
if ($this->session->userdata('logged_in') !=true) {
if ($this->router->fetch_class() != 'home' && $this->router->fetch_method() != "login") {
redirect("home/login");
}
}
Related
I tried to set custom redirects for specified files, like if url contains wp-login.php then redirect to the home url, the first statement is working and redirect the page to the home url but in the else i got this error:
ERR_TOO_MANY_REDIRECTS
Here is my code in functions.php
function secure_redirect(){
$page_viewed = basename($_SERVER['REQUEST_URI']);
if($page_viewed != "wp-login.php?secure_login")
{
if(strpos($page_viewed, "wp-login.php") !== false)
{
wp_redirect(get_site_url());
}
}
else
{
wp_redirect(get_site_url()."/".$page_viewed);
}
}
add_action('init','secure_redirect');
If you only want specific to redirect then why you have put redirection code in else part also? Skip the else part. Write redirection code only on if part
function secure_redirect(){
$page_viewed = basename($_SERVER['REQUEST_URI']);
if($page_viewed != "wp-login.php?secure_login")
{
if(strpos($page_viewed, "wp-login.php") !== false)
{
wp_redirect(get_site_url());
}
}
}
add_action('init','secure_redirect');
I want to be when the back button on the browser is clicked. system will redirect to previous page or to a specific page.
I've made a function to check the login session like this
public function cekAuth(){
/* Mengecek login session
*/
$sesi = $this->ci->session->userdata('isLogin_'.project);
$key = $this->ci->uri->segment('1');
if ($sesi == TRUE && $key == "access") {
redirect('dashboard/home');
exit();
} else if($sesi == FALSE) {
redirect('access');
exit();
}
}
if the login session is TRUE and the user accesses the login page will be redirected to the dashboard. This has been successfully.
but when I logout always redirected to the dashboard and not to the login page.
LOGOUT:
function logout(){
$this->session->sess_destroy();
$this->session->set_userdata('isLogin_'.project , FALSE);
redirect('access');
}
Can you help me? :)
It would be great first please check your condition is perfect or not, May be if you will do on this ways get idea where is problem.
public function cekAuth(){
/* Mengecek login session
*/
$sesi = $this->ci->session->userdata('isLogin_'.project);
$key = $this->ci->uri->segment('1');
if ($sesi == TRUE && $key == "access") {
echo '1';
exit();
redirect('dashboard/home');
} else if($sesi == FALSE) {
echo '2';
exit();
redirect('access');
}
}
Change this line in logout function:
$this->session->set_userdata('isLogin_'.project , FALSE);
To
$this->session->unset_userdata('isLogin_'.project , FALSE);
I want go to the home pages as the privilege of user that current logged in.
When i am trying to open the page that having the link it goes to the home page. What changes that i need on my code
<body>
Home
</body
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
header('location:admin_home.php');
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
header('location:home.php');
}
else
{
header('location:user_home.php');
}
}
Your function is being used inside an <a href="...">, so it's clearly supposed to return a URL (also you need to echo it)
Your current code is trying to redirect the user immediately, which won't work because you've already sent <a href=".
Try:
function home() {
if($_SESSION['privillage'] == "ADMIN") return "admin_home.php";
if($_SESSION['privillage'] == "SUPERVISOR") return "home.php";
return "user_home.php";
}
header('location is used if you want a PHP script to redirect the browser to another page. What you are trying to do is simply changing the href based on certain conditions.
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
return 'admin_home.php';
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
return 'home.php';
}
else
{
return 'user_home.php';
}
}
For readability, you could consider using PHP's switch statement
may be you have to capitalize the first letter of "location" use "Location" in header('Location:user_home.php'); :)
I have set category page my default home page.But now i want to show CMS(Home page) for not logged in user while for logged-in i want to show category page.Means how i can set logic that can load both cms pages. How can i do that ? Thanks for any help in advance
<?php if(!Mage::getSingleton('customer/session')->isLoggedIn()): ?>
load->category page
<?php else: ?>//If user is NOT logged in
Load home page default one
<?php endif; ?>
IF you want only to check condition on home page .You can do like this .Change little bit code for CMS controller like this :
class Mage_Cms_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction($coreRoute = null)
{
if(Mage::getSingleton('customer/session')->isLoggedIn())
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('catalog/category/view/id/3'));
}else{
$pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE);
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('defaultIndex');
}
}
}
Try and inform me about results.Hope this will solve your issue
This might work in your case :
// condition depending on login status of user
if(!$this->helper('customer')->isLoggedIn())
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getBaseUrl());
die();
}
else
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('YOUR_CATEGORY_URL'));
die();
}
IF above doesn't work, try this :
$sessionCustomer = Mage::getSingleton("customer/session");
if($sessionCustomer->isLoggedIn()) {
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('YOUR_CATEGORY_URL'));
} else {
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getBaseUrl());
}
I have an index page, I want it to include a page called splash.php and not display.php when a user lands on index.php, but once a user does something (sets a variable) ie if a user searches (variable "query") i want it to include display.php and not include splash.php
What is wrong with this code?
function hasGet()
{
return !empty($_GET['fact']);
return !empty($_POST['query']);
}
if (hasGet()) {
include("display.php");
}
else {
include("splash.php");
}
This question should be removed
Only the first return statement is executed. Try:
return !empty($_GET['fact']) && !empty($_POST['query']);
A better way to accomplish what you are trying to do is use sessions.
index.php
<?php
session_start();
if (!isset($_SESSION['visited'])) {
$_SESSION['visited'] = true;
include 'splash.php';
} else {
include 'display.php';
}
?>
This way after a user visits index.php for the first time, $_SESSION['visited'] is set to true and it won't show the splash page throughout their visit.
You cannot have two returns as you are doing. Try
return (!empty($_GET['fact']) && !empty($_GET['query']));
You might want to try this...
if($_SERVER["SCRIPT_NAME"] == "/index.php"){
include("splash.php");
}else{
include("display.php");
}
2.
if(!empty($_GET["fact"]) || !empty($_POST["query"])){
include("display.php");
}else{
include("splash.php");
}