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
Related
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
How to post values to loginchk_coustomer.php given in below code, not through Url by any other way.
Is there any other way to post these value to loginchk_coustomer.php becoz it is not secure.
<?php
include "include/connect.php";
$user_name = $_REQUEST['user_name'];
$password = $_REQUEST['password'];
//echo "select * from school_info where school_id='$user_name' and school_password='$password'";
$sql_query = mysql_fetch_assoc(mysql_query("select * from school_info where school_id='$user_name' and school_password='$password'"));
$db_username = $sql_query['db_username'];
$db_password = $sql_query['db_password'];
$db_databasename = $sql_query['db_databasename'];
echo "<script>";
echo "self.location='member/loginchk_customer.php?db_username=$db_username&db_password=$db_password&db_databasename=$db_databasename&user_name=$user_name&password=$password'"; // Comment this line if you don't want to redirect
echo "</script>";
?>
You need to create a session to store all that information.
Here's what they are - from http://php.net/manual/en/features.sessions.php:
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
To start a session write at the beginning of your code:
session_start(); // needed in all pages that will use the variables below
and then after your assign the information this way:
$_SESSION['username'] = $sql_query['db_username'];
$_SESSION['password'] = $sql_query['db_password'];
$_SESSION['databasename'] = $sql_query['db_databasename'];
All the information will persist on those variables along the site until you do:
session_destroy();
I also recommend you not to redirect with javascript, but this way in PHP:
header('Location: member/loginchk_customer.php');
Possibly after checking this answer you will think about to change the way you check the login information. But that's okay. It's the way of learning.
More information about sessions: http://php.net/manual/en/book.session.php
I hope this helps.
I have the following login script, where i do use sessions.
<?php
session_start();
if(isset($_SESSION['logged_in'])){
$id = $_SESSION['id'];
header("Location: start.php?id=$id");
exit();
}
if(isset($_POST['submit'])){
$x1 = $_POST['x1'];
$x2 = $_POST['x2'];
...
$query = $db->query("SELECT * FROM table WHERE x1='".$x1."' AND x2='".$x2."'");
if($query->num_rows === 1){
$row = $query->fetch_object();
$id = $row->id;
$_SESSION['logged_in'] = true;
$_SESSION['id'] = $id;
header("Location: start.php?id=$id");
3more queries
exit();
start.php will be just:
<?php
echo $_GET['id'];
?>
I thought $_GET['id'] would be stored on the server so that $_GET should be displayed. The fetch_object is working. I know that, because it will be displayed the right way at "id=$id" at the browser. So would someone be that friendly and could help me out. Thanks!
The $_GET superglobal is defined as part of the URL string:
http://example.org/index.php?foo=bar&baz=1
In index.php:
echo $_GET['foo']; // bar
echo $_GET['baz']; // 1
So $_GET is not stored on the server, but is passed with each HTTP request, as is $_POST, but that is passed in the HTTP headers rather than simply appened to the end of the URL.
$_GET variables are those passed via the URL, i.e. index.php?foo=bar&baz=qux (foo equals bar, baz equals qux).
These variables are not stored on the server as a part of the session, but rather only exist with that request. If you want to store information on the server as a part of the session, you should use $_SESSION instead, which will exist within the current session, regardless of the request.
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
I have got a basic login script at the moment.
When the user logs in 2 variables are defined:
$_SESSION['user_id'] = $user['user_id'];
$_SESSION['username'] = $user['username'];
the index.php file has this
session_start();
if(array_key_exists('user_id', $_SESSION) && array_key_exists('username', $_SESSION)):
That is all fine, however once the session is started I would like to add more values from a database so I have this code here:
$res = mysql_query($sql);
$_SESSION = mysql_fetch_assoc($res);
When I do this it overrides the $_SESSION['user_id'] and $_SESSION['username'].
I tried this:
$_SESSION .= mysql_fetch_assoc($res);
but it didn't work.
Does anyone have any ideas?
Thanks
peter
$_SESSION is an array. You are over-riding the entire $_SESSION variable. Do this instead:
$_SESSION['data'] = mysql_fetch_assoc($res);
http://us2.php.net/manual/en/session.examples.basic.php
That's because you're setting the value of the variable $_SESSION to the return value of mysql_fetch_assoc($res);.
What you want to do is something like $_SESSION['query_result'] = mysql_fetch_assoc($res). If you just want to add a single column of your database result to the session you would do it like this:
$res = mysql_query($sql);
$data = mysql_fetch_assoc($res);
$_SESSION['myKey'] = $data['myKey'];
Try merging the arrays
$res = mysql_query($sql);
$_SESSION = array_merge(mysql_fetch_assoc($res), $_SESSION);
See http://php.net/manual/en/function.array-merge.php
Functions mysql_* are deprecated and removed in the future, use PDO or MySQLi is recommended.
Some hosters have disabled these functions.