How do i use current user id for my site?

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
User avatar
Echo Dino
Registered User
Posts: 37
Joined: Fri Mar 22, 2019 11:09 am
Name: Saba
Contact:

How do i use current user id for my site?

Post by Echo Dino » Mon May 27, 2019 12:09 pm

hi :D

i've a little question about current user id need for this form.

Code: Select all

<form action="test.php" method="get">
<input type="hidden" name="kind" value="1"><br>
<input type="hidden" name="user_id" value="???"><br>
PetName: <input type="text" name="petname" value=""><br>
<input type="submit">
</form>
my question is for ??? value in user_id input. how can i use current session or user id for who press submit?

example i'm an admin. i want when i press submit. that submits my id (2) in database . or onebody's id is 50 that submits id as 50 when pressing submit.



help? :D

User avatar
david63
Registered User
Posts: 16534
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: How do i use current user id for my site?

Post by david63 » Mon May 27, 2019 12:22 pm

Without seeing the full extent of your code, including the PHP code that is feeding into the template, it is impossible to answer.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
Echo Dino
Registered User
Posts: 37
Joined: Fri Mar 22, 2019 11:09 am
Name: Saba
Contact:

Re: How do i use current user id for my site?

Post by Echo Dino » Mon May 27, 2019 12:38 pm

david63 wrote:
Mon May 27, 2019 12:22 pm
Without seeing the full extent of your code, including the PHP code that is feeding into the template, it is impossible to answer.

OK

ADOPT.HTML

Code: Select all

<!-- INCLUDE overall_header.html -->
<div align="center">
<h2>Adoption Center (demo)</h2>
<br>
<br>

<div class="panel">
<h4>that's demo only. you can't adopt it right now click button to see why</h4>
 <img src="http://cranterns.epizy.com/main/img/cranies/jgh/young2.png" alt="jgh">
<form action="test.php" method="get">
<input type="hidden" name="userid" value="???"><br>
<input type="hidden" name="kind" value="1"><br>
PetName: <input type="text" name="petname" value=""><br>
<input type="submit">
</form>
</div>
</div>

<!-- INCLUDE jumpbox.html -->
<!-- INCLUDE overall_footer.html -->
and test.php

Code: Select all

<h1 align="center">Don't afraid! You see it because this page is under Maintenance</h1>

<div align="center"><img src="https://d29hzik3xqzv4r.cloudfront.net/original/2X/5/502d4bbdf1fd3240aee3ee5235e96c30b5015ed5.gif" alt="bug"></div>
<?php
include_once("config.php");
try {
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpasswd);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO phpbb_crany(crany_name, crany_breed, crany_gender, crany_level, crany_born, crany_mother, crany_father, crany_element, crany_user) VALUES (:cn, :cb, :cg, :cl, :cbo, :cm, :cf, :ce, :cu)");
    $stmt->bindParam(':cn', $cn);
    $stmt->bindParam(':cb', $cb);
    $stmt->bindParam(':cg', $cg);
    $stmt->bindParam(':cl', $cl);
    $stmt->bindParam(':cbo', $cbo);
    $stmt->bindParam(':cm', $cm);
    $stmt->bindParam(':cf', $cf);
    $stmt->bindParam(':ce', $ce);
    $stmt->bindParam(':cu', $cu);



    // insert a row
    $gender= mt_rand(0,1);
    $cn = $_GET['petname'];
    $cb = rand(0,1);
    $cg = 1;
    $cl = 1;
    $cbo = $_SERVER['REQUEST_TIME'];
    $cm = 0;
    $cf = 1;
    $ce = 1;
    $cu = $_GET['userid'];
    //end script
    $stmt->execute();

    echo "Crany adopted successfully named ".$_GET['petname']."<br>but you can't have it in your den because we're try to create den system right now";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
?>

User avatar
david63
Registered User
Posts: 16534
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: How do i use current user id for my site?

Post by david63 » Mon May 27, 2019 1:41 pm

I don't see anything in your code that relates to phpBB
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
Echo Dino
Registered User
Posts: 37
Joined: Fri Mar 22, 2019 11:09 am
Name: Saba
Contact:

Re: How do i use current user id for my site?

Post by Echo Dino » Mon May 27, 2019 1:47 pm

david63 wrote:
Mon May 27, 2019 1:41 pm
I don't see anything in your code that relates to phpBB
how can i relate to phpbb

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3897
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: How do i use current user id for my site?

Post by kasimi » Mon May 27, 2019 3:08 pm

Code: Select all

<input type="hidden" name="userid" value="{{ user.data.user_id }}"><br>

User avatar
Echo Dino
Registered User
Posts: 37
Joined: Fri Mar 22, 2019 11:09 am
Name: Saba
Contact:

Re: How do i use current user id for my site?

Post by Echo Dino » Mon May 27, 2019 3:19 pm

kasimi wrote:
Mon May 27, 2019 3:08 pm

Code: Select all

<input type="hidden" name="userid" value="{{ user.data.user_id }}"><br>
Worked! thank you <3

User avatar
EA117
Registered User
Posts: 903
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: How do i use current user id for my site?

Post by EA117 » Mon May 27, 2019 3:37 pm

kasimi wrote:
Mon May 27, 2019 3:08 pm

Code: Select all

<input type="hidden" name="userid" value="{{ user.data.user_id }}"><br>
Get out of here with that kind of sorcery. 😜 I had thought creation of an actual template variable was necessary to access the user class members, and so I had taken this approach of creating a template variable from an event listener. But no, it's already been there the whole time.... In other words, thank you.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1158
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: How do i use current user id for my site?

Post by mrgoldy » Tue May 28, 2019 6:28 pm

https://github.com/phpbb/phpbb/blob/mas ... g.php#L360

The entire \phpbb\user object is added to the template.
So you can access the user's data, style, ip, sessions etc...
For example: {{ user.data.user_id }}, {{ user.data.username }}, {{ user.data.user_colour }}.
But also: {{ user.ip }} and {{ user.style.style.id }}.
Or: {{ SOME_UNIX_TIME_VARIABLE|date(user.data.user_date_format) }}

User avatar
EA117
Registered User
Posts: 903
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: How do i use current user id for my site?

Post by EA117 » Tue May 28, 2019 7:53 pm

Yeah, now that I know what I'm looking for, I see only a couple of the proSilver templates ever need to take advantage of this, which contributed to why it wasn't more obvious. But definitely very cool to have access to.

Now I've simply thrown a <script>var sessionIPGlobal = "{{ user.ip }}";</script> into my overall_header.html and defined the BBCode as <script>document.write(sessionIPGlobal);</script>, with no more need for an extension or event listener to expose that info.

Post Reply

Return to “phpBB Custom Coding”