Big or small characters on registration - php

I need add function, if someone enter username with big characters, it will be saved with small characters to db.
if(empty($_POST) === false && empty($errors) === true){
$register_data = array(
'username' => $_POST['username'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
'joined' => date('Y-m-d H:i:s'),
'email_code' => md5($_POST['username'] + microtime()),
);

strtolower()
strtolower($_POST['username']);
complete code
if(empty($_POST) === false && empty($errors) === true){
$register_data = array(
'username' => strtolower($_POST['username']),
'first_name' => strtolower($_POST['first_name']),
'last_name' => strtolower($_POST['last_name']),
'email' => $_POST['email'],
'password' => $_POST['password'],
'joined' => date('Y-m-d H:i:s'),
'email_code' => md5($_POST['username'] + microtime()),
);
}

So you mean, change the case to lower. You can use:
php in built function i.e. strtolower function like this,
$username = $_POST['username'];
$username = strtolower($username);

try strtolower() to convert a string in lowercase
strtolower($_POST['username'])
Also you need to escape your post strings to prevent sql injection

use strtolower()
$usernamestr = strtolower($username);
and insert this value in database

Related

Laravel inserts two user records at register, how to prevent this?

I am creating an application where I have to insert a user at registration with custom fields. As found online, i customised the create method in the Laravel RegisterController. However, now the application inserts two user records whenever I register a new user. Can someone help me with this please?
Here is the code of my create method in the RegisterController
protected function create(array $data)
{
/********************************************************************************
* CALCULATE ALL THE NEEDED DATA FOR THE USER
********************************************************************************/
// Delete the uncompleted registration
UncompletedRegistration::deleteByEmail($data['email']);
// Set the right values based on the filled values
$compercentagecreative = 0.0;
$compercentagenotcreative = 0.0;
$creativepercent = 0.0;
switch ($data['headjob']) {
case 1:
$compercentagecreative = config('constants.percentageRates.comPercentageCreative.headjob');
$compercentagenotcreative = config('constants.percentageRates.comPercentageNotCreative.headjob');
$creativepercent = config('constants.percentageRates.creativePercent.headjob');
break;
case 2:
$compercentagecreative = config('constants.percentageRates.comPercentageCreative.notheadjob');
$compercentagenotcreative = config('constants.percentageRates.comPercentageNotCreative.notheadjob');
$creativepercent = config('constants.percentageRates.creativePercent.notheadjob');
break;
default:
$compercentagecreative = config('constants.percentageRates.comPercentageCreative.headjob');
$compercentagenotcreative = config('constants.percentageRates.comPercentageNotCreative.headjob');
$creativepercent = config('constants.percentageRates.creativePercent.headjob');
break;
}
// Format the VAT number
$data['vatnumber'] = Helper::formatVatNumber($data['vatnumber']);
$isVatValid = false;
try {
// Check if vat is valid
$response = Helper::checkVat($data['vatnumber']);
$responseArray = json_decode($response);
$isVatValid = $responseArray->valid;
} catch (\Exception $exception) {
$isVatValid = false;
}
// Generate an activation key
$activationKey = md5(uniqid('CS', true));
/********************************************************************************
* CREATE THE USER IN THE DATABASE
********************************************************************************/
// Create the user and insert in the database
return User::create([
'usertype' => config('constants.userTypes.USER'),
'registeredon' => strtotime(date("Y-m-d H:i:s")),
'activationkey' => $activationKey,
'language' => 'nl',
'email' => Helper::truncate($data['email']),
'fullname' => Helper::truncate($data['lastname'] . ' ' . $data['firstname']),
'firstname' => Helper::truncate($data['firstname']),
'lastname' => Helper::truncate($data['lastname']),
'password' => Hash::make($data['password']),
'lastloginon' => strtotime('now'),
'lastloginip' => $_SERVER['REMOTE_ADDR'],
'activatedon' => strtotime(date('Y-m-d H:i:s', strtotime('1970-01-01 00:00:00'))),
'deleted' => false,
'companyname' => Helper::truncate($data['companyname']),
'street' => Helper::truncate($data['street']),
'number' => Helper::truncate($data['number']),
'city' => Helper::truncate($data['city']),
'zipcode' => Helper::truncate($data['zipcode']),
'vatnumber' => Helper::truncate($data['vatnumber']),
'validvat' => $isVatValid,
'website' => Helper::truncate($data['website']),
'phonenumber' => Helper::truncate($data['phonenumber']),
'bankname' => Helper::truncate($data['bank']),
'iban' => Helper::truncate($data['iban']),
'bicswift' => Helper::truncate($data['bicswift']),
'paymentremindermode' => $data['paymentremindermode'],
'invoicecreationremindermode' => 2,
'nettosavedmailmode' => 1,
'zombiemailsent' => 0,
'zombiemail180sent' => 0,
'nettosavedperinvoicmailemode' => 1,
'logo' => NULL,
'emailaccepted' => false,
'contractaccepted' => false,
'compercentagecreative' => $compercentagecreative,
'compercentagenotcreative' => $compercentagenotcreative,
'contractdate' => date("Y-m-d H:i:s"),
'creativepercent' => $creativepercent,
'activity' => $data['activity'],
'headjob' => $data['headjob'],
'template' => config('constants.templates.ORIGINAL'),
'freebtw' => isset($data['freebtw']) ? ($data['freebtw'] == "on" ? true : false) : false,
'refid' => Input::get('invite_id'),
'api_key' => Helper::generateRandomString(40),
'allowed_quotation' => true,
'send_bcc' => false
]);
}

POST request in PHP is returning PHP code when file extension is used

I am sending post requests in PHP to get a boolean value from my API (so it should return wither true or false)
This is the code I am using in the file for my API. The file is called users.php
if ($_POST['type'] == "authenticateMinecraft"){
$p = new dibdibs\post(
array(
'url' => 'https://authserver.mojang.com/authenticate',
'data' => array(
'agent' => array(
'name' => 'Minecraft',
'version' => 1
),
'username' => $_POST['username'],
'password' => $_POST['password'],
'clientToken' => "33225A179D9A4E1BDA73C012C1C3CBAB8BD00326883BDBEB6FA682482E40F68D"
)
)
);
$res = $p->json();
if (isset($res["selectedProfile"])){
echo("true");
}
else{
echo("false");
}
}
This is the code I am using to reference it (I am using a class which I have put on Pastebin to actually send the request).
$params = array(
'data' => array(
'type' => 'authenticateMinecraft',
'username' => $mcuname,
'password' => $mcpasswd
),
'url' => "api/users.php"
);
$c = new dibdibs\post($params);
$r = $c->http();
var_dump($r);
Whenever I use the .php fule extension when defining url, the whole PHP code of the API page is returned, but when I remove the extension, only true or false is returned. Why is this and is it a problem that I should be aware of and I should fox?

Creating Cookies in Codeigniter

Well I have created sessions but having trouble implementing cookies in my website. In config file i have set $config['sess_expire_on_close'] = TRUE; so that user's session expires on closing browser.
Now what i want is, On login if user checks remember me ... all data should be stored in a cookie so that on closing browser, user is still logged on.
function login($email, $password, $loginas) {
if ($loginas == 'user') {
$this->db->select('*');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get("user_info");
if ($query->num_rows() > 0) {
foreach ($query->result() as $rows) {
$newdata = array('id' => $rows->id,
'firstname' => $rows->firstname,
'lastname' => $rows->lastname,
'address' => $rows->address,
'city' => $rows->city,
'email' => $rows->email,
'phone' => $rows->phone,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
if ($rememberme == 'on') {
// create a cookie here with all data that i have put in session
}
return TRUE;
}
return FALSE;
}
}
Does creating cookie automatically creates session? or we have put these data in session manually again?
In CodeIgniter, you can use set_cookie()
$cookie = array(
'name' => 'The Cookie Name',
'value' => 'The Value',
'expire' => '86500',
'domain' => '.example.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
First of all you need to load cookie helper
$this->load->helper('cookie');
Set your cookie
$cookie = array(
'name' => "cookieName",
'value' => array('id'=>$rows->id,
'firstname'=>$rows->firstname,
'lastname'=>$rows->lastname,
'address'=>$rows->address,
'city'=>$rows->city,
'email'=>$rows->email,
'phone'=>$rows->phone,
'logged_in'=>TRUE
) ,
'expire' => '86500',
);
Just pass your array into set cookie
$this->input->set_cookie($cookie);
And you can retrieve it using
$cookie['cookieName']['id'];
Also read manual

Update CC info of Braintree in PHP

I am trying to update CC info of braintree but function provided in docs is not working . and I am unable to find out the reason.
Here is my function :
public function updateCC(){
$fname = $this->input->get_post('fname');
$lname = $this->input->get_post('lname');
$expirationYear = $this->input->get_post('expirationYear');
$expirationMonth = $this->input->get_post('expirationMonth');
$cardholderName = $this->input->get_post('cardholderName');
$cvv = $this->input->get_post('cvv');
$cc_no = $this->input->get_post('cc_no');
$token = $this->input->get_post('token');
$BTCustomerID = $this->input->get_post('BTCustomerID ');
$result = Braintree_Customer::update(
$BTCustomerID,
(
'firstName' => $fname,
'lastName' => $lname,
'creditCard' => (
'paymentMethodNonce' => 'fake-valid-nonce',
'options' => (
'updateExistingToken' => $token,
'verifyCard' => true
)
)
));
echo json_encode(array('error'=>-1));
}
Due to this code my application is crashing .
$result = Braintree_Customer::update(
$BTCustomerID,
(
'firstName' => $fname,
'lastName' => $lname,
'creditCard' => (
'paymentMethodNonce' => 'fake-valid-nonce',
'options' => (
'updateExistingToken' => $token,
'verifyCard' => true,
)
)
));
I’m a developer at Braintree. In your call to Braintree_Customer::update(), paymentMethodNonce must be passed inside of creditCard at the same level as options. In your code you are passing the it inside of options. See this code example for reference.

Appending a new key into a existing array

it may seem to be a silly question, but i dont know why, this thing isn't working for me.
I have an exisiting array
$data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'password' => $enc_password,
'date_of_adding' => date('d/m/Y'),
'support_timestamp' => $timestamp
);
now i want to insert or append a new key with value into this existing array.
where the new key and its value is
'username' => $username
what i did is that
$data['username'] = $username;
but still the thing isn't working.
Can anybody tell what i am doing wrong??
try this
$data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'password' => $enc_password,
'date_of_adding' => date('d/m/Y'),
'support_timestamp' => $timestamp
);
$data2 = array('username'=>$username);
$data = array_unshift($data,$data2);
You can do like this:
$data[]=array('username' => $username)
It will append the new array into an existing array
It should work using $data['username'] = $username;
Have a look at it here. Click the run icon
You may use
$data2 = array('username'=>$username);
$data = array_push($data,$data2);
or visit
demo

Categories