User Login on Website
-
I currently have a website developed in WordPress that is not a blog. I want to create a member exclusive area, with a simple login script in php. I was wondering if there would be a way to exploit the WordPress user system in doing this.
If I create a user, will I be able to make a login on the website that can be accessed with that user name and password? If so how?
-
I am also interested in adding this feature to my sites. If you find anything, please let me know.
Thanks
So far, i have been able to create a user from outside the wordpress directory, on my own webapp. However having trouble displaying a logged in user, ill post that in another thread. THis is how I did account creation, however you’ll have to modify a bunch of it and I did not include functions from other files. Hope it helps point you in the right direction.
You’ll need to create 2 pages for account creation.
register.php – with a submission form, ive used JQuery for Ajax, so it may look foreign to some.
<? include "../common/header.php"; ?> <div class="fitted_container"> <form id="form_register"> <table align=center> <tr><td><p>Username</p></td></tr> <tr><td><input type="text" id="username"></td></tr> <tr><td><p>Email</p></td></tr> <tr><td><input type="text" id="email"></td></tr> <tr><td><input type="button" onmousedown="javascript:register()" value="Register"></td></tr> </table> </form> <p id="response_text"></p> </div> <script language="javascript"> function register() { // TODO: Client Side Validation // Appears Okay, Submit var form = $("#form_register"); $("#response_text").load("register_act.php", { username: $("#username").val(), email: $("#email").val() }, function() { } ); } </script> <? include "../common/footer.php"; ?>
register_act.php – the form elements are passed to this, this is the tough part.
I basically went through the process WordPress used to do this, so alot of the code comes from there. This will not work with a copy and paste, you need to use your own connection handler to the database and define some of the functions that I modified from wordpress.
Within register_act.php<? /** Make sure that the WordPress bootstrap has ran before continuing. */ // you'll most likely have to change this to your own blog directory. require('../blog/wp-load.php'); $user_email = $_POST["email"]; $user_login = $_POST["username"]; $nicename = strtolower($user_login); $user_pass = wp_generate_password(); $hashed_user_pass = wp_hash_password($user_pass); // this creates a connection to my database $conn = new GTConnection("wrdp1"); /* Server side Validation - if there is an error, printing will show the error on the page. I was using ajax to run this script, and when there is an error, it gets put into a container... So the user never leaves the register.php page upon error.*/ if ( $user_login == '' ) { print '<strong>ERROR</strong>: Please enter a username.'; return; } elseif ( !gt_validate_username( $user_login ) ) { print '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.'; return; } elseif ( gt_username_exists( $user_login ) ) { print '<strong>ERROR</strong>: This username is already registered, please choose another one.'; return; } // Check the e-mail address if ($user_email == '') { print '<strong>ERROR</strong>: Please type your e-mail address.'; return; } elseif ( !is_email( $user_email ) ) { print '<strong>ERROR</strong>: The email address isn’t correct.'; return; } elseif ( gt_email_exists( $user_email ) ) { print '<strong>ERROR</strong>: This email is already registered, please choose another one.'; return; } $result = mysql_query(" INSERT INTO <code>wrdp1</code>.<code>wp_users</code> ( <code>ID</code>, <code>user_login</code>, <code>user_pass</code>, <code>user_nicename</code>, <code>user_email</code>, <code>display_name</code>, <code>user_registered</code> ) VALUES ( NULL, '$user_login', '$hashed_user_pass', '$nicename', '$user_email', '$user_login', now() ) "); if (!$result) { die('Invalid query: ' . mysql_error()); } else { $user_id = mysql_insert_id($conn->link()); if ( !$user_id ) { print '<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:[email protected]">webmaster</a> !'; //$errors->add('registerfail', sprintf(__('<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !'), get_option('admin_email'))); return; } $user = new WP_User($user_id); $user->set_role(get_option('default_role')); //$user->getUserById($user_id); //$user_login = stripslashes($user->user_login()); //$user_email = stripslashes($user->user_email()); $message = 'New user registration at MySite ' . "\r\n\r\n"; $message .= 'Username: ' . $user_login . "\r\n\r\n"; $message .= 'E-mail: ' . $user_email . "\r\n"; $message .= 'Password: ' . $user_pass . "\r\n"; wp_mail('[email protected]', 'New User Registration @ MySite', $message); if ( empty($user_pass) ) { print "password is blank"; return; } $message = 'Username: ' . $user_login . "\r\n"; $message .= 'Password: ' . $user_pass . "\r\n"; $message .= "https://www.mysite.ca/login.php" . "\r\n"; wp_mail($user_email, '[MySite] Your username and password', $message); } $conn->close(); ?>
- The topic ‘User Login on Website’ is closed to new replies.