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.
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?
As i have two files i have declared variable $msg["CE"] in config.php file and i am trying to access that variable in function of login.php page ...
config.php
<?php
$msg["CE"] = "connection_error";
?>
login.php
<?php
include_once "config.php";
function myf()
{
return $msg["CE"];
}
echo myf();
?>
Please help me how to access that msg["CE"]
The variable is available in the file login.php. However, because you attempt to access the variable from within a function (of which you don't supply the variable as a parameter), you need to use the global keyword.
function myf() {
global $msg;
return $msg["CE"];
}
You should take a look at the documentation for variable scope for PHP.
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
I've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.
I am using a jquery function to call a getdata.php file like this -
$("#tasks").load("getdata.php?choice=" + $("#projects").val());
The getdata.php echoes back results dynamically to the callee.
<?php //getdata.php
global $user_id; //<--These variables are defined in the callee file
global $con;
$choice=$_GET['choice'];
$query="SELECT task_id,title from tasks where user_id=$user_id AND proj_id=$choice";
$result=mysqli_query($con,$query) or die(mysqli_error($con));
if((mysqli_num_rows($res))!=0)
{
while (list($task_id, $title) = mysqli_fetch_row($result))
{
echo "<option value=$task_id>$title</option>";
}
}
else
{
echo "<option>No current Tasks</option>";
} ?>
Is there any way by which I can access the variables in the callee file from here. I tried using global as would normally be the case with included files but that's not working here.
You may consider using $_SESSION variables to set variables that are available to both your callee file and getdata.php.
Why not put them in the request ? `Something like
$("#tasks").load("getdata.php?choice=" + $("#projects").val() + "&user_id=" + user_id;
(How to pass PHP variables to js as already got a lot of answers on SO)
then in getdata.php
$user_id = $_GET['user_id'];
for the DB connection, you can create a kind of config file that you include on every script
config.php
function getDBConnection() {
blah...blah ...
return $con;
}
in getdata.php as in every script that need it :
require_once yourpath/config.php;
$con = getDBConnection();
For security reasons, if your config file contains passwords, login , etc ... put it outside
your web directory.