Question

I know, this question asked many times but i couldn't found a solution.
I've coded a php/JS script to get data from server and show them.
I am using Ajax to validate is user authenticated? I am using below method to do it;
My index.php:

>?php
...
...
$.post("data.php",{ req:'login',
username:$('#username').val(), //user enters given id
pass:$('#password').val(),
job:$('#job').val(),
rand:Math.random() } ,
function(data)
{
if (data == 'yes') // user is authenticated.
document.location='system.php';
}
...
...
// i am using a form to send collected data to data.php
?>

in data.php code is like this:

>?php
session_start();
$link = mysql_connect("localhost", "user", "pass"); 
if (!$link) {
    die("No connection : " . mysql_error());
}
    mysql_select_db("database");

if ($_POST["req"] == "login"){
    $user_id=($_POST["username"]);
    $user_pass=($_POST["pass"]);
    $user_job=($_POST["job"]);

    // select proper table (related to his/her job) and control did user supplied correct password?

    if( password is correct ){
    $_SESSION["id"]=$_POST["username"];
    // or $_SESSION["id"]=$user_id;
    echo "yes";
    }
    else echo "no"; //Invalid Login
} else if ($_POST["req"] == "userinfo"){
...
...
...
} else 
...
...
?>

system is working fine until here and i am redirecting to system.php, in system.php i want to use $_SESSION variable to get loged user's name in JavaScript.
Part of my system.php;

>?php
session_start();
if ((isset($_SESSION["id"]))||(!empty($_SESSION["id"]))) header("Location:index.php");
$userno = $_SESSION["id"];
if(isset($_GET["logout"]))
{
    session_destroy();
    $_SESSION["id"] = FALSE;
    header("Location:index.php");
}
?>
...
...
...
>script type="text/javascript">
$.post("data.php",{ req:'userinfo',username:'>?php echo ($userno) ?>',rand:Math.random() } ,function(data){
// sure ?php is written corrctly in script ;)
//.... again get data from data.php and put results into page..
});
>/script>

This code is working properly in my PC (xampp) but on my server there is no chance..
echo ($userno) outputs nothing.
I've searched many place and i found that it is because register_globals is off. I understood why it is.
But why i can't get the value of $_SESSION["id"] and assign it to $userno in my system.php
NOTE : I am using same data.php file for many other requests and all these requests are dependent to username but different conditions change the tables that use for data source, so i can't get information at the begining.
Thanks right now...
Note: Because of i couldn't find the correct way to using code highlighting i put >?php for starting of php code parts and >script for JS code parts..

Was it helpful?

Solution 3

It is very interesting, i've controlled error.log file on my server (i don't know why i didn't control before) there are some errors say: sessions already sent by ... like errors
and i found a forum page that says :
Sometimes the problem is caused by the Include Unicode Signature (BOM) property, then i controlled my coding type and i saw it is Unicode with BOM (i am using without BOM but i don't understand how it could happen), when i convert without bom problem is solved.

This is totally my false (even misinformation) thanks for all your helps.

OTHER TIPS

I believe it should be $_SESSION, not $_session. It's a superglobal, and it is case sensitive.

echo '<pre>';
print_r($_SESSION);
die;

What do you get?

Check the contents of the $userno on the server-side:

$userno = $_session["id"];
var_dump($userno);
die;

Simple and easy solution to register yours variables.

foreach ($_POST as $key => $value) {
 eval("$".$key."= '".$value."';");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top