I am using jquery throughout a php project, so all pages load dynamically into the main page, I am trying to make links only visible to a certain user, so if they goto the main page and append the URL, i.e ?mode=55rt67 this gets stored as a variable and can be checked throughout the app. I am using the below, but it doesnt work. any suggestions?
if (empty($_GET)) {
$mode = "user";
}else{
define ('$mode', '($_GET['mode']);
}
define is used to declare constants, you want to use a variable, not a constant.
UPDATED (use session to store mode variable):
if (empty($_GET)) {
$_SESSION['mode'] = "user";
}else{
$_SESSION['mode'] = $_GET['mode'];
}
And don't forget to use session_start on every page
Change your code with below code.
// Try this
if(isset($_GET['mode']) ){
define ('mode',$_GET['mode']);
}else{
define ('mode',"user");
}
echo mode;
//OR
if(isset($_GET['mode']) ){
global $mode = $_GET['mode'];
}else{
global $mode = "user";
}
echo $mode;
if (empty($_GET)) {
$_SESSION['mode'] = "user";
}else{
$_SESSION['mode'] = $_GET['mode'];
}
The above solution worked perfectly, added session_start to the index page, and it now carries through the rest of the pages.
Awesome, thanks all
Related
I've got this session variable, which was initiated in index.php
<?php
$sid = session_id();
if (empty($sid) {
session_start();
}
//value of the variable can be changed and saved before coming back to the index
if(!isset($_SESSION['context'])){
$_SESSION['context'] = 1;
}
//...
?>
Later a script is used to create or update a database entry.
entryupdate.php:
<?php
include_once("template.php");
/*....*/
$sid = session_id();
if (empty($sid)) {
session_start();
}
/*....*/
//Create a new entry, context is not given in the $input_object at this point.
$template = new Template($input_object);
$context = $template->getContext();
?>
template.php is a script where only the Template Object is defined, so I don't call $session_start in it, my rationale being that it would always be called by the scripts including it, as it is in the case I'm describing here. Here is the relevant code:
<?php
class Template
{
private $m_context;
//other private parameters
function __construct($arguments_object){
if(!is_null($arguments_object)){
/*....*/
$this->m_context = $arguments_object->context;
/*....*/
}
}
/*....*/
function getContext(){
if(!empty($this->m_context)){
return $this->m_context;
}else{
//this would be our case, as the parameter was not initialized yet.
return $_SESSION['context'];
}
}
/*....*/
}
?>
Now, a colleague stumbled on an error and upon investigation, I found out that the $context was set to NULL. When I tried to reproduce the issue, the context was correctly initialized. And to be honest, for the few years the tool has been used, it is the first time this kind of issue was encountered.
I've also checked, at no point I am unsetting this particular session variable.
Am I making a false assumption there, in thinking that template.php would find the session variable when the session was started in entryupdate.php where it was included and used?
Lets say that my domain is www.domain.com. I want to add GET variable to the every url which starts with domain.com. For example if user enters domain.com in browser, default get variable will be added so url will look like: domain.com?variable=default or if user enters domain.com/contact.php it will look like domain.com/contact.php?variable=default.
Thanks
You can include a say header.php file in all pages:
if ( !isset($_GET['variable']) ) {
header('Location: ' . $_SERVER['REQUEST_URI'] . '?variable=default');
}
Why not use it like this?
if (isset($_GET['var'])) {
$var = $_GET['var'];
} else {
$var = "default";
}
So if the var exist in URL, use that one. Else, use the default variable.
Or you could use a session to retrieve a variable that cannot be changed by the user.
// On first page
if (!isset($_SESSION)) { session_start(); }
$_SESSION['var'] = "var";
///////////////////////////////////////////////////////
// On second page
if (!isset($_SESSION)) { session_start(); }
if (isset($_SESSION['var'])) {
$var = $_SESSION['var'];
} else {
$var = "default";
}
I am trying to make a login validation page for my class and this is the code I have for the page LoginDataModel.php.
<?php
//define a constant variable for fxUsers.ini
define('FX_LOGIN_INI_FILE', 'fxUsers.ini');
class LoginDataModel {
private $ini_array;
//construct class will read and store an associative array
public function __construct() {
$this->ini_array = parse_ini_file(FX_LOGIN_INI_FILE);
}
//validateUser function will compare the username and password
//given by the user to the values stured in the ini file.
public function validateUser($username, $password){
if(in_array($username,$this->ini_array) && in_array($password,$this->ini_array)){
return TRUE;
} else {
return FALSE;
}
}
}
?>
This code will be called in my login.php page once the user passes through his credentials. If the users credentials do not match, he will simply be rerouted back to the login page to try again. The code for the login page is
<?PHP
//check for key to see if this is the first time loading the page
if (empty($_POST['txtUser'])){
$user = '';
$pass = '';
} else {
$user = $_POST['txtUser'];
$pass = $_POST['txtPassword'];
}
//call method from a different file
require_once ('LoginDataModel.php');
$LoginDataModel = new LoginDataModel();
$control = $LoginDataModel->validateUser($user, $pass);
//if user and password match, continue to next file and exit current file
if($control === TRUE){
include 'fxCalc.php';
exit();
}
?>
While I believe to have everything set, The only thing I need is how to compare the values between the user and the values in the ini file. Any help would be appreciated
EDIT
I should have mentione that my ini file will just be
[section]
admin = pass
EDIT 2
My code reflect the changes I've made thanks to the support from this post as well as looking back at my text book. My problem is now that When I pass the user and pass through the file, it returns as false even though the strings match perfectly.
You are doing the wrong way of comparison in the below line..
if($ini_array == $username && $ini_Array == $password){
The parse_ini_file() returns an array , so you just can't check a variable $username inside an array (i.e. $ini_array) using a == operator. You should be using array_search or in_array() functions as such.
Something like...
if(in_array($username,$ini_array) && in_array($password,$ini_Array)){
I've searched for a day to find a solution for this,But no luck yet,
I have a function called : online() this function is inside a file called client.php which is located in root/dir/includes/client.php:
In client.php:
// db connection
include "base.php";
// check if availabe
$available = "false";
$check = mysql_query("SELECT available FROM users ");
while ($row=mysql_fetch_array($check)) {
if($row['available'] == "yes") {
$available = "true";
}
}
// get config
$fetch = mysql_query("SELECT * FROM config ");
$config = mysql_fetch_array($fetch);
// functions
function online() {
// globals
global $available,$config,$path;
// build box
if($available == "true") {
?>
<div id="online">
<?
} else {
?><div id="offline"> <?
}
echo 'client.php is included';
}
}
?>
About client.php:
first it establishes a database connection, then checks if a user is available, if yes : $available = "true";
else:
$available = "false";
Then I include client.php in index.php (located in root), So i Have:
in index.php:
$path = "dir/";
include $path . "includes/client.php";
So far so good, everything works,
The problem is...
I need to use this function in other pages in sub directories too, to be more specific, I'm trying to add this function to my wordpress website which is located in: root/wp
in my wordpress header I include client.php :
include "../dir/includes/client.php";
And I get the output , So i'm sure it is included, But, none of my global variables work when I open my wordpress (root/wp) resulting in $available = null while it is expected to be "false" as it is defined in client.php
The confusing thing is that, when I echo $available inside my wordpress header I can get the value, but when I echo it inside client.php it is null again, so if I echo both in wordpress header and client.php , when I open my wordpress page, I can see the one I included in header, and the other one inside client.php is null.
Any help would be much appreciated.
It may be an issue with a variable scope. Try to use
$GLOBALS['available']
instead of
$available
in your client.php. So it will be defined in global scope.
At the definition section also:
$GLOBALS['available'] = "false"; and $GLOBALS['available'] = "true";
Your problem is probably the link reference in your include file. You could try:
include "/rootfolder/dir/includes/client.php"; instead of include "../dir/includes/client.php"; Also had a problem like this but this fixed it.
I have problems with php multilanguage. I'm using function *check_lang* and it works fine in one page, but once I go to another page the $_SESSION['lang'] variable $lang turns back into default (en). What is the problem?
<?php
function check_lang() {
if(isset($_GET['lang'])
{
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang
}
if (!isset($_SESSION['lang'])) {
$lang = 'en';
} else {
$_SESSION['lang']=$lang;
}
//directory name
$dir = 'languages';
return "$dir/$lang.lng";
}
?>
You have to:
session_start();
At the top of each of your scripts in which you want to use session variables.
You need to call session_start() on each page you're planning on using the $_SESSION[] global in. That's what tells PHP that it should look up the session_id from the user's cookies or the query string so that PHP knows which session's values to use.
Reference.