I have perhaps the most perplexing issue I've ever had with PHP. Please see the following code:
public $profile;
public $account;
function __construct(){
if(isset($_SESSION['uid'])){
$this->$profile = $_SESSION['user_profile'];
$this->$account = $_SESSION['user_account'];
echo "<script> alert('".$this->$profile->forename."'); </script>"; //Shows nothing
}else{
unset($_SESSION['user_profile']);
unset($_SESSION['user_account']);
}
}
For some reason, it seems that either $this->$profile will be written or $this->$account will be written, but only if it is the last one. In the situation above, if I moved the profile line to be after the account line, it would be written. However, in this case it is not.
Both $_SESSION variables are objects retrieved from an SQL statement, and their assignments are valid as accessing either of the variables directly (eg. $_SESSION['user_profile']->forename) works fine.
Any ideas? Thank you.
You are using variable variables, but your code looks like you are trying to access a member variable.
Replace this:
$this->$profile
$this->$account
With this
$this->profile
$this->account
Related
this might be a stupid question but I'm stuck here at this silly problem for the part 2 hours.
I have this function which checks if a particular config file's variable are not empty.
Here's the function :-
include 'inc.config.php';
function not_valid_settings()
{
if((empty($_GLOBAL['id'])) || (empty($_GLOBAL['username'])) || empty($_GLOBAL['password']))
{
return true;
}
else
{
return false;
}
}
Here's the config inc.config.php file:
$_GLOBAL=array();
$_GLOBAL['id'] = "asas";
$_GLOBAL['username'] = "asas";
$_GLOBAL['password'] = 'as';
Function Calling
include 'inc/inc.functions.php';
if(not_valid_settings())
{
echo "Please enter correct entries in inc/inc.config.php";
exit();
}
For some reason, I always get the Please enter correct details. Even if my $_GLOBAL['username']='';.
What wrong am I doing here?
The problem is that $_GLOBAL appears to be a PHP superglobal, but it is not -- there is no such superglobal in PHP. As a result, this variable is not immediately accessible everywhere. If you add global $_GLOBAL; as the first line if your function, your code should work:
function not_valid_settings()
{
global $_GLOBAL;
Perhaps you meant to use $GLOBALS instead, though I would strongly advise against it. Use a name like $SETTINGS instead, and don't forget to use global $SETTINGS; in functions where you need to access the settings object.
In general, you should avoid choosing variable names that start with $_ unless they are PHP superglobals; this prefix implies a superglobal, while your variable is not. This will create unnecessary confusion.
$GLOBALS is what you mean to use, though this is not a good practice.
http://php.net/manual/en/reserved.variables.globals.php
I downloaded some files from github (from this page, if matters ).
This is for php login stuff, and everything works, but I cannot understand how it is possible.
Here is the typical top part of my pages
require('inc/config.php'); // db credentials and connect
require('inc/password.php'); // class password, hashing etc
require('inc/user.php'); // class user
one of the first function in user.php is the following:
...
private function get_user_hash($username){
$_SESSION["uname"] = $username; // echo of this variable works
...
So, the question is - where the value of $username is coming from?
Because there is no such a variable in the preceding files (config.php and password.php).
This is its first occurrence in entire script workflow and how it is possible that it has some value?
find from where get_user_hash() is called and their will be some argument in it. It is not necessary that it should be named $username.
for ex-
$user="some user name";
get_user_hash($user);
$username is an argument of the get_user_hash function, which isn't executed until some code elsewhere in your script. Although it appears first, it isn't run first, and the code that calls this function will pass it a value for $username.
I'm creating a website and somewhere in the code I need to query for a user attribute (ex:account state) and in the same row I have the reason, case is account state is "suspended".
I'm trying to minimize the requests to the database, so I created a function to verify account state.
function getAccountState($userid,$reason){}
What I am trying to do is if account state is "suspended" I would change the $reason to "the database reason".
I've already done that but if I change the $reason inside the function, outside the function it will not change.
I searched for "php pointers" on google but I think there is not such thing.
Is there a way to do this? Other way I'll just make another database request...
You could of course pass the variable by reference but as you don't seem to need it, I would just return it from the function:
function getAccountState($userid){
// your code
return $reason;
}
and call it like:
$reason = getAccountState($userid);
If you want to stay your code as it is now, you could pass the variable by reference:
function getAccountState($userid,&$reason){}
^ like so
You could consider passing it in by reference. Or perhaps just changing the function to return the correct information.
In the definition of functions you can tell that $reason argument is passed by reference, not value. To do so, use & in front of variable:
function getAccountState($userid,& $reason){}
You are looking for references, in PHP terminology, not pointers.
They work this way :
function getAccountState($userid, &$reason){ // Notice the &
$reason = "database locked"; // Use it as a regular variable
}
getAccountState(12345, $reason); // Here, it is written as a regular variable, but it is a ref.
echo $reason; // echoes "database locked"
I have a page named ChangeApprovalInfo.php - It has a function called Row_Rendered as follows;
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$RecordOwner = $this->RequestUser->CurrentValue;
echo $RecordOwner;
}
Echoing $RecordOwner gets me the data I will need for a sql query on another page....
I have another page called ChangeApprovalEdit.php - This page has
<?php include_once "ChangeApprovalinfo.php" ?>
at the top of the file.
ChangeApprovalEdit.php has a function where I need the $RecordOwner variable as defined in ChangedApprovalInfo.php
If I add "echo $RecordOwner" on the ChangeApprovalEdit.php page, I get an error saying it's an unknown variable. My understanding is that I need to "make it global" or some such business. I know very little about PHP and the pages I am editing are long and complex. (to me, at least)
What do I need to do? I know that the information I have provided might not be enough to answer the question. I don't know enough to even know exactly what I need to ask. If more information is needed, I will edit and follow up.
pastebin of the files
ChangeApprovalInfo.php = http://pastebin.com/bSRM1wwN
ChangeApprovalEdit.php = http://pastebin.com/AStG9pqb
EDIT:
Changing Row_Rendered to this seems to be more effective. I'm having trouble seeing WHERE I can later echo this variable... but I'm getting somewhere with this...
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
}
Don't echo variables from functions, which just outputs them to the standard output. return them from the function so you can use the value elsewhere as well.
function Row_Rendered() {
$RecordOwner = $this->RequestUser->CurrentValue;
return $RecordOwner;
}
Then instead of
$obj->Row_Rendered();
use
echo $obj->Row_Rendered();
and if you want to use the value elsewhere, use
$value = $obj->Row_Rendered();
You can do a couple of things:
First, you can return $RecordOwner from the function, and store its value in a variable. This method is usually preferred.
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$RecordOwner = $this->RequestUser->CurrentValue;
echo $RecordOwner;
return $RecordOwner;
}
// Store it in a variable when calling the function.
$RecordOwner = Row_Rendered();
Or, you can make it global inside the function:
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
echo $GLOBALS['RecordOwner'];
}
You can use the $GLOBALS superglobals array, like this:
function Row_Rendered() {
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
}
However, you should not do that. Instead, refactor your application so that the view in ChangeApprovalinfo.php just contains a function, which is then called with the appropriate parameters.
EDIT: Chaning Row_Rendered to this seems to be more effective. I'm having trouble seeing WHERE I can later echo this variable... but I'm getting somewhere with this...
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
}
I feel compelled to write another answer to this update. Let me demonstrate the use of globals as seen from outside that function:
$obj->Row_Rendered();
$obj->foobar();
echo $GLOBALS['RecordOwner'];
Quick, what will be echoed and where does that value come from? Well, it depends on what $obj-foobar() does. Maybe it changes the global variable. Maybe it doesn't. Who knows if the variable has been set at all? How would you trace back what happened exactly without adding a debug line after every single function call?
And that's just three lines of code. Imagine that in an application of any complexity.
Now, the same thing if I return the value from Row_Rendered:
$owner = $obj->Row_Rendered();
$obj->foobar();
echo $owner;
If the Row_Rendered method is behaving as it should (returning the owner), this code is very predictable. If you do not follow this pattern, you'll have a hell of a time getting anything done when the application grows to any halfway complex size.
Set the variable as global from within the function
$my_global_var = "old value";
function doing_stuff(){
global $my_global_var; //this will use the global variable instead of creating a local one
$my_global_var = "new value";
}
echo $my_global_var;//returns "new value"
I have the following setup:
class.staff.php
This defines many variables, the one I'm working with right now is $thisuser->getStaffLang();
class.language.php
(Only a function, not a class) This file runs a sql query based on the one variable I pass it from header.inc.php as well as it should pull the staff members unique language ID.
The function is:
function translate($TRANSLATION){
$sql="SELECT $TRANSLATION FROM ".LANGUAGE_TABLE." WHERE LANGUAGE_ID=".$thisuser->getStaffLang;
$query = mysql_query($sql);
$translation = mysql_result($query,0);
print $translation;
}
header.inc.php
First file I'm working with using this function
example translation entry is:
translate('TEXT_WELCOME_BACK_STAFF');
My problem is that when I'm outside the function $thisuser->getStaffLang; is populated but inside the function it is empty. I really don't want to have to pass the same variable to the function over and over as some files can have upwards of 20 translations in them and that seems like alot of redundant coding. Can someone tell me how in the heck I can get that variable to be recognized by the function without have to pass it to it every single time when calling the function? Hope this wasn't clear as mud. :\
Note: Both class.language.php (where the function is and doesn't work) and header.inc.php (where the variable alone works) have required class.staff.php. So they both should be able to utilize that code/variable.
add global $thisuser; at the beggining of translate()
You need to mark $thisuser as global. See below:
function translate($TRANSLATION)
{
global $thisuser; //<---- MUST MARK global
$sql="SELECT $TRANSLATION FROM ".LANGUAGE_TABLE." WHERE LANGUAGE_ID=".$thisuser->getStaffLang;
$query = mysql_query($sql);
$translation = mysql_result($query,0);
print $translation;
}
$thisuser->getStaffLang is probably not a global. You should either make it global, by adding global $thisuser to the first line of the function,
or better, pass this variable also in the function scope. So something like this:
function translate($TRANSLATION, $stafflang){
// function here
}
translate('TEXT_WELCOME_BACK_STAFF',$thisuser->getStaffLang);