globals php help

此生再无相见时 提交于 2019-12-25 03:13:46

问题


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.


回答1:


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;
?>



回答2:


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




回答3:


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');




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/1753530/globals-php-help

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!