Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP Re-Direct after login

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #31938
    rtmedia
    Member

    Hi Guys – Basically I purchased a login Script that I am fairly happy with other than the fact that once a user has successfully registered or successfully logged in they are simply given a success message but I want them to be automatically to be re-directed to a page of my choosing (e.g. success-login.php or success-registration.php etc)

    I have contacted the guy who made the script but he’s not been back in touch and I really need to find out how to do this. So was wondering if you could help (I’m not the greatest with php)

    Please find my login script below:

    require './includes/config.inc.php';
    class Login
    {
    // Allow access by all methods::
    public $conn;
    // This is a magic method called automatically when class is used::
    public function __construct()
    {
    /**
    * Change the database connection details inside the config.inc.php file::
    * Location: includes/config.inc.php::
    */
    $this->conn = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME)
    or die('I'm sorry but there is a problem with the server. Please check back soon.');
    }

    /**
    * This method checks the username and password from the login
    * and returns either a success message or error message::
    */
    public function checkUserLogin($username,$password)
    {
    // Set up our SQL statement::
    $sql = 'SELECT member_username,member_admin FROM members WHERE member_username = ? AND member_password = ?';

    // Check their login attempts::
    if(isset($_SESSION) && $_SESSION >= NUMBER_OF_ATTEMPTS)
    {
    $message = true;
    $message = "I'm sorry - You have had too many failed login attempts, please try again later.";
    return json_encode($message);
    }
    else
    {
    if($stmt = $this->conn->prepare($sql))
    {
    // A nice and secure way to query the database::
    $stmt->bind_param('ss',$username,$password);
    $stmt->execute();
    $stmt->bind_result($username,$admin);
    if($stmt->fetch())
    {
    // Set some sessions::
    $_SESSION = true;
    $_SESSION = $username;
    // If member_admin = 0 then they are standard user::
    $_SESSION = $admin;
    // Reset login attempts::
    $_SESSION = 0;

    // Ok so they have logged in... yaay!::
    $message = false;
    $message = "Welcome $username, you are now logged in!";
    return json_encode($message);
    }
    else
    {
    // Create a session and rack the attempts up so we can lock em out::
    @$_SESSION = $_SESSION + 1;
    // Ok they supplied incorrect details so scare them away::
    $message = true;
    $message = "You have entered an incorrect username/password.";
    return json_encode($message);
    }
    }
    }
    }

    ?>

    I’m guessing that the code I need to change is somewhere around here:

    $message = false;
    $message = "Welcome $username, you are now logged in!";
    return json_encode($message);

    Because that is where the login success message is. I’ve tried the following:

    $message = false;
    header("Location: login-success.php");
    exit();

    But have had no such luck I just get an error. This is only the first part of the code as this was the longest I could post in this forum so the piece of code perhaps that I may need to change may not be here – hopefully it is but let me know if theres something else I’m missing

    Would really appreciate if someone could help me out :)

    Richy

    #56102
    richtestani
    Member

    Instead of hacking the class, check if the user login was successful.
    Since the method is returning a value, test for the value in your javascript.

    it looks like this class was designed to work with an ajax call since it’s returning a json encoded response. So in your javascript, in the onSuccess parameter (assuming jQuery or Prototype), you could check the value of your response like:


    onSuccess: function(response) {
    if(!response.error) {
    alert(response.message);
    } else {
    location.href = '/my_page.html';
    }
    }

    You may have to parse the json response in your javascript as well.

    #56103
    rtmedia
    Member

    Firstly just wanna’ say thankyou for taking the time to help me.. I sorta had a feeling it may be in JavaScript file.

    I can login fine. i’m getting the success msg but I don;t want a success message I simply want the user to be re-directed to a new page

    I haven’t got a clue what im even looking for here. Below is the code to the file if you could maybe take a look to help:

    $(document).ready(function(){

    /**
    * Ensure that we have some text before we submit::
    */
    $('#login').click(function(){
    if($('input#username').val() == "" || $('input#password').val() == "")
    {
    $('#feedback').removeClass().addClass('error').text('Please enter a username and password!').fadeIn();
    return false;
    }
    else
    {
    $('#loading').fadeIn();
    $('#feedback').hide();

    $.ajax
    ({
    type: 'POST',
    url: 'process.php',
    dataType: 'json',
    data:
    {
    username: $('input#username').val(),
    password: $('input#password').val()
    },
    success:function(data)
    {
    $('#loading').hide();
    $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
    if(data.error === true)
    {
    // $('#memberLoginForm').fadeOut();
    }
    else
    {
    $('#memberLoginForm').fadeOut();
    $('#footer').fadeOut();
    $('.link').fadeIn();
    }
    },
    error:function(XMLHttpRequest,textStatus,errorThrown)
    {
    $('#loading').fadeOut();
    $('#feedback').removeClass().addClass('error').text('I'm sorry but the server has died, please try again!').fadeIn();
    // $('#memberLoginForm').fadeIn();
    }
    });
    return false;
    }
    });

    $('#register').click(function(){
    if($('input#register-username').val() == "" || $('input#register-password').val() == ""
    || $('input#secret-answer').val() == "" || $('input#register-email').val() == "" || $('input#captcha').val() == ""
    || $('input#register-firstname').val() == "" || $('input#register-surname').val() == "" || $('input#register-age').val() == ""
    || $('input#register-country').val() == "" || $('input#register-contactno').val() == "")
    {
    $('#feedback').removeClass().addClass('error').text('Please fill out all the fields above!').fadeIn();
    return false;
    }
    else
    {
    $('#loading').fadeIn();
    $('#feedback').hide();

    $.ajax
    ({
    type: 'POST',
    url: 'process.php',
    dataType: 'json',
    data:
    {
    regUsername: $('input#register-username').val(),
    regPassword: $('input#register-password').val(),
    secretAnswer: $('input#secret-answer').val(),
    regEmail: $('input#register-email').val(),
    captcha: $('input#captcha').val(),
    regFirstname: $('input#register-firstname').val(),
    regSurname: $('input#register-surname').val(),
    regAge: $('input#register-age').val(),
    regCountry: $('input#register-country').val(),
    regContactNo: $('input#register-contactno').val()
    },
    success:function(data)
    {
    $('#loading').hide();
    $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
    if(data.error === true)
    {
    // $('#memberLoginForm').fadeOut();
    }
    else
    {
    $('#memberLoginForm').fadeOut();
    $('#footer').fadeOut();
    $('.link').fadeIn();
    }
    },
    error:function(XMLHttpRequest,textStatus,errorThrown)
    {
    $('#loading').fadeOut();
    $('#feedback').removeClass().addClass('error').text('I'm sorry but the server has died, please try again!').fadeIn();
    // $('#memberLoginForm').fadeIn();
    }
    });
    return false;
    }
    });

    $('#forgot-password').click(function(){
    if($('input#forgot-email').val() == "")
    {
    $('#feedback').removeClass().addClass('error').text('Please enter an email so I can work with it!').fadeIn();
    return false;
    }
    else
    {
    $('#loading').fadeIn();
    $('#feedback').hide();

    $.ajax
    ({
    type: 'POST',
    url: 'process.php',
    dataType: 'json',
    data:
    {
    forgotEmail: $('input#forgot-email').val()
    },
    success:function(data)
    {
    $('#loading').hide();
    $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
    if(data.error === true)
    {
    // $('#memberLoginForm').fadeOut();
    }
    else
    {
    $('#forgotPasswordForm').fadeOut();
    $('#footer').fadeOut();
    $('.link').fadeIn();
    }
    },
    error:function(XMLHttpRequest,textStatus,errorThrown)
    {
    $('#loading').fadeOut();
    $('#feedback').removeClass().addClass('error').text('I'm sorry but the server has died, please try again!').fadeIn();
    // $('#memberLoginForm').fadeIn();
    }
    });
    return false;
    }
    });

    $('#renew-password').click(function(){
    if($('input#re-password').val() == "" || $('input#re-password-2').val() == "")
    {
    $('#feedback').removeClass().addClass('error').text('Please enter a new password for your account!').fadeIn();
    return false;
    }
    else
    {
    $('#loading').fadeIn();
    $('#feedback').hide();

    $.ajax
    ({
    type: 'POST',
    url: 'process.php',
    dataType: 'json',
    data:
    {
    newPass: $('input#re-password').val(),
    newPass2: $('input#re-password-2').val(),
    newEmail: newEmailVariable
    },
    success:function(data)
    {
    $('#loading').hide();
    $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
    if(data.error === true)
    {
    // $('#memberLoginForm').fadeOut();
    }
    else
    {
    $('#forgotPasswordForm').fadeOut();
    $('#footer').fadeOut();
    $('.link').fadeIn();
    }
    },
    error:function(XMLHttpRequest,textStatus,errorThrown)
    {
    $('#loading').fadeOut();
    $('#feedback').removeClass().addClass('error').text('I'm sorry but the server has died, please try again!').fadeIn();
    // $('#memberLoginForm').fadeIn();
    }
    });
    return false;
    }
    });
    });
    #56110
    rtmedia
    Member

    I also have this file called process.php that may have something to do with it

    
    
    sleep(2);

    if(!empty($_POST) && !empty($_POST))
    {
    $username = strip_tags($_POST);
    $password = strip_tags(md5($_POST));

    require_once 'classes/Login.php';
    $Login = new Login;
    if($message = $Login->checkUserLogin($username,$password))
    {
    echo $message;
    }
    }
    elseif(!empty($_POST) && !empty($_POST) && !empty($_POST) && !empty($_POST) && !empty($_POST) && !empty($_POST) && !empty($_POST) && !empty($_POST) && !empty($_POST))
    {
    // Clean the variables before we pass them into the class::
    $username = strip_tags($_POST);
    $password = strip_tags(md5($_POST));
    $email = strip_tags($_POST);
    $captcha = $_POST;
    $firstname = strip_tags($_POST);
    $surname = strip_tags($_POST);
    $age = strip_tags($_POST);
    $country = strip_tags($_POST);
    $contactno = strip_tags($_POST);

    $userPassword = strip_tags($_POST);

    require_once 'classes/Login.php';
    $Login = new Login;
    if($message = $Login->registerUser($username,$password,$email,$captcha,$userPassword,$firstname,$surname,$age,$country,$contactno))
    {
    echo $message;
    }
    }
    elseif(!empty($_POST))
    {
    $email = strip_tags($_POST);
    require_once 'classes/Login.php';
    $Login = new Login;
    if($message = $Login->forgotPassword($email))
    {
    echo $message;
    }
    }
    elseif(!empty($_POST) && (!empty($_POST)) && (!empty($_POST)))
    {
    $newPass = strip_tags(md5($_POST));
    $newPass2 = strip_tags(md5($_POST));
    $newEmail = strip_tags($_POST);

    require_once 'classes/Login.php';
    $Login = new Login;
    if($message = $Login->changeUserPassword($newEmail,$newPass,$newPass2))
    {
    echo $message;
    }


    }
    else
    {
    /**
    * This shouldn't get called because the jQuery will stop it. However people can be sneaky
    * and disable javascript. Tbh doesn't matter but... Lighter server costs whenever eh?
    */
    $message = true;
    $message = "Please fill out all of the required fields!";

    echo json_encode($message);
    }

    ?>
    #56082
    richtestani
    Member

    Hi –
    It looks like you can replace the success method with:

    success:function(data)
    {
    $('#loading').hide();
    $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
    if(data.error === true)
    {
    // $('#memberLoginForm').fadeOut();
    }
    else
    {
    $('#memberLoginForm').fadeOut();
    $('#footer').fadeOut();
    $('.link').fadeIn();
    location.href = 'http://www.apple.com';
    }
    },

    I’ve only added the line: location.href =
    Replace the value with wherever you want to go.

    This is the javascript way. Since it’s being called with Javascript, there is no way to redirect with the php unless you remove the ajax call.

    Hope this helps.
    Rich

    #55856
    rtmedia
    Member

    Your an absolute legend.. Thanks allot Rich

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.