/    Sign up×
Community /Pin to ProfileBookmark

how to share a variable in two php files?

I want to use a variable as the index of an array. In a.php, it will display the content of the array. When a “save” button is pressed in a.php, b.php will increase the variable by 1, i.e., a new row is added to the table. But I don’t know how to set the variable, because I tried use “global” or “static”, both of them don’t work. I think the manner I called the variable is wrong. But I don’t know how to do that. Can anyone help me about that? Thanks a lot!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@cwrathOct 12.2005 — Use sessions and just increment (etc.) the variable on b.php
Copy linkTweet thisAlerts:
@reinmontrealauthorOct 12.2005 — I am sorry but I don't know how to use sessions. Can you give me an example, Cwrath? Thank you very much!
Copy linkTweet thisAlerts:
@cwrathOct 12.2005 — CW will do.

Sessions? Sigh. Erm let me see....


a session takes info and stores it in a file on the server (ish) to access session data you must first use start_session, to add data to a session use the global array $_session['foo'] = 'bar'


e.g.

<?php

//page1

session_start();

$_SESSION['name'] = "CW";

echo '<a href="page2.php">P2</a>';

?>



<?php

//page two

session_start();

echo "Hello $_SESSION['name'];

?>

(havnt checked, but it should work)
Copy linkTweet thisAlerts:
@cwrathOct 12.2005 — Oh an to destory a session (usually when somebody is loggin off) use session_destroy();
Copy linkTweet thisAlerts:
@reinmontrealauthorOct 12.2005 — Thank you very much, Cwrath!
Copy linkTweet thisAlerts:
@cwrathOct 13.2005 — np
×

Success!

Help @reinmontreal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...