So I'm trying to make a form that creates a user and everything works... except the part that actually saves the user to the database...
The thing about it is that the user_save function isn't even returning a false or null value. It is just stopping the script altogether. I even tried echoing some values after it and they are inaccessible.
After hours of echoing out values, I have narrowed it down to the execute() method of the query object.
Here is the code:
$account = array(
'name' => $user_email,
'pass' => $user_password,
'mail' => $uer_email,
'theme' => '',
'signature' => '',
'signature_format' => NULL,
'created' => 0,
'access' => 0,
'login' => 0,
'status' => 1,
'timezone' => NULL,
'language' => '',
'picture' => 0,
'init' => 'email address',
'data' => NULL
);
if (user_save(NULL, $account)) {
user_authenticate($user_email, $user_password);
} else {
echo "Something looks broken.";
}
I have checked the Apache / PHP / MySQL logs and there are absolutely no notable errors. I thought that there might be a problem with how my data is set up in the array. Before this, I defined only the fields that HAD to be defined but it made no difference.
What do you think is wrong?
The line:
'init' => 'email address',
should be:
'init' => $uer_email,
The following code is all you need to create a new user, try starting from this and then adding the other values that you need.
$account = array(
'name' => $user_email,
'pass' => $user_password,
'mail' => $user_email,
'status' => 1,
'init' => $user_email
);
user_save(null, $account);
Related
I made a registration form but after the registration I am unable to login into the newly created account. Wordpress says wrong password for this email, what am I doing wrong?
Here is the code:
$userdata = array(
'user_pass' => $password,
'user_nicename' => $company_clean,
'user_email' => $email,
'user_login' => $company_clean,
'display_name' => $company_clean,
'nickname' => $company_clean,
'first_name' => $name,
'last_name' => $last_name,
'rich_editing' => false,
'syntax_highlighting' => false,
'comment_shortcuts' => false,
'user_registered' => date('Y-m-d H:i:s'),
'show_admin_bar_front' => false,
'role' => 'customer',
);
$user_id = wp_insert_user( wp_slash($userdata) );
add_user_meta($user_id, 'company', $company, false);
add_user_meta($user_id, 'primary', '1', false);
add_user_meta($user_id, 'miles', 0, false);
add_user_meta($user_id, 'phone', $phone, false);
if(!term_exists($company, 'customers')){
wp_insert_term(
$company,
'customers'
);
}
wp_redirect(get_home_url() . '/login');
I already tried with and without wp_slash() but the result is the same.
Solved, before registering the new user I was encrypting the password with md5() and then I was using that to register it.
wp_insert_user() does its own encryption so it was a double encrypted password, hence it was impossible for WP to match the entered value, with the messy one stored in the db.
Anyway searching on the web I found out that it is better to use wp_hash_password() rather than md5().
I do some database transaction and the set $new_user = TRUE like this
Model Code:
$data_insert = array
(
'username' => $username_post,
'password'=>$username_post,
);
$this->db->insert('Tenant', $data_insert);
$new_tenant_id = $this->db->insert_id();
//CREATE TABLE FOR THAT DISTRIBUTOR
$this->dbforge->add_field(
array(
'id' => array(
'type' => 'INT',
'constraint' => 10,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'site_key' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'display_name' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'null' => TRUE
),
'ext' => array(
'type' => 'VARCHAR',
'constraint' => '50',
'null' => TRUE
),
'auth_user' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'null' => TRUE
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => '128',
'null' => TRUE,
),
'base_ini_id' => array(
'type' => 'VARCHAR',
'constraint' => '50',
'null' => TRUE
),
'md_user' => array(
'type' => 'VARCHAR',
'constraint' => '128',
'null' => TRUE
),
'uc_user' => array(
'type' => 'VARCHAR',
'constraint' => '50',
'null' => TRUE
),
'uc_password' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'null' => TRUE
),
'comments' => array(
'type' => 'VARCHAR',
'constraint' => '200',
'null' => TRUE
),
'custom_ini_filename' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'null' => TRUE
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'null' => TRUE
),
));
$this->dbforge->add_key('id', TRUE);
if (!$this->db->table_exists('table_name'))
{
$this->dbforge->create_table($usertable);
}
//TABLE CREATED NOW ADD SOME DATA
$insert_data = array
(
'site_key' =>$site_post,
'tenant_id'=>$new_tenant_id
);
//TENANT CREATED AND THE SITE BY HIM IS ADDED TO DATABASE
$query = $this->db->insert('MLCSites',$insert_data);
$new_user = TRUE;
Now if i have the user in database then $validate is set to TRUE if not i check if $new_user == TRUE then I set loggedin = TRUE in my session like this:
if(!empty($validate))
{
if ($validate == TRUE)
{
// Log in user
$data = array(
'site' => $site->site_post,
'id' =>$tenant_id,
'username'=>$username_post,
'user_table'=>$usertable,
'nec_distributor'=>TRUE,
'loggedin' => TRUE,
);
$this->session->set_userdata($data);
return TRUE;
}
elseif ($new_user == TRUE)
{
// Log in user
$data = array(
'site' => $site->site_post,
'id' =>$new_tenant_id,
'username'=>$username_post,
'user_table'=>$usertable,
'nec_distributor'=>TRUE,
'loggedin' => TRUE,
);
$this->session->set_userdata($data);
return TRUE;
}
return FALSE;
}
Now in my controller i Check like this:
$dashboard ='customer/dashboard';
$rules = $this->distributor_m->rules;
$this->form_validation->set_rules($rules);
if ($this->distributor_m->login() == TRUE)
{
var_dump($this->session->all_userdata());
$this->distributor_m->loggedin() == FALSE || redirect($dashboard);
redirect($dashboard);
}
else
{
$this->session->set_flashdata('error', 'That email/password combination does not exist');
//redirect('secure/login', 'refresh');
}
But when i submit username and key, all the database transaction are done as per the code successfully. But there is nothing in my session, if I resend the form information then i see the SESSION data. So where I am going wrong?
There is no good and bad you can do anything you want. But Model we used to only interact with Database. So Controller is the one handle all the sites.
So its better if you add the session in controller. Its helpful in some ways too.
Ex: if you set flashdata you have to redirect the page. So in model you can't archive it. But if you do same on controller you are able to redirect where ever you want.
$this->session->set_flashdata('item', 'value');
redirect('controller/name');
1) Do not store all that information in the session. Make a long randomized string to use as a token. Store that token in the sesssion. Use that token to retrieve the user details from a database. Note that I'm not talking about saving the codeigniter session itself to a database. Use session files. Make your own user table to hold the user name, site, etc etc. Of course put in some other details like sign up date, last activity date, etc.
2) Set the session first. Before everything else you are doing. Especially before doing something as major as creating a new database table :-)
3) The session is set. Next page or whatever the user clicks on something that starts the table creation. Before you create the table, validate that the session is valid.
4) Models can be for many things not just interacting with a database. There are two schools of design in terms of controllers - wise people who advocate "thin controllers" that are clean, easy to maintain, and much less prone to errors. And those other people who stuff their controllers full of code and thus have "fat controllers". Not going to say which is better - so I will just imply it :-)
This code works fine .Have a glimpse.
In this case I have two kind of user: admin and customer
public function index()
{
if(($this->session->userdata('logged_in'))){
redirect('/customer/dashboard/', 'refresh');
}
if(isset($_POST['username']) && isset($_POST['password'])){
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$username = $_POST['username'] ;
$password = md5($_POST['password']);
if ($this->form_validation->run() == FALSE)
{
echo "not success";
}
else
{
$this->load->model('Book_upload');
$user_details = $this->Book_upload->validate_user_login($username,$password);
if(count($user_details)==0){
echo "Invalid Login";
} else {
$newdata = array(
'userid' => $user_details[0]['AdminId'],
'username' => $user_details[0]['UserName'],
'role' => $user_details[0]['Role'],
'fullname' => $user_details[0]['FullName'],
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
$session_flag = $this->session->all_userdata();
if($session_flag['role'] == 'customer')
redirect('/customer/home/', 'refresh');
if($session_flag['role'] == 'admin')
redirect('/admin/dashboard/', 'refresh');
}
}
}
$this->load->view('customer/login');
}
how to see what is the actual problem if we enter the data through a form in a textbox to insert in the database table, but there is not displayed any error
after commenting on the error reporting(0) also
code for database:
<?php
include('connection_temp.inc.php');
include('functions.inc.php');
?>
<?php
sm_registerglobal('MEMBER_ACCOUNT_INFO_ID', 'FIRST_NAME', 'LAST_NAME', 'EMAIL_ID', 'PHONE_NO', 'STATES_ID', 'CITIES_ID', 'PASS', 'LOCALITY');
$tbl_registrations = array(
'FIRST_NAME' => $FIRST_NAME,
'LAST_NAME' => $LAST_NAME,
'STATES_ID' => $STATES_ID,
'CITIES_ID' => $CITIES_ID,
'EMAIL_ID' => $EMAIL_ID,
'PHONE_NO' => $PHONE_NO,
'PASS' => $PASS,
'LOCALITY' => $LOCALITY,
);
/*$tbl_member_account_info = array(
'FIRST_NAME' => 'ssk',
'LAST_NAME' => 'sss',
'STATES_ID' => '6',
'CITIES_ID' => '12',
'EMAIL_ID' => 'asd#w.com',
'PHONE_NO' => '3324234',
'PASS' => '123',
'LOCALITY' => 'abad',
);*/
//$registrations_id = insert($con, "tbl_registrations", $tbl_registrations);
$member_account_info_id = insert($con, "tbl_member_account_info", $tbl_member_account_info);
//echo $member_account_info_id;
?>
Do not add comma with last element in array. try below code
<?php
include('connection_temp.inc.php');
include('functions.inc.php');
?>
<?php
sm_registerglobal('MEMBER_ACCOUNT_INFO_ID', 'FIRST_NAME', 'LAST_NAME', 'EMAIL_ID', 'PHONE_NO', 'STATES_ID', 'CITIES_ID', 'PASS', 'LOCALITY');
$tbl_registrations = array(
'FIRST_NAME' => $FIRST_NAME,
'LAST_NAME' => $LAST_NAME,
'STATES_ID' => $STATES_ID,
'CITIES_ID' => $CITIES_ID,
'EMAIL_ID' => $EMAIL_ID,
'PHONE_NO' => $PHONE_NO,
'PASS' => $PASS,
'LOCALITY' => $LOCALITY
);
/*$tbl_member_account_info = array(
'FIRST_NAME' => 'ssk',
'LAST_NAME' => 'sss',
'STATES_ID' => '6',
'CITIES_ID' => '12',
'EMAIL_ID' => 'asd#w.com',
'PHONE_NO' => '3324234',
'PASS' => '123',
'LOCALITY' => 'abad',
);*/
//$registrations_id = insert($con, "tbl_registrations", $tbl_registrations);
$member_account_info_id = insert($con, "tbl_member_account_info", $tbl_member_account_info);
//echo $member_account_info_id;
?>
Error reporting must be set as error_reporting(E_ALL) to see all the errors. if you set error_reporting(0) no error will be displayed as it is used to hide all the error generally in production mode.
And remove the comma in the end as you are passing the array that comma should not be there, and check the $con variable once whether you have properly declared the database connection or not, if the problem is still not resolved check the error by setting the error_reporting(E_ALL) and debug and check whether any error appears in the console.
I have an application that I work in that is experiencing a problem where randomly a blank page loads.
I've found that the beforeFilter function fires but it never goes into the action of the controller that is being called at that time. I have also found that the session when this happens can not be found. session_status() returns PHP_SESSION_NONE. When the page is reloaded php can magically find the session again and the page loads normally.
Any help is appreciated. If you need/would like more information just let me know.
EDIT:
AppController beforeFilter
public function beforeFilter() {
parent::beforeFilter();
// Log all access to applictaion
$this->AccessLog->logPageAccess($this->request, $this->Session);
// Read the app's desired datetime display and set as view variable for TimeHelper use.
if(Configure::check('Datetime.dateDisplayFormat')) {
$timeFormat = Configure::read('Datetime.dateDisplayFormat');
$this->set(compact('timeFormat'));
}
}
Session Auth.User when session found
array(
'password' => '*****',
'id' => '44',
'role_id' => '5',
'username' => 'user',
'password_token' => null,
'email' => 'user#example.com',
'email_verified' => true,
'email_token' => null,
'email_token_expires' => null,
'active' => true,
'is_login_locked' => false,
'last_login' => '2015-02-04 16:41:47',
'last_action' => null,
'created' => '2014-07-07 12:45:46',
'modified' => '2015-02-04 16:41:47',
'created_by' => '19',
'modified_by' => '44',
'deleted' => false,
'deleted_date' => null,
'account_locked' => false,
'lu_theme_id' => '4',
'first_name' => 'Joe',
'last_name' => 'Bloggs',
'Role' => array(
'id' => '1',
'name' => 'User',
'is_admin' => false
)
)
$_SESSION when not found
array()
Are you running an array of servers under a load balancer? I've seen session weirdness in that type of environment before now.
Last weekend I was trying to troubleshoot a bug on a website where the Session was not being preserved in IE - today I went to do further work on the site on my laptop, and I could no longer log in -invariably I have done something incredibly stupid.
I'm using xampp on a Windows laptop, and working on localhost, and this occurs in all browsers. I am not very experienced with troubleshooting these kinds of problems - I have been able to ascertain the following:
The user is able to login (Auth->login() successfully logs the user in), the issue is the Session is gone when they are redirected
I can see the Sessions being written in my /tmp/ dir containing (what looks to be) the correct data
I can create my own stupid cookies and their values persist
No other cookies exist for the site
So, it would appear to me that the Session cookie is not being set, but I have run out of ideas as to why this might be occurring. I haven't changed any cookie related browser settings (outside of enabling cookies in IE), and I have double checked my Chrome cookie settings. I have also, as I mentioned, written some junk cookies in AppController, and I can see them created, and their data persists.
If I call $_SESSION after login(), everything looks great, but if I print $_SESSION before logging in, it's empty.
I am quite sure I have managed to do something retarded, but I have run out of ideas as to what it might be. I have restored my /app/core.php to be the Cake defaults:
Configure::write('Session', array(
'defaults' => 'php'
));
My login() function looks essentially as follows:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again.'));
}
Auth settings in AppController:
class AppController extends Controller {
public $components = array(
'Session',
'Cookie',
'Acl',
'Email',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'password')
)),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
),
);
And example output from printing $this->Auth->user(), $_SESSION before the redirect in login():
\app\Controller\UsersController.php (line 203)
array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
\app\Controller\UsersController.php (line 204)
array(
'Config' => array(
'userAgent' => '4af162a3a94462226b6e93c6806203aa',
'time' => (int) 1417317929,
'countdown' => (int) 10,
'language' => 'eng'
),
'Auth' => array(
'User' => array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
)
)
Last created session file:
Config|a:4:{s:9:"userAgent";s:32:"4af162a3a94462226b6e93c6806203aa";s:4:"time";i:1417317929;s:9:"countdown";i:10;s:8:"language";s:3:"eng";}Auth|a:1:{s:4:"User";a:12:{s:2:"id";s:2:"10";s:4:"name";s:5:"super";s:9:"is_active";s:1:"1";s:5:"email";s:14:"super#test.com";s:8:"group_id";s:1:"3";s:7:"address";s:1:"3";s:5:"phone";s:10:"xxxxx";s:8:"category";s:1:"P";s:16:"communication_in";s:1:"E";s:7:"created";s:19:"2014-11-29 16:27:19";s:8:"modified";s:19:"2014-11-29 16:27:19";s:5:"Group";a:4:{s:2:"id";s:1:"3";s:4:"name";s:14:"Administrators";s:7:"created";s:19:"2014-11-16 21:01:35";s:8:"modified";s:19:"2014-11-16 21:01:35";}}}
Facepalm of the day:
Many hours later, I finally thought to check phpinfo(), and of course, the session.cookie-domain was set to the remote site. I suppose at some point last week I edited the wrong PHP ini file.