Insert into members not working - php

I have a script running and working, however the insert into members part don't work:
function register_member($username,$name,$email,$password) {
$time = time();
$ip = get_ip();
db()->query("INSERT INTO members (full_name,username,email,password,ip_address,last_active,date_created,banned,active)VALUES(?,?,?,?,?,?,?,?,?)",
$name,$username,$email,$password, $ip, $time, $time,0,1);
//login user
login_user($username, $pass, false);
if (config('auto-follow', '')) {
$users = explode(',', config('auto-follow'));
foreach($users as $user){
$user = get_user($user);
if ($user) {
follow($user['id']);
}
}
}
return true;
}
It seems for me that it can't read the db()->query ("INSERT INTO part. When I remove that query, the signup script can complete reading the function, but if I leave it in, it returns a 500 internal error. Any ideas?

Finally got the php.ini to change, and it is in the js script the error is happening. On this line here: var json = jQuery.parseJSON(result);
$(document).on("submit", "#signup-form", function() {
$(".loader-container").fadeIn();
$(this).ajaxSubmit({
url : baseUrl + 'signup',
success : function(result) {
var json = jQuery.parseJSON(result);
if (json.status == 1) {
//we can redirect to the next destination
window.location.href = json.url;
notify(json.message, 'success');
} else {
notify(json.message, 'error');
$(".loader-container").fadeOut();
}
}
})
return false;
});

Related

Not getting ajax response on the server even with 200 status

I have added an ajax call on the blur of input and click on submit button to verify the email exist or not.
Code is simple to just check the existing data and return true false in response.
On local it is working totally fine but on client's server the ajax is not responding as expected. The response status is 200 but nothing in the preview of response on the console of the browser. For more clarity adding the picture.
Ajax code:
jQuery.ajax({
url : "{{ path('sales_account_email_exist') }}",
type : "POST",
cache : false,
data : {email:email_val,user_id:$("#user-id").val()},
success : function(data){
$(".loader").hide();
var obj = $.parseJSON(data);
if(obj===false)
{
$("#duplicate_email_exist").css("display","inline-block");
errorMessage = '<i class="warning sign icon"></i>' + email_val + ' : Email already exists in the system.';
$("#duplicate_email_exist").html(errorMessage);
email_exist_valid = false;
}
else
{
$("#duplicate_email_exist").css("display","none");
$("#duplicate_email_exist").html("");
email_exist_valid = true;
}
}
})
Ajax backend code:
/**
* #Route("/exist-email", name="sales_account_email_exist",methods={"GET","POST"})
*/
public function emailExist(Request $request): Response
{
$email = $request->get('email');
$user_id = $request->get('user_id');
if (isset($email) && !empty($email)) {
if (isset($user_id) && !empty($user_id)) {
$query = $this->em->createQueryBuilder()
->select('u.id', 'u.email')
->from('App\Entity\User', 'u')
->andWhere('u.email =\'' . str_replace("'", '', $email) . '\'')
->andWhere('u.id !=' . $user_id)
->getQuery();
} else {
$query = $this->em->createQueryBuilder()
->select('u.id', 'u.email')
->from('App\Entity\User', 'u')
->where('u.email = :email')
->setParameter('email', $email)
->getQuery();
}
$email_exist = $query->getArrayResult();
if (!empty($email_exist)) {
return new response('false');
} else {
return new response('true');
}
}
return new response('true');
}
jQuery.ajax({
url : "{{ path('sales_account_email_exist') }}",
type : "POST",
cache : false,
data : {email:email_val,user_id:$("#user-id").val()},
success : function(data){
$(".loader").hide();
var obj = $.parseJSON(data);
if(obj===false)
{
$("#duplicate_email_exist").css("display","inline-block");
errorMessage = '<i class="warning sign icon"></i>' + email_val + ' : Email already exists in the system.';
$("#duplicate_email_exist").html(errorMessage);
email_exist_valid = false;
}
else
{
$("#duplicate_email_exist").css("display","none");
$("#duplicate_email_exist").html("");
email_exist_valid = true;
}
},
// add this
error: function (request, status, error) {
alert(request.responseText); // <-- your code will be here
}
})
Check logs
Add error: statement after success:, probably this will help to find problem.

express.js res.redirect in app.post route not redirecting to given URL

I'm using express.js and app.post route is used to validate user and redirect valid user to PHP page using the res.redirect(), but instead of redirecting it is displaying Found. Redirecting to http://localhost/home.php
my server.js code is as follows:
app.post('/set_node_session', function(req, res){
sess = req.session;
var usermail = req.body.field1;
var password = req.body.field2;
var sql="SELECT * FROM userInfo WHERE userEmail='"+usermail+"'";
con.query(sql, function (err, result, fields) {
if (err) throw err;
sess.user_id=result[0].userId;
sess.first_name=result[0].userFname;
sess.last_name=result[0].userLname;
if(sess.user_id) {
res.redirect('http://localhost/home.php');
}
else {
res.redirect('http://localhost/login.php');
}
});
});
nodejs:
var resp = sess.user_id ? '1' : '0';
res.send(resp );
and in your php:
$result = curl_exec($ch);
if(result == 1){
header("Location: http://localhost/home.php");
}
else{
header("Location: http://localhost/login.php");
}
die();

