views:

93

answers:

5

I am trying to use a global in a file that is just used to post data to. The global is not being registered on that page. How can I get the globals accessible in that page.

EDIT maybe I wasnt too clear so let me clear it up. I have these files

index.php

global $user;
echo $user->uid;

post.php

global $user;
echo $user->uid;

now from index.php I am posting to post.php through jquery. However when I echo $user->uid from post.php it is not echoing but when I echo it from index.php it is showing it. How can I get that $user->uid accessible from post.php.

A: 

if you need a global inside a function do like this

function phpFoobarFunction(){
  global $my_global;
}

reference about scoping

for superglobals like $_POST, $_GET (etc...) they are available anywhere.


from your updated question, you cannot share variable between two different call of PHP. you would have :

  • to recreate your $user class by realoading from the DB (or your source)
  • stock the object in the $_SESSION
  • pass the user object via javascript(but that's dangerous because user can edit that) and add it has params to your ajax call.

more reference about how PHP play with session there

RageZ
+1  A: 

Another way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.

Example from php.net

<?php
$a = 1;
$b = 2;

function Sum() {
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
} 

Sum();
echo $b;
?>
Yada
I edited post hopefully that will help.
ngreenwood6
+1  A: 

I think you should be looking into sessions rather than globals. From what it sounds like, you are registering a variable on one page, then when the user moves to another page, you want that variable to be usable.

Sessions will let you do that. Simply use this code:

<?php
session_start();
$_SESSION['my_var'] = 'foo';

And on subsequent pages...

<?php
session_start();
echo $_SESSION['my_var'];

Which will output foo.

http://www.php.net/manual/en/book.session.php

mattbasta
This is what you need. Lukman describes the problem, but this is the solution.
Chris
+1  A: 

You have to remember that when the user submits the form and redirect to post.php, the variables in index.php are long gone. PHP executions are short-lived unlike normal desktop applications.

Whatever you do in index.php to get the $user object must be repeated in post.php. If you don't want to replicate the code, just put it in some file init_user.php and have both index.php and post.php include the file using include_once('init_user.php');

Lukman
A: 

I agree with mattbasta, globals were disabled by security reasons. You can use it anyway but the right way is to use session.

Other thing is, you said: "now from index.php I am posting to post.php through jquery." Are you using some Ajax call? If not I don't understand why use JS to do this... If you are sending the data through Ajax and only want to send this param try this:

<input style="display:none" type="hidden" id="uid" name="uid" value="<?= $user->uid ?>" />

And then handle it from your ajax call like:

$.ajax({
    type: "POST",
    url: "/post.php",
    data: "uid=" + uid + "&otherVar=" + otherVar, //and so on..
    success: function() {
     //Here you can notify the user that the script ran successfully 
     }
});//end of ajax

Hope this help you.

Luis