Error on function php - php

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'); :)

Related

No navigation bar displayed after log in

I have 3 different navigation bars, a different 1 has to be loaded depending on the authority of the user. I have made a function in a different php file so i re use the code in each page. My problem is that after logging in , no navigation bar is being loaded. any suggestions to what might be the problem ?
Code in function.php file
function checkAuth()
{
session_start();
if(empty($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
include('regnav.php');
}
else if ($_SESSION['role'] =="admin"){
include('adminnav.php');
}
}
code in the begging of each page
<?php
require_once("function.php");
checkAuth();
?>
Try passing the session result into the function:
place session call at the top of the rendering page
session_start();
$this_session = $_SESSION['role'];
require_once("function.php");
place where you want to render menu
checkAuth($this_session);
the function
function checkAuth($this_session)
{
if($this_session == ''){include('menu.php');}
elseif($this_session == 'registered'){include('regnav.php');}
elseif($this_session == 'admin'){include('adminnav.php');}
}
I would personally restructure your pages just a little, like this:
At the top of each page put:
session_start();
include('function.php');
checkAuth();
Change the function to:
function checkAuth()
{
if (!isset($_SESSION['role'])) { require_once('menu.php'); }
if ($_SESSION['role'] == 'registered') { include('regnav.php'); }
if ($_SESSION['role'] == 'admin') { include('adminnav.php'); }
}
This assumes you aren't using functions in your navbar files, since you don't call any functions to write the navbar. You could, of course, put both navbars into functions and put both functions into a single navbar.php file that you would include at the top of every page (or put the functions into function.php), then call the appropriate navbar function from the checkAuth() function, like this:
if ($_SESSION['role'] == 'registered') { navbar_registered(); }
if ($_SESSION['role'] == 'admin') { navbar_admin(); }

Home page on condition basis in magento

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

CakePHP wait for alert acceptance

I have a user registration page that launches a third party script for a game, i only want this page to finish loading if the user accepts the permission (it will set a HTTP variable). I currently have it prompting for permission but is loading the page in the background, how would i go about "waiting" or "reloading" the page to re-check if this variable is set yet?
Controller function for add
public function add() {
if ($this->User->IGBRequireTrust()) {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
}
Public function in Model that is called above
// Returns TRUE if the user trusts the site, FALSE otherwise.
public function IGBRequireTrust()
{
if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes')
{
echo "<script>CCPEVE.requestTrust('http://*.".$_SERVER['HTTP_HOST']."');</script>";
}
return true;
}
The page needs to redirect back to the index function with a session flash msg set if they do not accept the permission. I tried adding an else in where it calls the IGBRequireTrust function but it seems to always do both.
UPDATE:
AppController Function
public function beforeFilter() {
$this->Auth->allow('index', 'view');
if($this->name == 'Users' && $this->action == 'add'){
//die('correct controller and action');
if(isset($_SERVER['HTTP_EVE_TRUSTED'])){
if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes' && $this->action != 'promptEveTrusted'){
$this->redirect(array('controller'=>'Users', 'action'=>'promptEveTrusted'));
}
} else {
$this->Session->setFlash(__('Registration is only allowed through the EVE in game browser.'));
$this->redirect(array('controller'=>'Users', 'action'=>'index'));
}
//} else {
// die('wrong controller and action');
}
}
Looks like you are injecting javascript into the page, which will not get rendered until after all php code. Maybe try using javascript as your logic here. Or jquery would be super easy. Also, $this->User->IGBRequireTrust() is always returning true. put return false in your if statement
// Returns TRUE if the user trusts the site, FALSE otherwise.
public function IGBRequireTrust()
{
if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes')
{
echo "<script>CCPEVE.requestTrust('http://*.".$_SERVER['HTTP_HOST']."');</script>";
return false;
}
return true;
}
<script>
var eve_trusted = '<?php echo $_SERVER['HTTP_EVE_TRUSTED']; ?>';
var declined_eve_url = '<?php echo $this->webroot; ?>users/unauthorized';
//Might have to wait for your script injection to load so we will wait for document ready
$(function(){
if(!eve_trusted) window.location.href = declined_eve_url;
});
</script>
Here I would offer you help in canceling the page load with js but, I am not sure of the order that your main script will load.
Alternatively, you could clean all of this up by putting your logic in the beforeFilter of your AppController.ctp
function beforeFilter(){
if($_SERVER['HTTP_EVE_TRUSTED'] != 'yes' && $this->action != 'promptEveTrusted')
$this->redirect(array('controller'=>'User', 'action'=>'promptEveTrusted'));
}
//USERS CONTROLLER
function promptEveTrusted(){
//This will give the user a blank template, comment if you don't wish this
$this->layout = 'ajax';
}
//views / users / prompt_eve_trusted.ctp
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function(){
CCPEVE.requestTrust('http://*.".$_SERVER['HTTP_HOST']."');
});
</script>
I would load the page without the http variable and that would just show a message (by javascript, for example).
If the user accepts it, then, javascript will redirect him to the same page with a variable, which will then load everything you want when the variable is set.
If he doesn't accept it, it will just keep in the same page or redirect him to another one, as you prefer.

