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..
error_reporting(E_ALL);
on top of your script. – mario$_session
. You might want to change it, just to be sure you're on the right foot there. – zneak