Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
#1 2024-07-01 18:20:36
- ironmangary
- Member
- From: United States
- Registered: 2022-10-13
- Posts: 36
Global Variables
Using mck_login, I have a page where users can, well, log in. Once logged in, I save their login name to a variable so I can use it later.
<txp:variable name="activeuser" trim><txp:mck_login name="name" /></txp:variable>
<p>Welcome to your Slam Center, <txp:variable name="activeuser" />!<br>
The above does display the username correctly. However, later on down the page, I have a link the user can click to create a new league:
<a href="index.php?s=league_new">Create New League</a></p>
On the league_new page, I need the “activeuser” variable, but it’s not getting passed from the previous page.
<p>Thanks <txp:variable name="activeuser" output /></p>
I put that in as a test, and the variable has no value. What am I missing? Can what I want be done?
Offline
Re: Global Variables
txp variables won’t persist across pages unless you save them in some form of persistent storage, whether in a cookie or as a database field.
I’m not familiar with mck_login, but could you not just set the variable again at the top of the page using the same code as before, e.g.
<txp:variable name=“activeuser” trim><txp:mck_login name=“name” /></txp:variable>
That would be easiest. Otherwise, the “name” of the currently logged-in user is already stored in the internal $txp_user
variable, and from that you can get the “Realname” of the author:
<txp:php>global $txp_user; echo get_author_name($txp_user);</txp:php>
Note, for this you need to allow PHP in pages (+ possible articles depending on where you are using it) in the Admin › Preferences › Publish pane. If you have guest users, you should check that this doesn’t enable them to perform malicious actions.
TXP Builders – finely-crafted code, design and txp
Offline
#3 2024-07-01 19:37:29
- ironmangary
- Member
- From: United States
- Registered: 2022-10-13
- Posts: 36
Re: Global Variables
Gotcha. Thanks for the help!
Offline
#4 2024-07-02 16:00:38
- ironmangary
- Member
- From: United States
- Registered: 2022-10-13
- Posts: 36
Re: Global Variables
Apparently, mck_login variables don’t persist, either. I tried the $txp_user code, which worked great, except is there a way to get the login name rather than the real name?
jakob wrote #337361:
txp variables won’t persist across pages unless you save them in some form of persistent storage, whether in a cookie or as a database field.
I’m not familiar with mck_login, but could you not just set the variable again at the top of the page using the same code as before, e.g.
<txp:variable name=“activeuser” trim><txp:mck_login name=“name” /></txp:variable>...
That would be easiest. Otherwise, the “name” of the currently logged-in user is already stored in the internal
$txp_user
variable, and from that you can get the “Realname” of the author:
<txp:php>global $txp_user; echo get_author_name($txp_user);</txp:php>...
Note, for this you need to allow PHP in pages (+ possible articles depending on where you are using it) in the Admin › Preferences › Publish pane. If you have guest users, you should check that this doesn’t enable them to perform malicious actions.
Offline
Re: Global Variables
ironmangary wrote #337365:
Apparently, mck_login variables don’t persist, either. I tried the $txp_user code, which worked great, except is there a way to get the login name rather than the real name?
Yes, just do:
<txp:php>global $txp_user; echo $txp_user;</txp:php>
The other variant just puts that through a function that looks up the real name.
TXP Builders – finely-crafted code, design and txp
Offline
Pages: 1