PHP Session Variable not being stored (I am calling Session_Start())
I can for the life of me figure out why I am losing all session variable
Here is my php code:
<?php
if($_POST['loginButton']){
session_destroy();
session_start();
$con = mysql_connect("localhost", $_POST["userNameTextBox"], $_POST["passwordTextBox"]);
if (!$con)
{
echo 'Could not connect: ' . mysql_error() ;
header( 'Location: index_error.html' ) ;
die();
}
//else
$_SESSION['IP']="localhost";
$_SESSION['username']=$_POST["userNameTextBox"];
$_SESSION['password']=$_POST["passwordTextBox"];
$_SESSION['database']="Chuckles_DB";
$_SESSION['Result'] ="Hi";
}
?>
but when I run the next php script
<?php
$res=session_start();
echo "Session Started with ".$res." <br>";
echo "Test::>".$_SESSION['IP']." ".$_SESSION['username']." ".$_SESSION['password']." ".$_SESSION['database']."<:::<br>";
$con = mysql_connect( $_SESSION['IP'],$_SESSION['username'],$_SESSION['password']);
开发者_运维问答if (!$con)
{
echo 'Could not connect: ' . mysql_error() . " <br><br>";
echo '<a href="index.html" target="">Please re-login to the web site</a> ';
}
else{
echo $con;
}
?>
The values set are not present (they are present the first time this page is posted to, but if posted to again, they are gone);
here is my phpinfo page
Thanks for any help!
May be it is not a suitable answer, but I found PHP sessions rather painful. Sometimes they cause trouble and they can easily be replaced by a simple database table. You just need to create a session ID and store it in your database table and cookies. Probably it will work faster if you need to connect database anyway. However, if it is not possible in your situation, you might also consider doing what PHP does; using file system to store session data. If you can use a specific database and table for your login information, use it. If you have no other option consider using files.
PS: If you use a manual method (database or file system), create long session ids.
Your code should be like
<?php
if(!session_start()){
echo "Session Failed!";
exit();
}
if(isset($_POST['commit_sale'])) {
echo 'Test7';
commit_sale($_SESSION['ticker']);
unset($_SESSION['ticker']);
}
?>
精彩评论