tags:

views:

112

answers:

3

Hello, When I call a php file using Ajax, I do this :

function logIntoApp(){
    Ext.Ajax.request({
        url:'php/login.php',
        params:{
                action:'login',
                email:Ext.getCmp('userLogin').getValue(),
                motDePasse:Ext.getCmp('passwordLogin').getValue()
        },
        method:'GET',
        success:function(result, request){
            //Ext.MessageBox.alert('Success', );
            if(result.responseText == 'admin'){
                //Ext.MessageBox.alert('Alert', result.responseText);
                window.location.replace("administration.php");
            }
            else if(result.responseText == 'pro'){
                //Ext.MessageBox.alert('Alert', result.responseText);
                window.location.replace("proAdministration.php");
            }
            else{
                Ext.MessageBox.alert('Alert', result.responseText);//'Email ou mot de passe incorrect!');
            }
        },
        failure:function(result, request){
            Ext.MessageBox.alert('Failed', 'Erreur de chargement de données');
        }
    });
}

in my php file, I set a $_SESSION to use it in my administration.php

if($role=='Administrateur'){
                    $_SESSION['role']='admin';
                    $_SESSION['email']=$email;
                    $message = 'admin';
                }

And in my administration.php, I have this :

<?php
session_start();
echo "alert(''". $_SESSION['role'] . ")";
    if($_SESSION['role']=='admin'){
        //echo "alert('pro')";
    }
    else{
        //echo "alert('admin')";
        header("Location:index.php");
    }
?>

And in my index.php I have this :

<?php

    session_start();
    $_SESSION['role']='visiteur';
    $_SESSION['email']='no';


?>

the problem is that I'm never redirected to administration.php, I think because the $_SESSION['role'] is never set as 'admin'

A: 

You should try session_start immediately after php tag no spaces in between session start and php tag

vinothkumar
how will no spaces help? You can start the session whenever you like, just make sure it's before using the $_SESSION superglobal, and yes, before sending any output.
Alexander
That's irrelevant. You can have as many spaces/line breaks/tabs as you want between <?php and session_start();. Only thing that matters is that nothing is printed _before_ the php tag.
halfdan
A: 

I'm no ExtJS expert, but judging by the logic, your result.responseText is always equal to alert('something'), and never something simple as admin or pro, so it gets to the last else statement. Maybe you should fix the output sent by PHP, skip the alert()

Alexander
I'm always using the content of result.responseText and always getting what I sent from the php, the problem is that the $_SESSION['role'] is not set in the php file
taichimaro
Did you do a var_dump($_SESSION) after session_start() and see the output?
Alexander
I want to know something before, should I do session_start(); in all my files ???
taichimaro
I tried the var_dump($_SESSION) and it's not containing admin, it contains what it was set in the index.php
taichimaro
where do you set it to 'admin'? In what file? When do you call it? And to answer - you need session_start when you're using sessions. If it's a request to a file that's sending a bitstream of an image, and there is no need for sessions, don't start it.
Alexander
I'm sending an authentification request using ajax to a php file, so, if the person is an admin, I set up the $_SESSION['role']='admin'; without doing the session_start(); because when I write session_start(); I have this messageWarning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /homez.380/webadmin/www/wa/php/login.php:1) in /homez.380/webadmin/www/wa/php/login.php on line 2
taichimaro
A: 

I found a solution in a forum, I changed my file from utf-8 to ainsi and it works I really dont understand, but who can explain please ?

taichimaro