Page 1 of 1

How do i use current user id for my site?

Posted: Mon May 27, 2019 12:09 pm
by Echo Dino
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

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

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

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

Posted: Mon May 27, 2019 12:38 pm
by Echo Dino
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;
?>

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

Posted: Mon May 27, 2019 1:41 pm
by david63
I don't see anything in your code that relates to phpBB

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

Posted: Mon May 27, 2019 1:47 pm
by Echo Dino
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

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

Posted: Mon May 27, 2019 3:08 pm
by kasimi

Code: Select all

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

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

Posted: Mon May 27, 2019 3:19 pm
by Echo Dino
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

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

Posted: Mon May 27, 2019 3:37 pm
by EA117
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.

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

Posted: Tue May 28, 2019 6:28 pm
by mrgoldy
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) }}

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

Posted: Tue May 28, 2019 7:53 pm
by EA117
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.