i have this piece of code, saved inside a file called 'functions.php'
$host = $_SERVER['HTTP_HOST'];
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
function redirect_to_home(){ /*function to redirect the user ot his profile page, or admin page if he is an administrator*/
if(admin_class::isadmin()){
header("Location:http://$host$folder//adminindex.php");
exit();
}
else{
header("Location:http://$host$folder/profile.php");
exit();
}
}
function redirect_to_welcome(){
header("Location:http://$host$folder/index.php");
exit;
}
function redirect_to_loan(){
header("Location:http://$host$folder/loan.php");
exit;
}
When browsing through the website itself, these don't work correctly and I could only navigate via links within the website, I've finished the whole thing though (I just used header("Location:http://localhost/YMMLS//pagename.php") when I was developing).
And I need some enlightenment here, I'm launching this website via LAN and those connected could access it via xxx.xxx.xxx.x/YMMLS. But of course, the website fails to redirect properly when any of these functions are called.
Thanks in advance,
NetBeans is warning you, because the variables are declared outside the function.
They don't exist within the scope of the function. Sure, you can call the function -- from outside the function, where those variables do exist -- but that's different. :)
Here -- try this;
class redirect {
public $host = '';
public $folder = '';
public function __construct() {
$this->host = $_SERVER['HTTP_HOST'];
$this->folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
}
public function home() { /*function to redirect the user ot his profile page, or admin page if he is an administrator*/
if(admin_class::isadmin()){
header("Location: http://$host$folder/adminindex.php");
} else {
header("Location: http://$host$folder/profile.php");
}
exit();
}
public function welcome() {
header("Location: http://$host$folder/index.php");
exit;
}
public function loan() {
header("Location: http://$host$folder/loan.php");
exit;
}
}
Instead of calling simply redirect_to_home(); this would be invoked by redirect::home();.
Hope that helps.
try this
host = $_SERVER['HTTP_HOST'];
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
function redirect_to_home(){
global $host, $folder;
if(admin_class::isadmin()){
header("Location:http://$host$folder//adminindex.php");
exit();
}
else{
header("Location:http://$host$folder/profile.php");
exit();
}
}
If this works then change this in all other functions
GOOD LUCK!!
DOCUMENTATION http://php.net/manual/en/language.variables.scope.php
Related
Okay, so this is what I do:
I go to www.mywebsite.com/orders?id=1
It redirects be to login before proceeding.
I log in successfully but it redirects to www.mywebsite.com/orders.
If I am already logged in and go directly using GET method, it works fine. But if I am asked to login, the GET method disappears.
How do I preserve ?id=1?
Before redirecting the user back to the login page store the current page (the requested page) in a session variable. Assuming you have a function called check_login this would more or less look like what you should do:
public function check_login() {
if (!$this->session->has_userdata('logged_in') || $this->session->logged_in !== true) {
if (!empty($_SERVER['QUERY_STRING'])) {
$uri = uri_string() . '?' . $_SERVER['QUERY_STRING'];
} else {
$uri = uri_string();
}
$this->session->set_userdata('redirect', $uri);
redirect('/auth/login');
}
}
Then when the user successfully logs in your login function should somewhere have the following logic:
public function login() {
// form validation
// get post vars
// check username/pwd against db
if ($login) {
if ($this->session->has_userdata('redirect')) {
redirect($this->session->redirect);
} else {
redirect('/dashboard');
}
} else {
// error logging in
}
}
session variable could store the id.While log in using session pass the id value.You can retrive the value anywhere in session.
$this->load->library('session');
$this->session->set_userdata('userId', 'YourId');
where userId would be the name of the session variable, and YourId would be the value.
Simply Use this
redirect($_SERVER['HTTP_REFERER']);
My redirect and server check not working for my index.php in codeigniter.
I have a front end install wizard for codeigniter with 4 steps the 4th step being the last. I am trying to make it so if user has gone through the steps and has reached step 4 that will have access to website on there domain/server.
Currently the code I use keeps on redirecting to the index.php of my install URL, even though I have completed all steps.
This code below is on my main index.php
if (!file_exists('install/index.php')) {
// Make sure we've not already tried this
if (strpos($_SERVER['REQUEST_URI'], '/install/')) {
header('Status: 404');
exit;
}
// Otherwise go to installer
header('Location: '.rtrim($_SERVER['REQUEST_URI'], '/').'/install/');
exit;
}
Welcome Controller
public function index()
{
if (!file_exists('install/index.php')) {
// Make sure we've not already tried this
if (strpos($_SERVER['REQUEST_URI'], '/install/')) {
header('Status: 404');
exit;
}
// Otherwise go to installer
header('Location: '.rtrim($_SERVER['REQUEST_URI'], '/').'/install/');
exit;
} else {
$this->load->view('welcome_message');
}
}
You can place this code in the welcome controller
Place your code inside the index function of the controller
class Welcome extends CI_Controller {
public function index()
{
/* your redirect code*/
}
public function comments()
{
echo 'Look at this!';
}
}
I've looked at existing answers for my problem.
I've echo'd the value right through the process and right up until the "header('Location" instruction the values remain intact.
I don't think it's a serialization problem as suggested for similar problems...
Here are the relevant bits of the class:
class clsSetUser {
protected $UserID = 0;
public function initUser($id) {
// get user details from database
$this->setUserID($id);
// etc...
}
private function setUserID($value) { $this->UserID = $value; }
public function getUserID() { return $this->UserID; }
}
common.php:
if(unset($clsUser)) $clsUser = new clsSetUser;
login-exec.php:
$clsUser->initUser($id);
header("Location: somewhere.php");
somewhere.php:
echo $clsUser->getUserID();
// here it equals 0
any ideas? does "header" serialize everything?
This is because PHP is actually starting from a clean slate in somewhere.php.
header("Location: somewhere.php"); sends a command the browser to connect to a different page. In this page non of variables of the previous page are available in PHP.
You need to set the userId in the $_SESSION so that you can reload the user from the database when he visits somewhere.php.
login-exec.php
$clsUser->initUser($id);
$_SESSION['user_id'] = $id;
header("Location: somewhere.php");
somewhere.php
$clsUser->initUser($_SESSION['user_id']);
I made a login script, when I want to check when the user is logged in, I use the function logged_in(), which consists of:
function logged_in()
{
if(isset($_SESSION['id']))
{
return true;
}
else
{
return false;
}
}
the session is set here:
else if ($login === true)
{
echo 'Login success.';
include 'include/aside.php';
include 'include/footer.php';
$userid = id_from_username($username);
$usernameforsession = username_from_id($userid);
$_SESSION['id'] = $userid;
$_SESSION['username'] = $usernameforsession;
header( "refresh:2;url=index.php");
exit();
}
And this is an example of me using it in 'index.php':
if(logged_in() === true)
{
echo 'Site content when user is logged in.';
} else if(logged_in() === false)
{
include 'include/widgets/register.php'; //registration form
}
And yes, the function is included in every page.
I made this function so it should work...
Why isn't it working?
Replace this code:
if(logged_in() === true)
With this code:
if(isset($_SESSION['id']))
That cuts out all the middlemen.
Although you have listed quite a bit of code, are you sure you are including session_start(); at the top of each page? You need to call this before you do anything at all with the session variables.
The second thing is that the error message is showing that the function isn't defined - are you sure you have it either in the source for the page or as an include to the code that defines it on each page?
If you have it in a file called 'functs.php', you need to include() it in every page that will make a call to that function.
If you are absolutely sure that the declaration is being included on every page, then I would suggest that you check to make sure the function is not declared as a method inside an object.
After validating user input and storing login credentials I start a session by calling session::start and then reload the index.php file by calling general::reload. The I use session::is_start() to determine which page to load from the index file.
I don't think this is working correctly as I always get the same page loaded - b1e.htm.
My concern is that my static class session does not maintain its value between the AJAX/PHP call and the reload index.php call.
Similar posting - here
index.php
include 'b2.php';
if(session::is_start())
{
include 'b2e.htm'; // user is logged in
}
else
{
include 'b1e.htm'; // user is not logged it
}
Snippet - session:start() and session::is_start();
class session
{
protected static $ses_id ="";
public static function start()
{
self::$ses_id = session_start();
}
public static function is_start()
{
return self::$ses_id;
}
public static function finish()
{
self::$ses_id = 0;
$_SESSION=array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
{
setcookie(session_name(), '', time()-2592000, '/');
}
session_destroy();
}
}
Snippet - general::reload()
class general
{
public static function reload()
{
$uri = 'http://';
$uri .= $_SERVER['HTTP_HOST'];
header('Location: '.$uri.'/host_name');
}
You can encapsulate and consolidate session functionality, but you can not fully monitor sessions with a class as php user code is stateless (even when using static keyword)...i.e. it will depend upon SESSION to retain state.
You need to call your session_start(); to actually start the session on each page.