session_id() isn't output anything on PHP
I have two pieces of codes one is the front end website php code that render the front end web pages and another is the backend api PHP code. On my front end website code I have session_start();
I have a piece of back end API code that once a user login then I give him or her a sessionid by doing sessionid();
However, when I use $sess开发者_JAVA百科ionid = session_id();
and echo $sessionid
, nothing's outputted.
$user="root";
$password="";
$database="test";
mysql_connect('localhost',$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$username = $first_param;
$username = str_replace( "UserName=", "", $username);
$password = $second_param;
$password = str_replace( "Password=", "", $password);
$query = "SELECT * FROM `user` WHERE username ='$username' AND password = '$password' ";
$result=mysql_query($query);
$num=mysql_num_rows($result);
if (debug) {
echo "<br>";
echo "--------the num of login is ----" .$num;
}
if ($num <1) {
}
else {
$sesid=session_id();
if (debug) {
echo "<br>";
echo "-----login sessionid is------" .$sesid;
echo "<br>";
}
$query = "UPDATE `user` SET sessionid='$sesid' WHERE username ='$username' AND password = '$password'";
mysql_query($query);
}
Make sure that the code running session_id() is on the same page or included into the page running session_start(). If not, you'll have to run session_start() again to get the session for the new page.
Actually, when you try this, it must work 100%
<?php
session_start(); //This must be called before the session_id();
$sessionid = session_id();
echo $sessionid;
?>
The function is called session_id()
, note the underscore.
Also your updated code seems to be missing the call to session_start()
. If this is a separate PHP file from the one session_start
is otherwise being called in you'll need to call it again at the beginning of this file.
精彩评论