When signing up on my dev site, a bug is here that i cant seem to figure out. I am allowed to pass the validation when signing up. But when i use the submit button to create the account, it seems to not want to log any information into the database. I get the logged in message but that's it, i can just recreate the
same over ad over again.
add_action( 'wp_ajax_nopriv_ajaxregister', 'ajax_register' );
add_action( 'wp_ajax_ajaxregister', 'ajax_register' );
function ajax_register() {
$title = $_POST['titles'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['user_email'];
$user_pass = $_POST['user_pass'];
$confirm_pass = $_POST['confirm_pass'];
$agree_tc = $_POST['agree_tc'];
$nationality = $_POST['nationality'];
$username = $_POST['user_email'];//strtolower($firstname) . '-' . strtolower($lastname);
$phone = '+' . $_POST['country_code'] . $_POST['phone'];
//$address = $_POST['address'];
if ($user_pass != $confirm_pass || !is_email($email) || $agree_tc != 'true') {
echo json_encode(array('loggedin' => false, 'message' => __('There are some errors')));
} else {
$user = array(
'user_login' => $username,
'user_pass' => $user_pass,
'first_name' => $firstname,
'last_name' => $lastname,
'user_email' => $email,
'role' => 'client'
);
$user_id = wp_insert_user($user);
if (is_wp_error($user_id)) {
$error_string = $user_id->get_error_message();
echo json_encode(array('loggedin' => false, 'message' => $error_string));
} else {
update_user_meta($user_id, 'user_num', $phone);
update_user_meta($user_id, 'user_nat', $nationality);
update_user_meta($user_id, 'display_titles',$title);
//update_user_meta($user_id, 'user_address', $address);
$creds = array(
'user_login' => $username,
'user_password' => $user_pass
);
$user_signon = wp_signon($creds, false);
echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));
}
}
die();
}
EDIT .
The markup was perfectly fine, however after going through my repo of changes. The display titles was not getting parsed.
function ajax_register() {
$title = $_POST['titles'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['user_email'];
$confirm_email = $_POST['confirm_email'];
$user_pass = $_POST['user_pass'];
$confirm_pass = $_POST['confirm_pass'];
$agree_tc = $_POST['agree_tc'];
$nationality = $_POST['nationality'];
$username = $_POST['user_email'];
$phone = '+' . $_POST['country_code'] . $_POST['phone'];
if ($user_pass != $confirm_pass || !is_email($email) || !is_numeric($_POST['phone']) || $agree_tc != 'true') {
echo json_encode(array('loggedin' => false, 'message' => __('There are some errors')));
} else {
$user = array(
'user_login' => $username,
'user_pass' => $user_pass,
'first_name' => $firstname,
'last_name' => $lastname,
'user_email' => $email,
'display_titles'=>$title, // THIS WAS MISSING
'role' => 'client'
);
$user_id = wp_insert_user($user);
if (is_wp_error($user_id)) {
$error_string = $user_id->get_error_message();
echo json_encode(array('loggedin' => false, 'message' => $error_string));
} else {
update_user_meta($user_id, 'user_num', $phone);
update_user_meta($user_id, 'user_nat', $nationality);
update_user_meta($user_id, 'display_titles',$title);
//update_user_meta($user_id, 'user_address', $address);
$creds = array(
'user_login' => $username,
'user_password' => $user_pass
);
$user_signon = wp_signon($creds, false);
echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));
}
}
die();
}
Related
I want to change the database, but my coding deletes all its contents.
I use codeigneter 3
my controller:
public function update_() {
$id = $this->input->post('id');
$nama = $this->input->post('nama');
$alamat = $this->input->post('alamat');
$number = $this->input->post('number');
$rekening = $this->input->post('rekening');
$email = $this->input->post('email');
$data = array(
'nama' => $nama,
'alamat' => $alamat,
'number' => $number,
'rekening' => $rekening,
'email' => $email,
);
$where = array (
'id' => $id
);
$this->Modellaptop->update_client($where, $data, 'tb_invoice ');
$this->session->set_flashdata('message',
'<div class="alert alert-success" role="alert">Update success </div>'
);
redirect('Admin/invoice');
}
my model:
public function update_client($where,$data,$table)
{
$this->db->where($where);
$this->db->update($table, $data);
}
maybe you can try like this
in controller
public function update_() {
$id = $this->input->post('id');
$nama = $this->input->post('nama');
$alamat = $this->input->post('alamat');
$number = $this->input->post('number');
$rekening = $this->input->post('rekening');
$email = $this->input->post('email');
$data = [
'nama' => $nama,
'alamat' => $alamat,
'number' => $number,
'rekening' => $rekening,
'email' => $email
];
$where = array (
'id' => $id
);
$this->Modellaptop->update_client($where, $data);
$this->session->set_flashdata('message',
'<div class="alert alert-success" role="alert">Update success </div>'
);
redirect('Admin/invoice');
}
in model
public function update_client($where,$data = [])
{
$this->db->where($where);
$this->db->update('tb_invoice(your table name)', $data);
//dont forget to return
return $this->db->affected_rows();
}
I am working on a school project.
The client-side is in Angular 9, and the server-side is PHP, made with SlimFramework v3.
The login function in my php backend:
Controller.php
$app->post('/api/user/login', function(ServerRequestInterface $request, ResponseInterface $response) use ($app){
$data = $request->getParsedBody();
$email = $data['email_json'];
$password = $data['password_json'];
$freg = new FRegistrazione();
$res = $freg->login($email, $password);
if ($res = true){
$secretKey = "Ma69r3Ga8A";
$issuerClaim = "APACHESERVER";
$audienceClaim = "CINEMA";
$issuedatClaim = time();
$notbeforeClaim = $issuedatClaim + 10;
$expireClaim = $issuedatClaim + 60000;
$token = array(
"iss" => $issuerClaim,
"aud" => $audienceClaim,
"iat" => $issuedatClaim,
"nbf" => $notbeforeClaim,
"exp" => $expireClaim,
"data" => array(
"email" => $email,
"password" => $password));
$jwt = JWT::encode($token, $secretKey);
$response = json_encode(
array(
"res" => "ok",
"message" => "Login eseguito correttamente",
"jwt" => $jwt,
"email" => $email,
"exipireAt" => $expireClaim
));
} else {
$response = json_encode(
array(
"res" => "ko",
"message" => "Credenziali errate"
));}
return $response;
});
Class FRegistrazione.php
public function login($email, $password) {
$islogged = false;
$query = 'SELECT * FROM registrazione WHERE email = ' . '\'' . $email . '\'' . ' AND password = ' . '\'' . $password . '\'';
$res = $this->_connection->query($query);
if ($res->num_rows == 1) {
$islogged = true;} else {$islogged = false;}
This code works correctly if I send a request with Postman.
But when I send a login request from Angular9,
if ($res->num_rows == 1) {
$islogged = true;} else {$islogged = false;}
always returns $islogged=false, even in the case of a valid query on Mysql.
hello Everyone i am new in codeigniter i was try to insert data in database but not work properly.but not insert data in database.I was activate helper and auto load is i was write.
my controller
public function save() {
if($this->input->post('submit')) {
$this->Checkout_model->process();
}
}
my model
function process() {
$name = $this->input->post('name');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$address = $this->input->post('address');
$data=array (
'name' => $name,
'phone' => $phone,
'email' => $email,
'address' => $address
);
$this->db->insert('customers',$data);
}
Use this code In Controller
public function save() {
if($this->input->post('submit')) {
$name = $this->input->post('name');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$address = $this->input->post('address');
$data=array (
'name' => $name,
'phone' => $phone,
'email' => $email,
'address' => $address
);
$this->Checkout_model->process($data);
}
}
In Model
function process($data) {
$this->db->insert('customers',$data);
}
I'm tring to implement an JWT for login with Angular 1.5.8 + CodeIgniter 3.1 on localhost, and I have a problem, console throwing token as 'undefined', from the server still {"error":"Wrong mail or password"}, but data is correct with database. Any suggestions, help, anything.
//-------------------User.php----------------------------
public function login()
{
$email = $this->input->post( 'email' );
$password = $this->input->post( 'password' );
$password = crypt( $password , config_item( 'encryption_key' ) );
$login = $this->User_model->login( $email , $password );
if ( !$login )
{
$output['error'] = 'Wrong mail or password';
}
else
{
$token = $this->jwt->encode( array(
'userId' => $login->id,
'name' => $login->name,
'email' => $login->email,
'role' => $login->role
) , config_item( 'encryption_key' ) );
$output['token'] = $token;
}
echo json_encode( $output );
}
//----------------User_model.php-------------------------
public function login( $email , $password )
{
$this->db->where( 'email' , $email );
$q = $this->db->get( 'users' );
$result = $q->row();
if ( empty( $result ) || $password != $result->password )
{
$output = false;
}
else
{
$output = $result;
}
return $output;
}
//----------------controler-user.js-------------------------
myCtrlsUser.controller( 'userLogin' , [ '$scope', '$http', 'store' , function( $scope, $http, store ){
$scope.user = {};
$scope.formLogin = function ( user ) {
$http.post( 'api/site/user/login/' , {
email : user.email,
password : user.password
}).success( function( data ){
$scope.submit = true;
$scope.error = data.error;
if ( !data.error )
{
store.set( 'token' , data.token );
}
console.log( store.get( 'token' ) );
}).error( function(){
console.log( 'Error #020: Problem with log in' );
});
};
}]);
I tried to make the project regrister using CodeIgniter framework , with verification email after list and it can be used. but there is weakness in my scirpt . when the register if he is using the same user then when submitted will appear statement that "you 've registered email" .. what should I add to controllers login.
function submit() {
//passing post data dari view
$_POST['dob'] = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$password = $this->input->post('password');
$email = $this->input->post('email');
$dob = $this->input->post('dob');
$jkl = $this->input->post('jkl');
$lastlogin = $this->input->post('lastlogin');
//memasukan ke array
$data = array(
'firstname' => $firstname,
'lastname' => $lastname,
'password' => $password,
'email' => $email,
'dob' => $dob,
'jkl' => $jkl,
'lastlogin' => $lastlogin,
'active' => 0
);
//tambahkan akun ke database
$this->m_register->add_account($data);
//redirect(base_url().'homepage/homepage');
$id = $this->m_register->add_account($data);
//enkripsi id
$encrypted_id = md5($id);
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => '*******#*****esy.com ',
'smtp_pass' => '**********',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$email_setting = array('mailtype'=>'html');
$this->email->initialize($email_setting);
$this->email->from('jobrecruit#jobrecruit.esy.es', 'JOBRECRUIT');
$this->email->to($email);
$this->email->subject('Confirmation Email');
$this->email->message("WELCOME TO JOB RECRUIT <br><p></p>Hallo $firstname $lastname <br><br><br><p>Terimakasih telah melakuan registrasi dengan:<br><br><p>
Username = $email<p>
Password = $password
<br><br>
<p>
untuk memverifikasi akun silahkan klik tautan dibawah ini</p><br><br>"
.site_url("login/register/verification/$encrypted_id")."
<br><br><br>
<p></p><br>
<p>Thanks</p>Admin JOBRECRUIT");
if($this->email->send())
{
$data = array ( 'isi' => 'login/vsuccess');
$this->load->view('layout/wrapper',$data);
}else
{
$data = array ( 'isi' => 'login/vgagal');
$this->load->view('layout/wrapper',$data);
}
}
I will suggest you to use form validation library of codeigniter.
function submit() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|xss_clean|is_unique[TABLE_NAME.email]');
$this->form_validation->set_message('is_unique', 'you have registered email.');
if($this->form_validation->run())
{
//passing post data dari view
$_POST['dob'] = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$password = $this->input->post('password');
$email = $this->input->post('email');
$dob = $this->input->post('dob');
$jkl = $this->input->post('jkl');
$lastlogin = $this->input->post('lastlogin');
//memasukan ke array
$data = array(
'firstname' => $firstname,
'lastname' => $lastname,
'password' => $password,
'email' => $email,
'dob' => $dob,
'jkl' => $jkl,
'lastlogin' => $lastlogin,
'active' => 0
);
//tambahkan akun ke database
$this->m_register->add_account($data);
//redirect(base_url().'homepage/homepage');
$id = $this->m_register->add_account($data);
//enkripsi id
$encrypted_id = md5($id);
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'jobrecruit#jobrecruit.esy.es ',
'smtp_pass' => 'jobrecruit123456',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$email_setting = array('mailtype'=>'html');
$this->email->initialize($email_setting);
$this->email->from('jobrecruit#jobrecruit.esy.es', 'JOBRECRUIT');
$this->email->to($email);
$this->email->subject('Confirmation Email');
$this->email->message("WELCOME TO JOB RECRUIT <br><p></p>Hallo $firstname $lastname <br><br><br><p>Terimakasih telah melakuan registrasi dengan:<br><br><p>
Username = $email<p>
Password = $password
<br><br>
<p>
untuk memverifikasi akun silahkan klik tautan dibawah ini</p><br><br>"
.site_url("login/register/verification/$encrypted_id")."
<br><br><br>
<p></p><br>
<p>Thanks</p>Admin JOBRECRUIT");
if($this->email->send())
{
$data = array ( 'isi' => 'login/vsuccess');
$this->load->view('layout/wrapper',$data);
}else
{
$data = array ( 'isi' => 'login/vgagal');
$this->load->view('layout/wrapper',$data);
}
}
}
Error message will be accessible in form_validation() or for specific form_error('email') print as it is on view to show error message
In Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
// if($this->session->userdata('user'))redirect('homepage/menu');
}
public function index($param='')
{
if($param == 'error')
$param = 'Incorrect username or password';
if($param == 'error1')
$param = 'User not acctive';
$data = array('title'=>'Login','message'=>$param, 'isi' => 'login/vlogin', 'base_url'=>base_url());
$this->load->view('layout/wrapper', $data);
}
public function do_login() {
$mail = $_POST['mail'];
$password = $_POST['password'];
$result = $this->Model_name->check_valid_user($mail,$password);
if ($result == 1)
{ //User exists
if ($result['active'] == 1)
{
//User exists and his email is verified
$session_set = array(
'user' => true,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'jkl' => $user->jkl,
'id' => $user->id,
'lastlogin' => $user->lastlogin
);
$this->Model_name->update_last_login($result['id']);
$this->session->set_userdata($session_set);
redirect('homepage/menu');
}
else
{
//User exists BUT his email is NOT verified
$this->session->set_flashdata('message', 'Akun anda belum aktif silahkan cek email anda untuk verifikasi');
//You have to capture and show the flash message in view
redirect('login/login/index');
}
}
else
{
//User does NOT exist at all
$this->session->set_flashdata('message', 'Username dan Password tidak sama.');
//You have to capture and show the flash message in view
redirect('login/login/index');
}
}
}
In Model
public function check_valid_user($mail,$password)
{
$query = $this->db->quesry("SELECT * FROM user WHERE mail='$mail' AND password = '$password'");
$result = $query->result_array();
$count = count($result);
if(empty($count) || $count >1 )
{
$log = 0;
return $log;
}
else
{
$log = 1;
return $log;
}
}
function update_last_login($id)
{
$data = array(
'lastlogin' => date('Y-m-d H:i:s')
);
$this->db->where('id', $id);
$this->db->update('user', $data);
}