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()
Related
I have this in my session
<?php
session_start();
if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
header("location: index.php");
exit;
}
?>
how can I calling other column which is user_id as session and insert into new table as current user that do insert action.
Yes, it is possible to create $_SESSION variables, it is fairly easy and straight forward. They are each element arrays.
You mentioned in your question that you wanted the user_id session variable, so just create the session array with the key of user_id.
$_SESSION['user_id'] = USER_ID;
$_SESSION['username'] = USER_NAME;
$_SESSION['email'] = USER_EMAIL;
//...
For more information on how to work with $_SESSION variables read PHP's Manual.
<?php
session_start();
if(!isset($_SESSION['myusername'])) {
header('location:kursna_login.php');
}
?>
Code always redirect me to kursna_login.php when the username and pass are correct in the login form. Is this the right declaration of the code?
if you are redirected to "kursna_login.php" so the session variable is not set , once you store the username in a session variable , for example let's suppose the username is coming from a form with a post method
you should save iy like this
$username = $_POST['username'];
$_SESSION['myusername'] = $username;
and after this you have to checkk the session array eith a simple print_r like this
<?php
session_start();
echo "<pre>";
print_r($_SESSION);
?>
this would give you all the session variables setted in your app as key => value and then try to verify if the name is really setted in session global variiable or not .
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'];
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
I have this problem. Session does not work when I use with $_POST. If I add 63 manualy it will works across pages. I can see the output 63.
$_SESSION['name'] = 63;
echo $_SESSION['name'] ;
but this below won't work when I switch between pages. The $row['id'] output is also 63.
$cari = "SELECT id FROM dns_soa WHERE `origin` = '".$_POST['origin']."'";
$keputusan = mysql_query($cari);
$row = mysql_fetch_array($keputusan);
$_SESSION['name'] = $row['id'];
echo $_SESSION['name'] ;
When I go to 2nd pages I can see the output but when I went back to 1st pages the output is gone. Any idea?
Because when you are coming back it will again set the session variable, as this time you don't have the $_POST[origin] variable that leads to $row['id']="" and the session variable also NULL...
What you should do is..
$cari = "SELECT id FROM dns_soa WHERE `origin` = '".$_POST['origin']."'";
$keputusan = mysql_query($cari);
$row = mysql_fetch_array($keputusan);
if(!isset($_SESSION['name']))
$_SESSION['name'] = $row['id'];
echo $_SESSION['name'] ;
When you go back to the page where you have used $_POST, $_POST becomes empty, unless your browser posts it again. So the SQL query returns no result. Hence, $_SESSION['name'] becomes empty.
Also make sure you have invoked session_start on every page before using $_SESSION