Custom Login Page - Session Problem

Hi,

I am trying to make a custom login page with registration and login on the same page with some custom fields so I didn't use any plugins. I wrote the following once the username and password are matched from the database.

$user_login=isset($get_user[0]->user_login)?($get_user[0]->user_login==''?'admin':$get_user[0]->user_login):'admin';
				$user=get_user_by('login',$get_user[0]->user_login);
				do_action('wp_login', $get_user[0]->user_login, $user);
				wp_set_current_user( $user->ID );
				wp_set_auth_cookie( $user->ID );

Under that code I quickly did a var_dump($current_user) and I am able to verify that I am logged in and have everything working but once I refresh the page or navigate to other pages I lose the login. Is it something to do with my session or cookie settings? Has anyone encountered this problem?

  • Timothy

    Hi Jack,

    So happy to hear from you again. Anyways attached is my code.

    Oh I think it didn't allow .php extension files. Here is the coding.
    I have removed the captcha for security purposes.

    <?
    	global $wpdb;
    	require_once($_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/themename/includes/lib/recaptchalib.php');
    	$publickey = ""; // you got this from the signup page
    	global $current_user;
    	get_currentuserinfo();
    
    	$error_message = '';
    	if($_POST['action1'] == "loginuser")
    	{
    		unset($user_id);
    		$chk_email = $wpdb->get_results('SELECT * FROM <code>wp_users</code> WHERE <code>user_email</code> = "'.$_POST['fullemaillogin'].'"');
    		if(count($chk_email) != 0)
    		{
    			$user_id = $chk_email[0]->ID;
    		}
    		else
    		{
    			$fix_phone = str_replace('+', '', str_replace('-', '', str_replace(' ', '', $_POST['fullemaillogin'])));
    			$get_pfields = $wpdb->get_results('SELECT * FROM <code>wp_usermeta</code> WHERE <code>meta_value</code> = "'.$fix_phone.'" AND <code>meta_key</code> = "phonenumber"');
    			if(count($get_pfields) != 0)
    			{
    				$user_id = $get_pfields[0]->user_id;
    			}
    			unset($get_pfields);
    			$get_pfields = $wpdb->get_results('SELECT * FROM <code>wp_usermeta</code> WHERE <code>meta_value</code> = "'.$fix_phone.'" AND <code>meta_key</code> = "alternatephonenumber"');
    			if(count($get_pfields) != 0)
    			{
    				$user_id = $get_pfields[0]->user_id;
    			}
    		}
    
    		if($user_id != null)
    		{
    			$vsessionid = session_id();
    			if (empty($vsessionid)) {session_name('PHPSESSID'); session_start();}
    			wp_clear_auth_cookie();
    			$get_user = $wpdb->get_results('SELECT * FROM <code>wp_users</code> WHERE <code>ID</code> = "'.$user_id.'"');
    
    			$chk_password = wp_check_password( $_POST['fullpasswordlogin'], $get_user[0]->user_pass, $user_id );
    			if($chk_password == true)
    			{
    				$creds = array();
    				$creds['user_login'] = $get_user[0]->user_login;
    				$creds['user_password'] = $_POST['fullpasswordlogin'];
    				$creds['remember'] = true;
    				$user = wp_signon( $creds );
    			}
    			else
    			{
    				echo 'false';
    			}
    		}
    		else
    		{
    			$error_message = __('User email / number is not found', 'themename');
    		}
    
    	}
    	if($_POST['action1'] == "registeruser")
    	{
    		$error_message = '';
    		$privatekey = "";
    		$resp = recaptcha_check_answer ($privatekey,
    									$_SERVER["REMOTE_ADDR"],
    									$_POST["recaptcha_challenge_field"],
    									$_POST["recaptcha_response_field"]);
    
    		if ($resp->is_valid) {
    			if(($_POST['fullpassword1'] == $_POST['fullpassword2']) && ($_POST['fullpassword1'] != ''))
    			{
    				if($_POST['fullemail'] != null)
    				{
    					$check_user = $wpdb->get_results('SELECT * FROM <code>wp_users</code> WHERE <code>user_email</code> = "'.$_POST['fullemail'].'"');
    					$full_phone = $_POST['fullphone'];
    					$fix_phone = str_replace('+', '', str_replace('-', '', str_replace(' ', '', $full_phone)));
    					$get_pfields1 = $wpdb->get_results('SELECT * FROM <code>wp_usermeta</code> WHERE <code>meta_value</code> = "'.$fix_phone.'" AND <code>meta_key</code> = "phonenumber"');
    					$get_pfields2 = $wpdb->get_results('SELECT * FROM <code>wp_usermeta</code> WHERE <code>meta_value</code> = "'.$fix_phone.'" AND <code>meta_key</code> = "alternatephonenumber"');
    					if(count($check_user) == 0)
    					{
    						if((count($get_pfields1) == 0) && (count($get_pfields2) == 0))
    						{
    							$verification_code = '';
    							for ($i = 0; $i<6; $i++)
    							{
    								$verification_code .= mt_rand(0,9);
    							}
    							$user_id = wp_create_user( $_POST['fullemail'], $_POST['fullpassword1'], $_POST['fullemail'] );
    							update_user_meta( $user_id, 'expirydate', '' );
    							update_user_meta( $user_id, 'displayname', $_POST['fullname'] );
    							update_user_meta( $user_id, 'confirmationCode', $verification_code );
    							update_user_meta( $user_id, 'phonenumber', $fix_phone );
    							echo '<div style="width: 100%; padding-bottom: 25px">';
    								echo '<div style="padding-left: 20%; padding-right: 20%; text-align: center">';
    									echo '<div style="width: 100%; border-radius: 15px; background-color: #666; color: #FFF; padding-top: 5px; padding-bottom: 5px">';
    										echo __('Your account has been successfully created. Please check your cellphone for verification code', 'themename');
    									echo '</div>';
    								echo '</div>';
    							echo '</div>';
    						}
    						else
    						{
    							$error_message = __('Phone number already exists', 'themename');
    						}
    					}
    					else
    					{
    						$error_message = __('Email already exists', 'themename');
    					}
    				}
    				else
    				{
    					$error_message = __('Please fill in email address', 'themename');
    				}
    			}
    			else
    			{
    				$error_message = __('Please check your confirmation password and ensure both password matches', 'themename');
    			}
    
    		} else {
    				$error_message = __('The reCAPTCHA was not entered correctly. Please try again.', 'themename');
    		}
    	}
    
    	if($error_message != '')
    	{
    		echo '<div style="width: 100%; padding-bottom: 25px">';
    			echo '<div style="padding-left: 20%; padding-right: 20%; text-align: center">';
    				echo '<div style="width: 100%; border-radius: 15px; background-color: #666; color: #FFF; padding-top: 5px; padding-bottom: 5px">';
    					echo $error_message;
    				echo '</div>';
    			echo '</div>';
    		echo '</div>';
    	}
    	echo '<div style="width: 100%; padding-bottom: 50px">';
    		echo '<div style="width: 50%; float: left">';
    			echo '<form name="myform" id="detail-form" enctype="multipart/form-data" method="post" action="">';
    				echo '<div style="width: 100%">';
    					echo '<div style="padding-bottom: 30px">';
    						echo '<div style="float: left">';
    							echo '<div style="padding-right: 12px">';
    								echo '<img src="http://www.themename.com/wp-content/uploads/2014/09/title-input.png" />';
    							echo '</div>';
    						echo '</div>';
    						echo '<div style="float: left">';
    							echo '<h1>'.__('Login Account', 'themename').'</h1>';
    						echo '</div>';
    						echo '<div style="clear: both"></div>';
    					echo '</div>';
    				echo '</div>';
    				// User Name
    				echo '<div style="width: 100%; min-height: 55px">';
    					echo '<div style="width: 35%; float: left">';
    						echo '<span style="padding-top: 15px; font-size: 18px">'.__('Email / Phone Number', 'themename').'</span>';
    					echo '</div>';
    					echo '<div style="width: 65%; float: left">';
    						echo '<input type="text" name="fullemaillogin" value="'.$_POST['fullemaillogin'].'" style="width: 75%; height: 25px; padding: 5px 10px">';
    					echo '</div>';
    					echo '<div style="clear: both"></div>';
    				echo '</div>';
    
    				// Password
    				echo '<div style="width: 100%; min-height: 55px">';
    					echo '<div style="width: 35%; float: left">';
    						echo '<span style="padding-top: 15px; font-size: 18px">'.__('Password', 'themename').'</span>';
    					echo '</div>';
    					echo '<div style="width: 65%; float: left">';
    						echo '<input type="password" name="fullpasswordlogin" style="width: 75%; height: 25px; padding: 5px 10px">';
    					echo '</div>';
    					echo '<div style="clear: both"></div>';
    				echo '</div>';
    
    				echo '<div style="width: 100%; text-align: center; padding-top: 25px">';
    					echo '<input type="hidden" name="action1" value="loginuser">';
    					echo '<input id="loginuser" type="submit" class="buttonform" value="'.__('Login Account', 'themename').'">';
    				echo '</div>';
    			echo '</form>';
    		echo '</div>';
    		echo '<div style="width: 50%; float: left">';
    			echo '<form name="myform" id="detail-form" enctype="multipart/form-data" method="post" action="">';
    				echo '<div style="width: 100%">';
    					echo '<div style="padding-bottom: 30px">';
    						echo '<div style="float: left">';
    							echo '<div style="padding-right: 12px">';
    								echo '<img src="http://www.themename.com/wp-content/uploads/2014/09/title-input.png" />';
    							echo '</div>';
    						echo '</div>';
    						echo '<div style="float: left">';
    							echo '<h1>'.__('Create Account', 'themename').'</h1>';
    						echo '</div>';
    						echo '<div style="clear: both"></div>';
    					echo '</div>';
    				echo '</div>';
    
    				// Phone Number
    				echo '<div style="width: 100%; min-height: 55px">';
    					echo '<div style="width: 35%; float: left">';
    						echo '<span style="padding-top: 15px; font-size: 18px">'.__('Cellphone Number', 'themename').'</span>';
    					echo '</div>';
    					echo '<div style="width: 65%; float: left">';
    						echo '<input type="text" name="fullphone" value="'.$_POST['fullphone'].'" style="width: 75%; height: 25px; padding: 5px 10px">';
    					echo '</div>';
    					echo '<div style="clear: both"></div>';
    				echo '</div>';
    
    				// User Name
    				echo '<div style="width: 100%; min-height: 55px">';
    					echo '<div style="width: 35%; float: left">';
    						echo '<span style="padding-top: 15px; font-size: 18px">'.__('Name', 'themename').'</span>';
    					echo '</div>';
    					echo '<div style="width: 65%; float: left">';
    						echo '<input type="text" name="fullname" value="'.$_POST['fullname'].'" style="width: 75%; height: 25px; padding: 5px 10px">';
    					echo '</div>';
    					echo '<div style="clear: both"></div>';
    				echo '</div>';
    
    				// User Email
    				echo '<div style="width: 100%; min-height: 55px">';
    					echo '<div style="width: 35%; float: left">';
    						echo '<span style="padding-top: 15px; font-size: 18px">'.__('Email Address', 'themename').'</span>';
    					echo '</div>';
    					echo '<div style="width: 65%; float: left">';
    						echo '<input type="text" name="fullemail" value="'.$_POST['fullemail'].'" style="width: 75%; height: 25px; padding: 5px 10px">';
    					echo '</div>';
    					echo '<div style="clear: both"></div>';
    				echo '</div>';
    
    				// User Password
    				echo '<div style="width: 100%; min-height: 55px">';
    					echo '<div style="width: 35%; float: left">';
    						echo '<span style="padding-top: 15px; font-size: 18px">'.__('Password', 'themename').'</span>';
    					echo '</div>';
    					echo '<div style="width: 65%; float: left">';
    						echo '<input type="password" name="fullpassword1" style="width: 75%; height: 25px; padding: 5px 10px">';
    					echo '</div>';
    					echo '<div style="clear: both"></div>';
    				echo '</div>';
    
    				// User Password
    				echo '<div style="width: 100%; min-height: 55px">';
    					echo '<div style="width: 35%; float: left">';
    						echo '<span style="padding-top: 15px; font-size: 18px">'.__('Confirm Password', 'themename').'</span>';
    					echo '</div>';
    					echo '<div style="width: 65%; float: left">';
    						echo '<input type="password" name="fullpassword2" style="width: 75%; height: 25px; padding: 5px 10px">';
    					echo '</div>';
    					echo '<div style="clear: both"></div>';
    				echo '</div>';
    
    				// Captcha
    				echo '<div style="width: 80%">';
    					?>
    					<script type="text/javascript">
    						 var RecaptchaOptions = {
    							theme : 'clean'
    						 };
    					 </script>
    
    					<?
    					echo recaptcha_get_html($publickey);
    				echo '</div>';
    
    				echo '<div style="width: 100%; text-align: center; padding-top: 25px">';
    					echo '<input type="hidden" name="action1" value="registeruser">';
    					echo '<input id="registeruser" class="buttonform" type="submit" value="'.__('Create Account', 'themename').'">';
    				echo '</div>';
    
    			echo '</form>';
    		echo '</div>';
    		echo '<div style="clear: both"></div>';
    	echo '</div>';
    ?>
  • Timothy

    Hi Jack,

    I've done some testing on my own. I found the problem is that when I have this code running on my header.php

    $credentials['user_login'] = 'usradmin';
    	$credentials['user_password'] = 'pwd';
    	$credentials['remember'] = false;
    	$result = wp_signon( $credentials );
    	if ( is_wp_error($result) )
    		echo $result->get_error_message();

    However, if I place it under a page template or inside my shortcode functions it does not work. Is it somewhat related to the sessions not being able to save?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.