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';
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);
hi i know how to store post value into session but how can i store session value into post.
post into session
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['mno'] = $_POST['mno'];
$_SESSION['age'] = $_POST['age'];
I have an array stored in session and i want to store it into post.
Can i do this? If yes then how?
I want to store all array value in post from session
The opposite should work without any problems.
$_POST['name'] = $_SESSION['name'];
$_POST['email'] = $_SESSION['email'];
$_POST['mno'] = $_SESSION['mno'];
$_POST['age'] = $_SESSION['age'];
If you want to hold an array you can do it like this:
$datapost = array ( 'name' => $_SESSION['name'], 'email' => $_SESSION['email']);
$_POST['info'] = $datapost;
$_POST is not a persistent store where you can put things. The point of $_POST is that it is populated at the start of the request, with the data that was passed to the server from the client (usually, web browser).
You can write into that array, but it won't have any special effect. It's just an array variable that's globally available in all scopes of your code. Normally, you'd just want to create a new array, and assign whatever you want in there:
$data = [];
$data['stored_foo'] = $_SESSION['foo'];
$data['submitted_foo'] = $_POST['foo'];
See also Why are Global Variables Evil?
If you want to send data back to the browser, you can:
put it in the output (using echo etc)
put it in an HTTP header (using the header() function)
put it in a cookie (which will be sent as an HTTP header, crafted for you by the setcookie() function)
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
?>
When I check my cookies I do see that PHPSESSID is being made. But when I try to use them I get an error message saying that the variable is undefined.
This is how I set my Sessions:
$posts = array("auto_year", "auto_brand", "auto_model", "auto_bodywork", "auto_doors",
"auto_fuel", "auto_gearbox", "auto_type", "auto_uitvoering", "auto_part", "auto_description", "email_address");
//Define POSTS and set into SESSIONS
foreach ($posts as $post) {
if (isset($_POST[$post])) {
$_SESSION[$post] = $_POST[$post];
$$post = $_SESSION[$post];
}
}
I also tried it manually like:
$_SESSION['auto_year'] = '2012';
But it still doesn't work. I did call session_start() on top of the pages of both but it just keeps giving me that error.
using ${} you can create dynamic variables. In your case just change to:
${$post} = $_SESSION[$post];
Another option is to create the variables using extract() which enables you to set all variables in one go;
extract($_SESSION);
Will create a variable for each key in the session, e.g. $auto_year, $auto_brand, etc.
http://php.net/manual/en/function.extract.php
note
I'm wondering why you want to have all data in separate variables, whereas a single associative array may be just as easy to handle?
It's likely something wrong with the $_POST data coming from your form. As I don't have form info, I tried this by setting $_POST.
session_start();
$_POST['auto_year'] = 1977;
$posts = array("auto_year", "auto_brand", "auto_model", "auto_bodywork", "auto_doors",
"auto_fuel", "auto_gearbox", "auto_type", "auto_uitvoering", "auto_part", "auto_description", "email_address");
//Define POSTS and set into SESSIONS
foreach ($posts as $post) {
echo $post."<br>";
if (isset($_POST[$post])) {
echo "Creating variable for $post<br>";
$_SESSION[$post] = $_POST[$post];
$$post = $_SESSION[$post];
echo '$auto_year: '.$auto_year . "<br>";
}
}
echo "<br><br>".var_dump($_SESSION);