开发者

Php variable in several files including jquery

i created two variable and these two variable are using everywhere . i don't want to store these variable in to database e.g ( $username ,$password and there are 3 files using these variable load.php,index.php and add.php , i am also using jquery to load the add.php as in ajax (to add the user in JSON)

$.ajax({
        type: "POST",
        url: "add.php",
        data: "twitter="+encodeURIComponent(twitter),
        /* Sending the filled in twitter name */
        success: function(msg){

            /* PHP returns 1 on s开发者_运维问答uccess, and 0 on error */
            var status = parseInt(msg);
            if(status)
            {
                $('#response').html('Thank you ');
                $('#twitterName').val('');
            }
            else
            $('#response').html('<span style="color:red">There is no user.</span>');

        }
    });

how can i use these two variable in a single file to perform the whole operation

Thanks


Sessions and Cookies are the standard way to create persistence with variables around a system but if you're actually wanting to store something like a password then this is not advisable. Exactly where you store or dont store these variables is very dependent on the context, for example if the variables are static and won't change but needed around the system then its commonplace to use something like a PHP constant in a commonly included file. If you're wanting to create persistence with a user login then its safer - for example - to store in a session the username and a reference to the fact the user is logged in, rather than the full login details.


Include a file which declares and sets these variables in all 3 php files and use them as you want.


Create a file and name it something like db_include.php

Inside this file, store the variables you want to use across the 3 files as follows:

<?php

var $username = 'username';

var $password = 'idontknow';

?>

Then inside each of the 3 php files (load.php, index.php, and add.php), add the following code:

<?php

include 'db_include.php';

?>

Once you have set this up, you should be able to access the $username and $password variables from all three files. If the username or password should change, you will only need to update the db_include.php file with the new values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