I have a session which I need to assign to a variable to be used on Stored Procedure. But cant seem to get the variable being affected on it
I have already tried the following, but nothing
1)
ob_start();
echo $_SESSION['User'];
$userV = ob_get_contents();
ob_end_clean();
2)
$userV = $_SESSION['User'];
3)
$userV= echo $_SESSION['User']; //fails as echo, used print instead
4)
$userV= print $_SESSION['User'];
But when I use a know value like below it works
$userV= 41;
The intended Procedure as follows:
$sql="CALL sp_getUser('$userV')";
The second one is the correct one: $userV = $_SESSION['User'];.
If $userV does not hold the expected value after this assignment, two things can be the reason: You haven't initialized the session (session_start()) or the you haven't set the $_SESSION["User"] (somewhere in another script you should have something like $_SESSION["User"]=41;.
You can test what is in the $_SESSION-variable presently by print_r($_SESSION);
Related
Is it possible to achieve something like this?
$_SESSION["new"] = "This are the values of $_SESSION['A'] & $_SESSION['B']";
I want to echo the "new" session value on a different page for my user to check.
I tried various things such as the code below but it doesn't work.
$_SESSION["new"] = <?php echo $_SESSION['A']?>;
Yes, you can assign multiple session variables by using the concatenation operator.
$_SESSION["new"] = "This are the values of ".$_SESSION['A']."&".$_SESSION['B'];
On the page, you want to display, simply print the session variable.
echo $_SESSION["new"];
Please make sure,on both pages, you are using session_start() function
$_SESSION["new"] = 'foo bar';
You can simply assign any value to your session variables. you can read more here
if you are sure of the type of value in $_SESSION["A"] and $_SESSION["B"], and assuming in your case they are both string, you can do the following:
$_SESSION["new"] = sprintf('This are the values of %s & %s', $_SESSION['A'], $_SESSION['B']);
I have a function with a reference parameter. The function populates the reference with an array. When it returns I test the reference value with a print_r(). It's successful. But when I try and assign it to another var as in,
$_SESSION['allvats'] = $ref_all_vats;
I get an empty session var. How do I assign this? I've tried declaring the reference as an array before invoking the function and also assigning to sessions as
$_SESSION['allvats'][] = $ref_all_vats;
Thank you.
Edit.
Session is running. Other SESSION vars populate. Here is the code bit:
if(buildMetalArrayNsp($process_id, $ref_all_vats)) {
writeDataToFile("vats " . print_r($ref_all_vats, true), __FILE__, __LINE__);
$_SESSION['allvats'] = $ref_all_vats;
}
Here is the session code:
$lifetime= 60 * 60 * 24 * 30; // 30 days
session_start();
setcookie(session_name(),session_id(),time()+$lifetime);
Try like this
session_start();
$_SESSION['allvats'] = $ref_all_vats;
list($firstVar, $secondVar) = explode(' ', 'Foo Bar');
list() is what you are after.
Your code is fine, there is something else going wrong. Are you sure the if statement is evaluating to true? Whenever you are tracking down errors it is good to use echo or print_r quite a bit. For example:
echo "<pre>Above if\n";
if(buildMetalArrayNsp($process_id, $ref_all_vats)) {
echo "In if\n";
$_SESSION['hello'] = "world";
$_SESSION['allvats'] = $ref_all_vats;
}
echo "Below if\n";
echo "\n-----------------------\n";
echo $_SESSION['hello'];
echo "\n-----------------------\n";
print_r($_SESSION['allvats']);
echo "\n-----------------------\n";
print_r($ref_all_vats);
echo "</pre>";
Of course if you are not displaying to a web page remove the <pre> tags.
$var = require_once("target.php");
is that possible to store require_once in variable and execute it late?
I need to place this into function
function foo($path){
if($path !== ""){$path = require_once($path);}
do something first...
$path//than execute require_once
}
The answer is no. When you assign require_once(); to a variable, the variable turns into a boolean with 1 in case the file was included successfully, or 0 otherwise (useless in require_once() as it returns a fatal error if it fails.
So, doing:
<?php
$hello = require_once("./hello.php");
echo $hello; // Prints 1.
?>
Anyway, if you create a php file that returns something, as for example:
FILE: require.php
<?php
$hello = "HELLO";
return $hello;
?>
In this case, the previous example would be different:
<?php
$hello = require_once("./require.php");
echo $hello; // Prints HELLO.
?>
So, you cannot store the function itself to execute it later, but you can store returned values from the required or included files. Anyway, if you explain better what are you using it for, I maybe able to help you better.
I have a session for login data, and I am trying to take it and put it into a variable to shorten whenever I want to echo or use the ID, Username, or Email. What is happening is that now I have it in a variable, but when I echo it, it shows it in an array.
Here is my variable data.
$User = htmlentities(ucwords($_SESSION['user']['username']));
$Email = htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8');
If I echo any of these, it returns "Array".
EDIT
I fixed my solution by changing how I get my $_SESSION data, and using unset to get rid of the array. Now I can just do this,
$User = $_SESSION['user']["username"];
$Email = $_SESSION['user']["email"];
Thank you for reminding me about how I'm even getting my session data in the first place =]
If you are using classes, where you specify all your variables, maybe try this:
$User = $_SESSION['user']->username;
echo $User;
where user is your class and username is your variable
WHen you add it to your session, you need to do soething like this:
$_SESSION['user'] = new user();
$_SESSION['user']->username= 'whatever value it is';
How can I use the global keyword so that when I press the submit button I can set the global keyword so that the top part of the script works?
located on top of the script.
if(!isset($u)){
echo 'the $u has no value';
} else if(isset($u)){
echo 'the $u has a value of yes';
}
located on bottom of the script.
if (isset($_POST['submit'])){
global $u;
$u = 'yes';
}
global is related to scope, not to order of execution
If both pieces of code are global, i.e. are not contained into functions, the 'global' keyword has no effect, because they are in the same scope
As another answer has correctly pointed out, your problem is an order of execution problem, not a scope problem
That's not what global means. Global means that the variable can be accessed inside functions and the like. You probably want to use Sessions. This involves calling
sesssion_start();
somewhere (usually the top of your script).
Variables can then be stored and retrieved by doing
$_SESSION['name'] = $foo;//Store a variable into the session
$bar = $_SESSION['bar'];//Retrieve a variable from the session
In your case you would store u variable into the session and retrieve it after the submit.
Is there some reason you aren't just passing this value via the form?
You have to run the
if (isset($_POST['submit'])){
global $u;
$u = 'yes';
}
before the
if(!isset($u)){
echo 'the $u has no value';
} else if(isset($u)){
echo 'the $u has a value of yes';
}
PHP reads the code line by line, so isset($u) always return FALSE until the line $u = 'yes'; is run.