News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Register a user from a small PHP file hidden somewhere.

Started by madturnip, March 09, 2012, 08:24:10 PM

Previous topic - Next topic

madturnip

I am looking for a way to register a user and give him/her a group, username, password, email, and activate status.  However, I don't want to have to login to SMF to do that.  I am building a small program, which I could just link to the database, however I want the registration to all take place on the site itself.   The file would be hidden somewhere that only I would know about.

Just a small PHP script that would allow text boxes, maybe a drop down for groups available, and then a submit button.  Voila!  Done....


Is this difficult or something rather simple...

madturnip

#1
I figured in the PHP file the credentials of the ADMIN could be placed in the file using a variable so that it can authenticate automatically and create the new user when the submit button is pressed.  I had help from someone who helped me create a POST version of this and it worked quite well.   I would think being able to create a user would be possible with ease too.

CODE FOR POST:

<?php

// Using SSI?
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
require_once(dirname(__FILE__) . '/SSI.php');
elseif (!
defined('SMF'))
die('<strong>Error:</strong> Cannot install - please make sure that this file in the same directory as SMF\'s SSI.php file.');

// Call our template
templateFunc();

function 
templateFunc()
{

// Globalize everything we'll need...
global $sourcedir$scripturl$fileName$possibleBoards$errors$smcFunc;

// Our Logic
loadLogic();

// Now the HTML
echo '<form action="'$fileName'" method="post">
<!-- NAME -->
<br /><br />
<label for="name">
<span style="font-weight: bold;'
, isset($errors['name']) ? ' color: #ff0000;' '''">*Name:</span>
</label>
<input type="text" maxlength="255" id="name" name="name" size="40"'
, isset($_POST['name']) && !empty($_POST['name']) ? ' value="' $_POST['name'] . '"' ''' />
<!-- EMAIL -->
<br /><br />
<label for="email">
<strong>Email:</strong>
</label>
<input type="text" maxlength="255" id="email" name="email" size="40"'
, isset($_POST['email']) && !empty($_POST['email']) ? ' value="' $_POST['email'] . '"' ''' />
<!-- SUBJECT -->
<br /><br />
<label for="subject">
<span style="font-weight: bold;'
, isset($errors['subject']) ? ' color: #ff0000;' '''">*Subject:</span>
</label>
<input type="text" maxlength="255" id="subject" name="subject" size="40"'
, isset($_POST['subject']) && !empty($_POST['subject']) ? ' value="' $_POST['subject'] . '"' ''' />
<!-- BOARD ID -->
<br /><br />
<label for="id_board">
<strong>Board to Post in:</strong>
</label>
<select id="id_board" name="id_board" style="padding: 4px;">'
;
if (!empty($possibleBoards))
{
foreach ($possibleBoards as $key => $value)
{
echo '<option value="' $key '"', isset($_POST['id_board']) && $_POST['id_board'] == $key ' selected="selected"' '''>' $value '&nbsp;&nbsp;</option>';
}
}
else
{
echo '<option value="">No Boards Available</option>';
}
echo '
</select>
<!-- BODY -->
<br /><br />
<label for="body">
<span style="font-weight: bold;'
, isset($errors['body']) ? ' color: #ff0000;' '''">*Body:</span>
</label>
<textarea id="body" name="body" cols="50" rows="5">'
, isset($_POST['body']) && !empty($_POST['body']) ? $_POST['body'] : '''</textarea>
<span class="smalltext" style="margin-left: 70px; display: block;">BBC is Enabled</span>
<br />
<!-- SUBMIT -->
<input type="submit" value="Submit" />
</form>
<span class="smalltext">* = Required Fields</span>'
;

}

