James Davies
James Davies

Reputation: 3

PHP Global Variable throught pages

I am using jquery throughout a php project, so all pages load dynamically into the main page, I am trying to make links only visible to a certain user, so if they goto the main page and append the URL, i.e ?mode=55rt67 this gets stored as a variable and can be checked throughout the app. I am using the below, but it doesnt work. any suggestions?

if (empty($_GET)) {
$mode = "user";
}else{
define ('$mode', '($_GET['mode']);
}

Upvotes: 0

Views: 71

Answers (3)

James Davies
James Davies

Reputation: 3

if (empty($_GET)) {
    $_SESSION['mode'] = "user";
}else{
    $_SESSION['mode'] = $_GET['mode'];
}

The above solution worked perfectly, added session_start to the index page, and it now carries through the rest of the pages.

Awesome, thanks all

Upvotes: 0

Jigar Patel
Jigar Patel

Reputation: 207

Change your code with below code.

// Try this
        if(isset($_GET['mode']) ){  
           define ('mode',$_GET['mode']);
        }else{
           define ('mode',"user");
        }
    echo mode;

//OR

       if(isset($_GET['mode']) ){  
           global  $mode = $_GET['mode'];
        }else{
           global  $mode = "user";
        }
 echo $mode;

Upvotes: 0

Sal00m
Sal00m

Reputation: 2916

define is used to declare constants, you want to use a variable, not a constant.

UPDATED (use session to store mode variable):

if (empty($_GET)) {
    $_SESSION['mode'] = "user";
}else{
    $_SESSION['mode'] = $_GET['mode'];
}

And don't forget to use session_start on every page

Upvotes: 2

Related Questions