Drupal 7 comes with the built-in user registration form (user/register). I use this form for new users to register. Which is quite obvious. Now the problem is, and I find it hard to believe that it isn't there, I need some validating.
When a new user completes the form and hits submit, the account is being created. Good.
But: when a user completes the form and hits submit, but the email address or username is already in use, the pages just reloads and the user is not being created, which is good, but theres no warning on what he has to change in the form what so ever.
I find it strange that this is not standard.
Could anyone provide me some help? I really have no clue...
Use can achieve that with many approaches.
Here's an example using hook_form_alter()
function [YOUR_MODULE]_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "user_register_form" || $form_id == "user_profile_form")
{
$form['#validate'][] = '_your_custom_validation_callback';
}
}
function _your_custom_validation_callback(&$form_state)
{
// use your validation code...
}
Hope this works... Muhammad.
Related
I am trying to create a module where the users creates his account and on submit, i get his information and insert them in a second database too.
I mean that he will exist in both databases and in Drupals user table and in user table of the other database.
How can i get his information and insert them to a custom database?
I am totally new to Drupal development.
Thank you in advance for any help or advice.
You will need to implement hook_form_alter() and use the following code:
function [YOUR_MODULE]_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "user_register_form")
{
$form['#submit'][] = "your_custom_submit_callback";
}
}
Then create the custom submit callback to manipulate the submitted values the way you like:
function your_custom_submit_callback($form, &$form_state)
{
// your code goes here...
}
Hope this works... Muhammad.
I have a relatively simple class which deletes a post:
function delete_post($postid, $reason){
//Stuff to delete post
$this->delete_response = 'Thanks, your course has been removed.';
}
This function is called at the top of a page with a form on. If the form is submitted, the same page checks the POST[] and carries out the function, like so:
if(!empty($_POST['removecourse'])){
$courseManager->delete_post($_POST['courseid'], $_POST['cancel-reason']);
echo $courseManager->delete_response;
};
So my problem is... when I refresh the page, the message keeps displaying. I know this is because I am re-submitting the form, and because there is no such P/R/G pattern going on, but as i am new to OOP, im wondering if im doing this the right way, or if anyone could suggest a way similar to PRG or something?
Add an if that test if somthing changed, like mysql_affected_rows
function delete_post($postid, $reason)
{
//Stuff to delete post
if(mysql_affected_rows())
{
$this->delete_response = 'Thanks, your course has been removed.';
}
}
I am trying to setup drupal 7 so that was when users signs-in for the first time they are presented with a terms of use (I have tried the terms of use module but it doesn't suit my needs as it only seems to work when the user creates the account and in our instance all accounts are created by the admin), and as is standard the user has to agree to these terms inorder to use there account.
For the second part of this once the user logs and agrees with the terms they have to be presented with the edit profile form to make necessarily updates.
Does anyone know of anyway to set this up (preferably without alot of development as this project is short on time)??? Any help is greatly appreciated.
You know that user has not logged in yet if their "access" and "login" values are 0. So it should be enough to check them and if they are 0, redirect user to your T&Cs. In theory at least.
The easiest solution (that comes to my mind right now anyway) would be something like this:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_login') {
$form['#submit'][] = 'MYMODULE_user_login_submit';
}
}
function MYMODULE_user_login_submit($form, &$form_state) {
$user = user_load($form_state['uid']);
if ($user->access == 0) {
$form_state['redirect'] = 'your-terms-and-conditions-form-url';
}
}
hook into "user_login" form and add own submit function
in own submit function check both mentioned values, and if they equal 0, redirect user to your T&Cs
The thing is that our submit gets called after main user_login_submit(), where (well, not exactly there) "login" value already gets updated - therefore we can check "access" field.
The rest sounds easy enough - page with T&Cs and agree form, redirecting to user profile edit afterwards, easy-peasy...
I have a Drupal website and I want to show different welcome pages, depending on what my users enter as profile fields. I can't use the global $user variable, because users are not automatically logged in (They have to very their email address before they can log in).
Where can I add code to set the redirect?
I've tried with $form['#redirect'] and $form_state['redirect'] in the form validator, but that didn't work.
You can use logintobogan for inspiration:
#implementation of hook_user
mymodule_user($op) {
if ($op == 'login') {
$_REQUEST['destination'] = '/user/will/be/redirected/here'
}
}
The important part is to make sure, that by the time the final drupal_goto() is called in user.module, you have set your $_REQUEST['destination'].
A few things to note:
Logintoboggan has a lot of code to deal with all sorts of edge-cases, such as redirecting out/to https. You can ignore these, if your case is simple.
Your module must be called after user.module and probably after other modules implementing hook_user, for they might change this global too. Very ugly, but the way this works in Drupal.
Do not -ever- issue drupal_goto() in any hook. Especially not hook_user, or hook_form_alter. drupal_goto will prohibit other hooks from being called; breaking functionality at the least, but often corrupting your database.
Do not issue drupal_goto() in form_alter callbacks such as "_submit", this might break many other modules and might even corrupt your database.
Similar to Berke's answer, but it seems like you just want this to be a one time thing. For that, you can check for the $account->access property to check their last login. If it is 0, then they are logging in for the first time.
This should work fine for email or no email validation.
<?php
/**
* Implements hook_user().
*/
function mymodule_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'login':
// execute this if they have never accessed the site before
if ($account->access == 0) {
// run conditional logic based on profile fields
// to set destination here
$_REQUEST['destination'] = 'path/to/welcome-page';
}
break;
}
}
?>
I suggest you use the Login Destionation module or you can use the Rules module redirect action which is maybe to robust for your purpose.
Just in case you don't want to write your own custom module :-)
I need a login to let 10 students to view educational material. Simple is good.
Perhaps it just redirects to a page if student logs in correctly. Can you send me a link or example, or best tutorial?
I was told JavaScript alone doesn't work, so the next simplest thing is preferred.
If there's an example where I don't have to rename all of my pages 'php', that would be better.
Thanks,
I used this when I was learning to do secure logon using PHP.
http://www.devshed.com/c/a/PHP/Creating-a-Secure-PHP-Login-Script/1/
Found it quite helpful.
Simple...
Create a file called functions and insert the following:
session_start();
$_GLOBALS['users'] = array(
//'username' => 'password'
'robert' => 'my_pass'
);
function isAuthed()
{
if(empty($_SESSION['logged_in']))
{
if(!empty($_REQUEST['username']) || !empty($_REQUEST['password']))
{
if(isset($_GLOBALS['users']) && is_array($_GLOBALS['users']))
{
if(isset($_GLOBALS['users'][$_REQUEST['username']]) && $_GLOBALS['users'][$_REQUEST['username']] === $_REQUEST['password'])
{
$_SESSION['logged_in'] = true;
return true;
}
}
}
}else
{
return true;
}
return false;
}
and then in your secured pages just do:
if(!isAuthed())
{
die("You're not authorized to see this page");
}
and on your login page just create a form that sends the username, password to the an area of your site that your authorizing
Note: This is not copy and past'able code, this is for example purposes only.
You could possibly "do" it with JavaScript if you did some kind of AJAX function which called a php page, and then returned your value. This could work, and it's how a lot of sites do their logins actually. So, your client wouldn't have to rename their site, and you could just set up an array of logins on the php page.
This would NOT be secure at all, but it would work just fine.
I guess you would do something like this (I'm going to use jQuery because it's easier to do Ajax with it. It's really easy to use, and if you're going to learn Javascript, it's probably better nowadays to know the basics and then use a framework library like jQuery)
$(document).ready(function(){
$("#NAME-OF-SUBMIT-BUTTON").submit(function(){
var username = $(this).find("#username");
var password = $(this).find("#password");
$("NAME-OF-DIV-FOR-RETURN").load('login.php', {['parameters']:,[username,password]},function(responseText){
if(responseText == 'SUCCESSFUL-RESPONSE-TEXT'){
$("#NAME-OF-FORM").html("Login Successful");
}
});
});
});
and of course you're going to want to set a session variable or cookie or something on your php page to indicate the user has logged in. Again, this is not very secure, but it's what I would do if it were like a homework assignment or just SUPER temporary. Of course, I would suggest making hard-coded usernames and passwords in an array on your original page in PHP with a postback to itself if you were going to go that temporary. Using Javascript and Ajax for this just seems like a bit much.
But, you requested it!
It depends on exactly what you're trying to do. If you want a pure-JS login system, you could do something fairly simple like XOR'ing a redirect page with a password, storing that in the page and then XOR'ing it again when they type in a password.
If you want an actual login-system, you need a back-end running some server (perhaps Node.js if you're trying to learn JavaScript), some type of database (e.g. MySQL), users stored in that database.
The front-end javascript might be responsible for validating the login via ajax. Using jQuery, something like:
function validateLogin(user, pass, successCallback, errorCallback){
$.get('/login', {user: user, pass:pass}, function(data){
if(data.status == 'success'){
successCallback();
}else{
errorCallback();
}
}
}