Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: andreblue2u on June 17, 2012, 07:42:48 PM

Title: Get a user name from simple portal page.
Post by: andreblue2u on June 17, 2012, 07:42:48 PM
So i want to be able to get a person's username from within a simple portal php page. What would i do to do so?
Title: Re: Get a user name from simple portal page.
Post by: TheListener on June 17, 2012, 07:43:47 PM
What exactly are you trying to do?
Title: Re: Get a user name from simple portal page.
Post by: andreblue2u on June 17, 2012, 07:57:16 PM
i want to retrivve the current user on the page its self. to pass on as a variable to a sql query
Title: Re: Get a user name from simple portal page.
Post by: TheListener on June 17, 2012, 07:59:28 PM
As this is mod related you would be better to use the mods support topic or www.simpleportal.net
Title: Re: Get a user name from simple portal page.
Post by: Arantor on June 17, 2012, 08:00:24 PM
Quote from: andreblue2u on June 17, 2012, 07:57:16 PM
i want to retrivve the current user on the page its self. to pass on as a variable to a sql query

Why, exactly?

Depending on what you're trying to use that query for 1) it might not even work and 2) odds are the user id is better to be used anyway (more reliable, faster for most things)

Let us know what you're trying to use it for and maybe we can provide you with better information on how to do it.
Title: Re: Get a user name from simple portal page.
Post by: andreblue2u on June 17, 2012, 08:11:30 PM
Quote from: Arantor on June 17, 2012, 08:00:24 PM
Quote from: andreblue2u on June 17, 2012, 07:57:16 PM
i want to retrivve the current user on the page its self. to pass on as a variable to a sql query

Why, exactly?

Depending on what you're trying to use that query for 1) it might not even work and 2) odds are the user id is better to be used anyway (more reliable, faster for most things)

Let us know what you're trying to use it for and maybe we can provide you with better information on how to do it.
I want to display the names of the persons who entered info. and so far with me passing it thru a text field it works fine for the query
Title: Re: Get a user name from simple portal page.
Post by: Arantor on June 17, 2012, 08:17:37 PM
What query, exactly? (This is important. It has security implications.)
Title: Re: Get a user name from simple portal page.
Post by: andreblue2u on June 17, 2012, 08:50:00 PM
Quote from: Arantor on June 17, 2012, 08:17:37 PM
What query, exactly? (This is important. It has security implications.)
This one:

$AddQuery = "INSERT INTO `melon`.`".$SQLTable."` (`name`, `adder`, `reason`) VALUES ('".mysql_real_escape_string($_POST['McUserToAdd'])."', '".mysql_real_escape_string($_POST['McUserAdder'])."', '".mysql_real_escape_string($Reason)."');";

Title: Re: Get a user name from simple portal page.
Post by: Arantor on June 17, 2012, 09:04:07 PM
If it's inside either the forum or SSI, $context['user']['name'] will contain the user's display name (not their internal name)

You don't need to push it through $_POST, just grab it from that variable.
Title: Re: Get a user name from simple portal page.
Post by: andreblue2u on June 18, 2012, 01:37:08 AM
When i use it, nothing its self comes up.
Title: Re: Get a user name from simple portal page.
Post by: Arantor on June 18, 2012, 09:48:19 AM
Did you declare $context as global before trying to use it?

You're asking me to help you and I have no information to do so.
Title: Re: Get a user name from simple portal page.
Post by: andreblue2u on June 18, 2012, 11:19:21 AM

<?php
//require_once('/home/andreblu/public_html/melon/forum/SSI.php');
//Define as needed.
$SQLHost 'localhost';
$SQLDatabase '';
$SQLUser '';
$SQLPassword '';
$SQLTable 'whitelist';
$col "";
$User =  $context['user']['name'];
//Connect here
mysql_connect($SQLHost$SQLUser$SQLPassword);
mysql_select_db($SQLDatabase);
//Function for current url
function my_url(){

    
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

    return 
$url;

}
//Check if set and isset($_POST['McUserAdder'])
if(isset($_POST['McUserToAdd'])  and isset($_POST['Submit'])){
//Assagin some thing if none
if(empty($_POST['Reason'])){
$Reason 'Reason';
}else{
$Reason $_POST['Reason'];
}
//Meh
echo 'Trying to add player : '.$_POST['McUserToAdd'].'<br />';
//Add to here
$AddQuery "INSERT INTO `".$SQLDatabase."`.`".$SQLTable."` (`name`, `adder`, `reason`) VALUES ('".mysql_real_escape_string($_POST['McUserToAdd'])."', '".mysql_real_escape_string($User)."', '".mysql_real_escape_string($Reason)."');";
//Errors
if(mysql_query($AddQuery)){
echo '<br />User Added<br />';
} else {
print(mysql_error());
echo '<br />';
echo $AddQuery;
echo '<br />';
}
}
echo 
my_url();
//Form for name and their name. Also reason and such
//Remove will be a 2nd page to reducce mistakes

?>

<form action="<?echo my_url(); ?>" method="POST">
<p>WhiteList: <input type="text" name="McUserToAdd" /></p>
<p>Your Name: <input type="text" name="McUserAdder" /></p>   
<p>Reason: <input type="text" name="Reason" /></p> 
<input type="hidden" name="Submit" />
<p><input type="submit" value="Submit" /></p>
</form>
<?
//Listing
//Query
$query = "SELECT * FROM `" . $SQLTable . "`;";
$Result = mysql_query($query);

echo "<table width=70% border=1 cellpadding=5 cellspacing=0>";

echo "<tr style=\"font-weight:bold\">
<td>Name</td>
<td>Added By</td>
<td>Reason</td>
</tr>";
$col = "";
while($row = mysql_fetch_assoc($Result)){

if($col == "#00CC33"){
$col = "#00FF33";
}else{
$col = "#00CC33";
}
echo "<tr bgcolor=$col>";

echo "<td>".htmlentities($row['name'])."</td>";
echo "<td>".htmlentities($row['adder'])."</td>";
echo "<td>".htmlentities($row['reason'])."</td>";

echo "</tr>";
}

?>

Thats how i have it set

EDIT: Got it working. Thanks foe helping me with this.
Title: Re: Get a user name from simple portal page.
Post by: Arantor on June 18, 2012, 11:44:24 AM
Yes, commenting out SSI will break it >_<