I want to know what is session_reset() for and when should we use it ?
when I use it I get an error like this: "Call to undefined index session_reset()".
I hope you know something about it .
Thank you in advance.
session_reset — Re-initialize session array with original values.
Example
first create a session variable
<?php
session_start();
$_SESSION["A"] = "Some Value";
echo $_SESSION["A"];
//Output: Some Value
//if you need to rollback the session values after seting new value to session variables use session_reset()
$_SESSION["A"] = "Some New Value"; // set new value
session_reset(); // old session value restored
echo $_SESSION["A"];
//Output: Some Value
?>
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 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);
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.
I'm a little confused - how do I get a variable name stored to a database?!
Record in the database is a string: "$test"
The variable $user is set before the records a fetched from database. So I want to "convert" this string to a real variable to get the value of it.
The following didn't work:
// $test is set to 'bla'
$test = 'bla';
// $var is the value from the database
$var = '$test';
// print $test
echo ${$var};
I know that it would work if I remove the '$' from the database record
$var = 'test';
echo $$var;
But how to handle this without?
String replace in better option, if you don't want to replace $ you can use eval function:
$test = 'bla';
$var = '$test';
eval("\$var = \"$var\";");
echo $var; //output: bla
I was facing a similar problem just now.
Here is what I did:
$res[0] is obtained from mysql_fetch_array() and it contains another query which has $variable embedded.
$qry="select query from sql_list where id=".$sql_id;
$result=mysql_query($qry);
$res=mysql_fetch_array($result);
eval("\$qry_2 = \"$res[0]\";");
mysql_query($qry_2)
It works! Maybe someone can suggest a better way.
This is 3 years later but since no one give you your answer, maybe this will help you or the next person searching for this.
From what I understand, you already declared a variable and stored the name of that var in the db.
$var = '$test'; // from db
$var[0] = ''; // remove first letter, simple & much faster than ( trim or substr )
//content of var
$var_content = ${$var};
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';