opencart to stop view index page until login

i am using opencart in one project,
every thing is working fine but i am unable to find an option to stop view of home page until LOGIN.
actually this is project requirement, no one can see home until LOGIN.
is there way to do this with OPEN CART ?
Thanks
this is untested, but should point you in the right direction:
(OpenCart 1.4.9.x)
Save this to:
catalog/controller/common/check_login.php
<?php
class ControllerCommonCheckLogin extends Controller {
public function index() {
if (!$this->customer->isLogged()) {
// Require to be logged in
$ignore = array(
'account', 'payment'
);
$match = false;
if (isset($this->request->get['route'])) {
foreach ($ignore as $i) {
if (strpos($this->request->get['route'], $i) !== false) {
$match = true;
break;
}
}
}
// Show site if logged in as admin
$this->load->library('user');
$this->registry->set('user', new User($this->registry));
if (!$this->user->isLogged() && !$match) {
return $this->forward('account/login');
}
}
}
}
?>
Edit /index.php
Find:
// Maintenance Mode
$controller->addPreAction(new Action('common/maintenance/check'));
Add After:
// Login Check
$controller->addPreAction(new Action('common/check_login'));
Essentially use the same logic as the maintenence check... The big difference is it looks for the word 'account' in the string... If it finds it it allows the page to be displayed, if not it redirects to the login page...
Use the word "account" instead of "login" in case they need to register... All of the account pages already check for loggin so there is no worry there...
Again, untested so you may need to tweak one or two things - but it should/may work by dropping in the code
check_login.php for opencart 1.5.3
<?php
class ControllerCommonCheckLogin extends Controller {
public function index() {
// Require to be logged in
if (!$this->customer->isLogged()) {
// Require to be logged in
$ignore = array(
'account','payment'
);
$match = false;
if (isset($this->request->get['route'])) {
foreach ($ignore as $i) {
if (strpos($this->request->get['route'], $i) !== false) {
$match = true;
break;
}
}
}
// Show site if logged in as admin
$this->load->library('user');
$this->user = new User($this->registry);
if (!$this->user->isLogged() && !$match) {
$this->redirect($this->url->link('account/login'));
}
}
}
}
?>
There is nothing built-in that I know of, but you can do it yourself. Based on your answers to #CarpeNoctumDC's questions you may have to do some digging, but this should get you started:
system/library/customer.php
public function isLogged() { ... }
catalog/controller/common/home.php
if (!$this->customer->isLogged()) {
// login page
exit;
}
The right way to go about this is to open
/catalog/controller/common/home.php
find public function index() { at the top of the code, and after it put
if(!$this->customer->isLogged()) {
$this->session->data['redirect'] = $this->url->link('common/home');
$this->url->redirect('account/login', '', 'SSL');
}
In case you're unsure how to do this properly, just take a look at the first few lines after public function index() { in
/catalog/controller/account/account.php
setting your code in the home controller to common/home in place of account/account

PHP function to check if 2 variables are empty

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

Categories