• I have WordPress website where I setup my own table to store users login credentials. People can register fine but when it comes time to login and create a session it doesn’t seem to work.

    This is in my header:

    <?php
        // Initialize session
        session_start();
        // Initialize DB
        include("functions/connect.php");
        <span class="login">
              <?php
                if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
                {
              ?>
                <p>Welcome, <?php echo $_SESSION['Username']?></p>
              <?php
                }
                else
                {
              ?>
                <p>Login</p><p>|</p><p>Signup</p>
              <?php } ?>
        </span>

    This is on my login page.

    <?php
        	if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['password']) && !empty($_POST['password']) AND isset($_POST['name']) && !empty($_POST['name'])){
        				$username = mysql_escape_string($_POST['name']);
        				$password = mysql_escape_string(md5($_POST['password']));
    
        				$search = mysql_query("SELECT username, password, active FROM wp_thread_users WHERE username='".$username."' AND password='".$password."' AND active='1'") or die(mysql_error());
        				$match  = mysql_num_rows($search);
    
        				if($match > 0){
        					$msg = 'Login Complete! Thanks';
        					//$email = $row['EmailAddress'];
        			        $_SESSION['Username'] = $username;
        			        //$_SESSION['EmailAddress'] = $email;
        			        $_SESSION['LoggedIn'] = 1;
        				}else{
        					$msg = 'Login Failed!<br /> Please make sure that you enter the correct details and that you have activated your account.';
        				}
        			}
    
        	    ?>
        	    <!-- stop PHP Code -->
    
        		<!-- title and description -->
        		<h2>Login Form</h2>
        		<p>Please enter your name and password to login</p>
    
        		<?php
        			if(isset($msg)){ // Check if $msg is not empty
        				echo '<div class="statusmsg">'.$msg.'</div>'; // Display our message and add a div around it with the class statusmsg
        			} ?>
    
        		<!-- start sign up form -->
        		<form action="" method="post">
        			<label for="name">Username:</label>
        			<input type="text" name="name" value="" />
        			<label for="password">Password:</label>
        			<input type="password" name="password" value="" />
    
        			<input type="submit" class="submit_button" value="login" />
        		</form>

    My code works on a regular php page without WordPress, but as soon as I login on the WordPress site it redirects me to “wp-login.php” also it
    never stores the session. The reason I am using this method as I don’t want users to see the WordPress backend at all as well as be able to add new fields and a checkbox for newsletters and lastly I will be implementing a invitation code feature to users that they can generate 3 codes per user and be able to send them out to friends so they can register as well.

    I really would not like to use plugins as they don’t really fit the needs I am looking for unless there is a plugin I am overlooking.

    EDIT:

    This is in my functions.php 
    
        add_action('init', 'initEverything');
    
        function initEverything() {
            if(!session_id()) {
                session_start();
            }
        }

    Thanks.

The topic ‘Separate User Table in WordPress with Session’ is closed to new replies.