function 
loadLogic()
{

// Globalize everything we'll need...
global $sourcedir$scripturl$fileName$smcFunc$possibleBoards$errors;

// Subs-Post for createPost();
require_once($sourcedir '/Subs-Post.php');

// File Name with ".php"
$fileName '.php';

// Allow smileys? true : false;
$smileysEnabled true;

// Default Board
$defaultBoard 1;

// Possible boards?
$possibleBoards = array();
$query $smcFunc['db_query']('''
SELECT id_board, name
FROM {db_prefix}boards'
);
while ($row $smcFunc['db_fetch_assoc']($query))
{
$possibleBoards[$row['id_board']] = $row['name'];
}
$smcFunc['db_free_result']($query);

// Submitting...
if (isset($_POST['name']))
{
// Validation
$errors = array();
if (isset($_POST['name']) && empty($_POST['name']))
$errors['name'] = 1;
if (isset($_POST['subject']) && empty($_POST['subject']))
$errors['subject'] = 1;
if (isset($_POST['body']) && empty($_POST['body']))
$errors['body'] = 1;

if (!in_array(1$errors))
{

// Message Options
$msgOptions = array(
'body' => !empty($_POST['body']) ? $smcFunc['htmlspecialchars']($_POST['body']) : '',
'subject' => !empty($_POST['subject']) ? $smcFunc['htmlspecialchars']($_POST['subject']) : '',
'attachments' => array(),
'icon' => 'xx',
'smileys_enabled' => isset($smileysEnabled) ? $smileysEnabled true,

);

// Topic Options
$topicOptions = array(
'board' => !empty($_POST['id_board']) ? (int) $_POST['id_board'] : $defaultBoard,
'mark_as_read' => false,
'lock_mode' => 0,
'poll' => 0,
'sticky_mode' => 0,
);

// Poster Options
$posterOptions = array(
'email' => !empty($_POST['email']) ? $_POST['email'] : '',
'ip' => $_SERVER['remote_addr'],
'name' => $_POST['name'],
'update_post_count' => true,
'id' => 1,
);

// Create...
createPost($msgOptions$topicOptions$posterOptions);

// Then redirect
redirectexit($scripturl);

}

}

}

?>


madturnip

I am willing to pay for an example.   

As I am not a coder.....

ascaland

Do you want this within the other form, or seperate? I have HTML with a bit of PHP code here,
echo '<form action="" method="post">
    <input type="text" name="username" />
    <input type="password" name="passwrd" />
    <input type="text" name="email" />
    <select name="group">';
   
    foreach ($groups as $key => $value)
        echo '<option value="' . $key . '">' . $value . '</option>';
       
echo '</select>
    <input type="submit" name="submit" value="Submit" />
</form>


And a PHP function you can use,
function registerMember() {
   
    global $smcFunc, $sourcedir, $groups;
   
    $groups = array();
    $request = $smcFunc['db_query'](
        'SELECT id_group, group_name
        FROM {db_prefix}membergroups');
    while ($row = $smcFunc['fetch_assoc']($request))
        groups[$row['id_group']] = $row['group_name'];
   
    // Only do this part if the submit button was pressed
    if (isset($_POST['submit'])) {
        // Make sure we include the registration function
        require_once($sourcedir . '/Subs-Members.php');
       
        // Sanitize some data
        $username = !empty($_POST['user']) ?
            $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES) : '';
        $email = !empty($_POST['email']) ?
            $smcFunc['htmlspecialchars']($_POST['email'], ENT_QUOTES) : '';
        $password = !empty($_POST['passwrd']) ?
            $smcFunc['htmlspecialchars']($_POST['passwrd'], ENT_QUOTES) : '';
       
    $regOptions = array(
    'interface' => 'admin',
    'username' => $username,
    'email' => $email,
    'password' => $password,
    'password_check' => $password,
    'openid' => '',
    'auth_method' => '',
    'check_reserved_name' => true,
    'check_password_strength' => false,
    'check_email_ban' => false,
    'send_welcome_email' => !empty($modSettings['send_welcomeEmail']),
    'require' => 'nothing',
    'extra_register_vars' => array(),
    'theme_vars' => array(),
            'memberGroup' => !empty($_POST['group']) && is_numeric($_POST['group']) ?
                $_POST['group'] : -1,
    );
   
        registerMember($regOptions);
    }
}


If you can get back to my with what you want me to do with the input, I can show you where to put these.

madturnip

#5
I was looking for a seperate php file to use...     So any help or example final product would be awesome....


Thanks a million by the way!

madturnip

#6
The code I posted for the user post above was just an example of a script someone wrote for me a year + ago.   Just to clarify.

I was looking for a seperate PHP script for the registration part.

live627

Quote$regOptions = array(
   'interface' => 'admin',
   'username' => 'user' . $i,
   'email' => 'user' . $i . '@mydomain.com',
   'password' => 'user' . $i,
   'password_check' => 'user' . $i,
   'openid' => !empty($_POST['openid_identifier']) ? $_POST['openid_identifier'] : '',
   'auth_method' => !empty($_POST['authenticate']) ? $_POST['authenticate'] : '',
   'check_reserved_name' => false,
   'check_password_strength' => false,
   'check_email_ban' => false,
   'send_welcome_email' => false,
   'require' => 'nothing',
   'extra_register_vars' => array(),
   'theme_vars' => array(),
);

$memberID = registerMember($regOptions, true);

madturnip

Quote from: Project Evolution on March 10, 2012, 12:56:58 PM
Do you want this within the other form, or seperate? I have HTML with a bit of PHP code here,

If you can get back to my with what you want me to do with the input, I can show you where to put these.

Would like it in a sep. php file not included in the example I gave above for posting.   I read what I wrote above and it was confusing....

Any help is appreciated....

ascaland

#9
Quote from: live627 on March 11, 2012, 01:39:52 PM
Quote$regOptions = array(
   'interface' => 'admin',
   'username' => 'user' . $i,
   'email' => 'user' . $i . '@mydomain.com',
   'password' => 'user' . $i,
   'password_check' => 'user' . $i,
   'openid' => !empty($_POST['openid_identifier']) ? $_POST['openid_identifier'] : '',
   'auth_method' => !empty($_POST['authenticate']) ? $_POST['authenticate'] : '',
   'check_reserved_name' => false,
   'check_password_strength' => false,
   'check_email_ban' => false,
   'send_welcome_email' => false,
   'require' => 'nothing',
   'extra_register_vars' => array(),
   'theme_vars' => array(),
);

$memberID = registerMember($regOptions, true);

That is the exact thing I did, except it's in a function and I'm making sure the data is safe...

Here is the entire file,
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
require_once(dirname(__FILE__) . '/SSI.php');
elseif (!defined('SMF'))
die('<strong>Error:</strong> Please make sure that this file in the same directory as SMF\'s SSI.php file.');

function loadLogic() {
   
    global $smcFunc, $sourcedir, $groups, $modSettings;

    $request = $smcFunc['db_query']('',
        'SELECT id_group, group_name
        FROM {db_prefix}membergroups');
    while ($row = $smcFunc['fetch_assoc']($request))
        $groups[$row['id_group']] = $row['group_name'];
   
    // Only do this part if the submit button was pressed
    if (isset($_POST['submit'])) {
        // Make sure we include the registration function
        require_once($sourcedir . '/Subs-Members.php');
       
        // Sanitize some data
        $username = !empty($_POST['user']) ?
            $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES) : '';
        $email = !empty($_POST['email']) ?
            $smcFunc['htmlspecialchars']($_POST['email'], ENT_QUOTES) : '';
        $password = !empty($_POST['passwrd']) ?
            $smcFunc['htmlspecialchars']($_POST['passwrd'], ENT_QUOTES) : '';
       
    $regOptions = array(
    'interface' => 'admin',
    'username' => $username,
    'email' => $email,
    'password' => $password,
    'password_check' => $password,
    'openid' => '',
    'auth_method' => '',
    'check_reserved_name' => true,
    'check_password_strength' => false,
    'check_email_ban' => false,
    'send_welcome_email' => !empty($modSettings['send_welcomeEmail']),
    'require' => 'nothing',
    'extra_register_vars' => array(),
    'theme_vars' => array(),
            'memberGroup' => !empty($_POST['group']) && is_numeric($_POST['group']) ?
                $_POST['group'] : -1,
    );
   
        registerMember($regOptions);
    }
}

$groups = array();
loadLogic();

echo '<form action="" method="post">
    <input type="text" name="username" />
    <input type="password" name="passwrd" />
    <input type="text" name="email" />
    <select name="group">';
   
    foreach ($groups as $key => $value)
        echo '<option value="' . $key . '">' . $value . '</option>';
       
echo '</select>
    <input type="submit" name="submit" value="Submit" />
</form>';

madturnip

#10
I get an error on this line of code:

groups[$row['id_group']] = $row['group_name'];

Below is the test.php file, but line 11 throws an error.  When I place $ in front of groups it then errors out on line 7 with this message:

Fatal error: Function name must be a string in /home/mcrider/public_html/test.php on line 7

test.php

<?php

function loadLogic() {
    
    global 
$smcFunc$sourcedir$groups;

    
$request $smcFunc['db_query'](
        
'SELECT id_group, group_name
        FROM {db_prefix}membergroups'
);
    while (
$row $smcFunc['fetch_assoc']($request))
        
groups[$row['id_group']] = $row['group_name'];
    
    
// Only do this part if the submit button was pressed
    
if (isset($_POST['submit'])) {
        
// Make sure we include the registration function
        
require_once($sourcedir '/Subs-Members.php');
        
        
// Sanitize some data
        
$username = !empty($_POST['user']) ? 
            
$smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES) : '';
        
$email = !empty($_POST['email']) ? 
            
$smcFunc['htmlspecialchars']($_POST['email'], ENT_QUOTES) : '';
        
$password = !empty($_POST['passwrd']) ? 
            
$smcFunc['htmlspecialchars']($_POST['passwrd'], ENT_QUOTES) : '';
        
    
$regOptions = array(
    
'interface' => 'admin',
    
'username' => $username,
    
'email' => $email,
    
'password' => $password,
    
'password_check' => $password,
    
'openid' => '',
    
'auth_method' => '',
    
'check_reserved_name' => true,
    
'check_password_strength' => false,
    
'check_email_ban' => false,
    
'send_welcome_email' => !empty($modSettings['send_welcomeEmail']),
    
'require' => 'nothing',
    
'extra_register_vars' => array(),
    
'theme_vars' => array(),
            
'memberGroup' => !empty($_POST['group']) && is_numeric($_POST['group']) ?
                
$_POST['group'] : -1,
    
);
    
        
registerMember($regOptions);
    }
}

$groups = array();
loadLogic();

echo 
'<form action="" method="post">
    <input type="text" name="username" />
    <input type="password" name="passwrd" />
    <input type="text" name="email" />
    <select name="group">'
;
    
    foreach (
$groups as $key => $value)
        echo 
'<option value="' $key '">' $value '</option>';
        
echo 
'</select>
    <input type="submit" name="submit" value="Submit" />
 </form>'
;
?>

ascaland

Quote from: madturnip on March 11, 2012, 04:54:48 PM
I get an error on this line of code:

groups[$row['id_group']] = $row['group_name'];

Forgot my $. Try the code again.

madturnip

#12
refer to Reply 10 above...   I did that and it errors out:

Fatal error: Function name must be a string in /home/mcrider/public_html/test.php on line 7

I am sorry man! I am a total noob if I am doing something wrong.  I just added the <?php and ?> to the file and expected it to work.  Maybe it was suppose to have other code with it.  Again, I apologize for being very unskilled when it comes to PHP programing.

ascaland

Quote from: madturnip on March 11, 2012, 05:05:55 PM
refer to post before this one...   I did that and it errors out:

Fatal error: Function name must be a string in /home/mcrider/public_html/test.php on line 7

Right right, I forgot to include SSI.php for the actual script. Updated.

madturnip

Ok it got passed the error....    Now, however, it give the following error:

QuoteWarning: Missing argument 2 for smf_db_query(), called in /home/mcrider/public_html/forum/test.php on line 14 and defined in /home/mcrider/public_html/forum/Sources/Subs-Db-mysql.php on line 221
Database Error
Query was empty
File: /home/mcrider/public_html/forum/test.php
Line: 14
Back

ascaland

*sigh* Writing code from memory isn't as effective as I thought... I'm confident this is it. Try it again.

madturnip

Fatal error: Function name must be a string in /home/mcrider/public_html/forum/test.php on line 15


:(

ascaland

Try this. >:(
<?php
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
 require_once(dirname(__FILE__) . '/SSI.php');
elseif (!
defined('SMF'))
 die('<strong>Error:</strong> Please make sure that this file in the same directory as SMF\'s SSI.php file.');

function 
loadLogic() {
    
    global 
$smcFunc$sourcedir$groups$modSettings;

    
$request $smcFunc['db_query'](''
        
'SELECT id_group, group_name
        FROM {db_prefix}membergroups'
);
    while (
$row $smcFunc['db_fetch_assoc']($request))
        
$groups[$row['id_group']] = $row['group_name'];

    
$smcFunc['db_free_result']($request);
    
    
// Only do this part if the submit button was pressed
    
if (isset($_POST['submit'])) {
        
// Make sure we include the registration function
        
require_once($sourcedir '/Subs-Members.php');
        
        
// Sanitize some data
        
$username = !empty($_POST['user']) ? 
            
$smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES) : '';
        
$email = !empty($_POST['email']) ? 
            
$smcFunc['htmlspecialchars']($_POST['email'], ENT_QUOTES) : '';
        
$password = !empty($_POST['passwrd']) ? 
            
$smcFunc['htmlspecialchars']($_POST['passwrd'], ENT_QUOTES) : '';
        
    
$regOptions = array(
    
'interface' => 'admin',
    
'username' => $username,
    
'email' => $email,
    
'password' => $password,
    
'password_check' => $password,
    
'openid' => '',
    
'auth_method' => '',
    
'check_reserved_name' => true,
    
'check_password_strength' => false,
    
'check_email_ban' => false,
    
'send_welcome_email' => !empty($modSettings['send_welcomeEmail']),
    
'require' => 'nothing',
    
'extra_register_vars' => array(),
    
'theme_vars' => array(),
            
'memberGroup' => !empty($_POST['group']) && is_numeric($_POST['group']) ?
                
$_POST['group'] : -1,
    
);
    
        
registerMember($regOptions);
    }
}

$groups = array();
loadLogic();

echo 
'<form action="" method="post">
    <input type="text" name="username" />
    <input type="password" name="passwrd" />
    <input type="text" name="email" />
    <select name="group">'
;
    
    foreach (
$groups as $key => $value)
        echo 
'<option value="' $key '">' $value '</option>';
        
echo 
'</select>
    <input type="submit" name="submit" value="Submit" />
 </form>'
;
?>

madturnip

had to make a few changes to the username area, but it worked.  Thank you....

:laugh:

ascaland

Quote from: madturnip on March 11, 2012, 06:52:31 PM
had to make a few changes to the username area, but it worked.  Thank you....

:laugh:

I'm curious to know what changes.

Advertisement: