i want to store a value in to session using PHP
for example $id=10 i want to store this value in a session
i tried
$pid= session_id($id);
echo $pid;
and
$pid = $_SESSION['$id'];
but not working
at the top of page
session_start();
then to set session variable
$_SESSION['id'] = $someID;
To retrieve the value of id
$pid = $_SESSION['id'];
Further more read more about session here
Here is the right code to store the variable in PHP session:
<?php
session_start();
$id = 10;
$_SESSION["user_id"] = $id;
?>
Now to get the session data:
<?php
session_start();
echo $_SESSION["user_id"];
?>
Also, I have write a complete guide on PHP session on one of my blog: How to start a PHP session, store and accessing Session data?
Try this..
<?php
session_start();
$id = 10; //store 10 in id variable
$_SESSION['id'] = $id; // now, store $id i.e, 10 in Session variable named id.
echo $_SESSION['id']; // now, print the Session variable
Related
i want insert data to current logged in user i changed 4 to session id but error please help me how to store id in session and use here
login.php
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
home.php
<?php
//Get current user ID from session
$userId = 4;
//Get user data from database
$result = $db->query("SELECT * FROM user WHERE id = $userId");
$row = $result->fetch_assoc();
?>
Try this.Directly add the value to session array.It will updated with session
session_start();
$_SESSION['user_id'] = 4;
$userId =$_SESSION['user_id']
executing the function call 'session_id()' returns the id of the session for the vistor, the session is actually a file kept in a directory of your server, the session_id will point to this file in all instances that I've personally seen.
to actually access the session you need to use $_SESSION to set and read data :) Happy Coding!
I downloaded a php login source and now it works and it even logs in. when it logs in it shows your name with
<?php echo $_SESSION['username']; ?>
but i also want to display there balance by putting
<?php echo $_SESSION['balance']; ?>
But it doesnt display the balance?
https://gyazo.com/7cd0a7888ac976590391888925d0c18f
I added the balance table to the existing tables.
I really dont know what to do! :(
Please Insert;
<?php session_start(); ?>
In every page where you want to access $_SESSION global array.
in your case;
<?php session_start(); ?>
$_SESSION['balance'] = 1000;
echo $_SESSION['balance']; // will output 1000.
suppose this page was set-session-value.php and you want to get this $_SESSION['balance'] value in get-session-value.php do as follow in get-session-value.php file;
<?php session_start(); ?>
echo $_SESSION['balance'];
In your login.php add this line after setting session for username
$_SESSION["balance"]=$balance;
And in your home, it is always advised to check if session exists and then print it. So,
if(isset($_SESSION["balance"])){echo $_SESSION["balance"]; }
start session before use
<?php
session_start();//start session
$_SESSION['username']='abc';//Set value session
$_SESSION['balance']=10;
echo $_SESSION['username']; //Use value of session
echo $_SESSION['balance'];
?>
please use this process for session use
When you logged into your application.After that you should want to add following code into top of the page.
session_start();
$_SESSION['username'] = $userNameValue;//set user name
echo $_SESSION['username'];
And then you should want to fetch data related to who logged user from your user table by using query.
Example
$query = "SELECT * YOURTABLENAME WHERE username='$_SESSION['username']'";
mysql_query($query, $connection) or die(mysql_error());
$data = mysql_fetch_array($query);
Then you can assign to $_SESSION['balance'] for value like this;
$_SESSION['balance'] = $data['balance'];
echo $_SESSION['balance'];
Let's say I have the following URL:
http://test/order?id=263&name=John
A php file handles the URL and I use $_GET to take the data from the URL and bind it to a variable:
<?php
$id = $_GET['id'];
$name = $_GET['name'];
?>
As it stands now, the user is able to change the URL and subsequently the values of the variables. I want the variables to be bound once and not to be subjected to change after. Is there any way to do that with PHP?
you could actually store them in session....
1)at the very top of the page initialize the session
2) check if the value in session exists and if not create it.
at this point every further change will not be taken in consideration,
<?php
session_start();
if (!isset($_SESSION['user'])) {
$_SESSION['user'] = [
'id' => (int) $_GET['id'], //Cast the id to int
'name' =>urldecode($_GET['name']) //url decode the name
];
}
Now you have your data stored in session and you can call it using:
$_SESSION['user']['id']
$_SESSION['user']['name']
and they will never be overwritten, if you want to be updated on every call or change it if some parameter has been passed you can add some option in the condition
if (!isset($_SESSION['user']) && $_GET['updateData') == 1) {
$_SESSION['user'] = [
'id' => (int) $_GET['id'], //Cast the id to int
'name' =>urldecode($_GET['name']) //url decode the name
];
}
<?php
start_session();
if(isset($_SESSION['name'])){
$name = $_SESSION['name'];
$id = $_SESSION['id'];
} else {
$_SESSION['id'] = $_GET['id'];
$id = $_GET['id'];
$_SESSION['name'] = $_GET['name'];
$id = $_GET['name'];
}
?>
You could try something like this.
Just save those variables in session or in some file until session is closed, if you have long session (login/logout). You can create array in session and keep there all these ids along with session ids. Hope that helps
according to php.net in order to change the session id in the cookie then the line session_id($id) has to come before the line session_start(). my question is when I first started a session and assigned values to the $_session array and then in another script I changed the session id. why was the data that I assigned has been deleted??
first script - starting a session and assigning values to the session array:
<?php
session_start();
// store session data
$_SESSION["username"] = "joshmathews" ;
$_SESSION["name"] = "josh" ;
?>
second script- changing the session id but failing to access the data from the first script:
<?php
session_id(200) ;
session_start();
echo "<br>Username = " . $_SESSION["username"];
echo "<br> name= " . $_SESSION["name"] ;
echo "<br>" . session_id() ;
?>
1.if you want to update the session_id() value then you should session_regenerate_id().
2.if you want to completely delete the session details, then use session_destroy()
I'm trying to set a session variable and use it on another page.
I have: pg1
session_start();
$_session['sessionID'] = $row['ID'];
Then on page two I have.
session_start();
$userID = $sessionID;
But when I use JC to alert this out I get nothing.
Am I doing this wrong?
Rather than:
$userID = $sessionID;
Use:
$userID = $_SESSION['sessionID']
You need to specify the $_SESSION there because that is the array you stored the value in :)
Have a look at this session tutorial if you want.
On the second page, you'll need to say
$userID = $_SESSION['sessionID'];
You need to do the following on page 2:
session_start();
$userID = $_SESSION['sessionID'];
You also need to use $_SESSION, not $_session