J
J
Junior2018-05-10 18:03:15
PHP
Junior, 2018-05-10 18:03:15

How to start php session via ajax?

How to start session via ajax? (Files below)
All files are included and session_start() is written at the beginning of index.php;
index.php :

<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
                <form id="login-form" class="login-form">
                    <h3 class="logo"><?php echo $SiteName?></h3>
                    <div id="after-logo"></div>
                    <p><input class="form" placeholder="Логин" id="login" type="text" name="login"></p>
                    <p><input placeholder="Пароль" id="password" type="text" name="password"></p>
                    <p><button id="login-btn" class="btn" type="submit">Войти</button></p>
                    <hr>
                    <h6 class="no-important"><p class="logo">Yappi Labs LLC</p><br>Copyright © 2018 Yappi LLC. All rights reserved.</h6>
                    <div id="cont">
                        
                </div>
</div>

general.js :
$("#login-form").submit(function(){
    if($("#login").val()=='' || $("#password").val()==''){
        $("#after-logo").html('Вы не заполнили все поля!!!');
        return false;
    }else{
    $.ajax({
        url:'assets/includes/functions/php/general.php',
        type:'POST',
        dataType:'html',
        data:$("#login-form").serialize(),
        success: function(answer){
            ans = $.parseJSON(answer);
            $('#after-logo').html(ans);
        }
    });}
    return false;
});


general.php :
<?php
if(isset($_POST['login']) or isset($_POST['password'])){
    $login = htmlspecialchars(trim($_POST['login']));
    $password = htmlspecialchars(trim($_POST['password']));
    
    include 'db-config.php';
    
    $user_check = mysql_query("SELECT * FROM users WHERE login='$login'");
    $user_info = mysql_fetch_array($user_check);
    if($login==$user_info['login']){
        if($password==$user_info['password']){
            $_SESSION['login']==$user_info['login'];
            $_SESSION['id']==$user_info['id'];
            echo json_encode('<script type="text/javascript">
  location.replace("https://yappi.ml/feed");
</script>');
        }else{
            echo json_encode('Пароль набран неверно');
        }
    }else{
        echo json_encode('Пользователя с таким логином не существует');
    }
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2018-05-10
@402d

general.php and where is session_start() in it?

J
Jdjdjbdb, 2018-05-10
@Jdjdjbdb

<?php

session_start();

if(isset($_POST['login']) && isset($_POST['password'])){
    $login = htmlspecialchars(trim($_POST['login']));
    $password = htmlspecialchars(trim($_POST['password']));
    
    include 'db-config.php';
    
    $user_check = mysql_query("SELECT * FROM users WHERE login='$login'");
    $user_info = mysql_fetch_array($user_check);
    if($login == $user_info['login'] && $password == $user_info['password']){
            $_SESSION['login'] = $user_info['login'];
            $_SESSION['id'] = $user_info['id'];
            echo '<script type="text/javascript">location.replace("https://yappi.ml/feed");</script>';
    } else {
        echo 'Введите коректные данные';
    }
}
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question