verifyIdToken works one time and fails another time

I'm trying to implement google sign-in to on my website.
I've done the steps from here Authenticate with Google.
This function executes after i have logged in to google :
function onSignIn(googleUser) {
var googleResponse = googleUser.getAuthResponse();
google_login(googleResponse, true);
};
Google_login function:
function google_login(res) {
var httpObject = getXMLHTTPObject();
var ajax_url = siteURL + 'google_login';
var params = 'token='+encodeURIComponent(res.id_token);
httpObject.open('POST', ajax_url, true);
httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpObject.onreadystatechange = function() {
if (httpObject.readyState == 4) {
if(httpObject.responseText == 'true') {
window.location = httpObject.responseURL;
}
else {
if(httpObject.responseText == '') {
window.location = siteURL + 'login_again';
}
else {
window.location = siteURL + 'google_login_error';
}
}
}
};
httpObject.send(params);
}
And in my model I'm using this code:
private $google_client;
function Google_model() {
parent::__construct();
$this->google_client = new Google_Client(['client_id' => 'my_client_id','client_secret' =>'my_client_secret']);
}
function check_google_user($access_token) {
$payload = $this->google_client->verifyIdToken($access_token);
if ($payload) {
return $payload;
}
return false;
}
In my controller I'm calling check_google_user function.
And here appears a strange behaviour. Sometimes when I try to login I get the payload, and sometimes not (PS: I'm trying to login with the same user in the same day). Am I doing something wrong?
EDIT:
I'm getting this error: Caught exception: Cannot handle token prior to 2017-01-25T16:20:24+0200
Solved this by commenting these lines in firebase JWT.php file:
throw new BeforeValidException(
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
);

How to use Ajax and return view al the same time

I have a login method in my controller where I check if there is such user in database or not. I call this method when press Submit button. I show login from view at the same time.
In my case, it doesn't show message if there is such user. I think because in my controller I load view.
How could I show this message if there is such user using Ajax and if I return view as I do in my case?
I'm using Kohana. Thanks!
My code is:
$(document).ready(function(){
$('#submit').on('click', function() {
if(username.length === 0 || password.length === 0) {
//...check if validation fails
}
else {
$.ajax({
url: "/admin/signin" ,
type: "POST",
data: {
"username":username,
"password":password
},
success: function(data) {
if(data !== 'error') {
window.location = "/admin/index";
}
else
{
alert('no such user');
}
}
});
}
});
});
public function action_signin()
{
if ($_POST) {
$is_admin = Model_Admin::signin($_POST);
print 'success';
} else {
print 'error';
}
}
$this->template->content = View::factory('admin/login_form');
}
If you want not load 'default' template try using $this->auto_render = FALSE;
also kohana controller has method is_ajax $this->request->is_ajax()
You controller code will be like this.
public function action_signin()
{
if($this->request->is_ajax()){
$this->auto_render = FALSE;
}
if ($_POST) {
$is_admin = Model_Admin::signin($_POST);
if($is_admin){
print 'success';
} else {
print 'error';
}
}else{
$this->template->content = View::factory('admin/login_form');
}
}

login auth check keeps returning false in laravel 4

I'm creating a login system in laravel. On my local server, the code works but when I put it on a live server, the Auth::check() keeps return false and thus when I login with the right credentials it redirects me back to login page again. FYI My liver server is using php 5.4 while my local server is using php 5.5
Here is my code.
routes.php
Route::get('/', function() {
//Auth::check() KEEPS RETURNING FALSE EVEN WHEN USER LOGS IN
if (Auth::check() == true) {
$role = Auth::user()->role;
if ($role == 1) {
return View::make('administrator');
} elseif ($role == 9) {
return View::make('agent');
}
} else {
return View::make('login');
}
});
LoginController.php
public function login() {
if (Auth::attempt(array('username' => Input::json('username'), 'password' => Input::json('password')))) {
return Response::json(Auth::user());
} else {
return Response::json(array('flash' => 'Invalid email or password'), 500);
}
}
}
angularcontroller.js
$scope.login = function() {
//assign variables
var post = {};
post.username = $scope.info.username;
post.password = $scope.info.password;
//Validation
var errors = 0;
if (errors == 0) {
loginServ.login(post).then(function(data) {
if (data.status == 500) {
alert('wrong username or password');
} else {
window.location.replace("");
}
});
}
};
angular_service.js
login_module.factory('loginServ', function($http, $q) {
return {
login: function(post) {
var url = "login/login";
return $q.all([
$http.post(url, {
username: post.username,
password: post.password
})
])
.then(function(results) {
var data = [];
angular.forEach(results, function(result) {
data = data.concat(result.data);
// console.log("data: "+result);
// console.log("result.data: "+result.data);
});
return data[0];
},
function(error) {
console.log(error.status);
return error;
// Handle error here
});
}
}
});
I have login form using angularjs. It sends a POST request and if it gets a 200 response then it sends a GET request to "/". However even with right login credentials it keeps going back to the same login page.
Thanks
Try to change the value in app\config\session.php => 'cookie'.
This should fix the issue

Categories