php header location redirect blank page - php

I have a problem with header location redirect me to a blank page without making me see the code and the browser does not report any error. I work locally with exampp
class UserController
{
public $username = '';
private $logged = false;
private $usermodel = '';
public function __construct()
{ session_start();
$this->usermodel = new UserModel();
if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_GET['action'])&& $_GET['action']== 'login' ){
$username = (isset($_POST['username']))? $_POST['username'] :false ;
$password = (isset($_POST['password']))? $_POST['password'] :false ;
if ($username !=false && $password !=false && $this->usermodel->checkLogin($username, $password)){
$this->username =$username ;
$this->logged = true ;
$_SESSION['username']= $username ;
$_SESSION['logged']= true ;
$_SESSION[ 'message' ] = 'Login effettuato correttamente';
}else{
$_SESSION[ 'message' ] = 'Errore con il login; riprovare!';
}
}
elseif (isset($_GET['action'])&& $_GET['action']== 'logout'){
unset($_SESSION['username']);
unset($_SESSION['logged']);
$_SESSION[ 'message' ] = 'Logout effettuato correttamente';
}
elseif (isset($_SESSION['username'])&& isset($_SESSION['logged'])){
$this->username = $_SESSION['username'] ;
$this->logged = true ;
}
elseif(($_SERVER['REQUEST_METHOD']=='POST' && isset($_GET['action'])&& $_GET['action']== 'registra' )){
$username = (isset($_POST['username']))? $_POST['username'] :false ;
$password = (isset($_POST['password']))? $_POST['password'] :false ;
$repassword = (isset($_POST['repassword']))? $_POST['repassword'] :false ;
$nome_reale = (isset($_POST['nome_reale']))? $_POST['nome_reale'] :false ;
$email = (isset($_POST['email']))? $_POST['email'] :false ;
if ($username !=false && $password !=false && $repassword !=false && $nome_reale && $email !=false
&& $this->usermodel->Registration($username,$password,$repassword,$nome_reale,$email) )
{
$this->username =$username ;
$this->logged = true ;
$_SESSION['username']= $username ;
$_SESSION['logged']= true ;
$_SESSION[ 'message' ] = "registrazione effettuato correttamente benvenuto $username";
}
}
$this->redirectToProperArea();
}
public function logged(){
return $this->logged ;
}
public function redirectToProperArea(){
$script_file = basename( $_SERVER[ 'SCRIPT_NAME' ] );
if ( $this->logged() && $script_file == 'login.php' ) {
ob_end_clean();
header('Location: ../index.php' );
exit;
}
elseif ( !$this->Logged() && ( $script_file == 'index.php' && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] != 'index' && $_GET[ 'action' ] != 'detail' && $_GET[ 'action' ] != 'logout' ) ) {
ob_end_clean();
header('Location: ../login.php');
exit;
}
elseif ( $this->logged() && $script_file == 'registra.php' ) {
ob_end_clean();
header('Location: views/benvenuto.php');
exit;
}
}
}

I Solved using the Function
`if (!headers_sent()) {
header('Location:views/benvenuto.php');
exit;
} `
Now I have another problem in benvenuto.php page I have an include ( ' messagge.php ' ) ; which is always in the views directory which is the same as benvenuto.php I find myself always with me does not load the blank page includes
Thanks for your help

Have you tried to replace :
header('Location:...');
to
echo '<script>document.location="..."</script>';

Related

Wordpress Registration with Email Confirmation

I have created a registration forms with PHP in my Wordpress site.
For now users can create an account and login normally.
But I want to add a function that sends an activation to email before they can login.
This is the current code I have.
<?php
$error= '';
$success = '';
global $wpdb, $PasswordHash, $current_user, $user_ID;
if(isset($_POST['task']) && $_POST['task'] == 'register' ) {
$role_option = $wpdb->escape(trim($_POST['role-option']));
$password1 = $wpdb->escape(trim($_POST['password1']));
$password2 = $wpdb->escape(trim($_POST['password2']));
$first_name = $wpdb->escape(trim($_POST['first_name']));
$last_name = $wpdb->escape(trim($_POST['last_name']));
$email = $wpdb->escape(trim($_POST['email']));
$email2 = $wpdb->escape(trim($_POST['email2']));
$username = $wpdb->escape(trim($_POST['username']));
$userrole = $wpdb->escape(trim($_POST['userrole']));
if( $email == "" || $password1 == "" || $password2 == "" || $username == "" || $first_name == "" || $last_name == "" || $role_option == "") {
$error= 'Please don\'t leave the required fields.';
} else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error= 'Invalid email address.';
} else if(email_exists($email) ) {
$error= 'Email already exist.';
} else if($email <> $email2 ){
$error= 'Emails do not match.';
} else if($password1 <> $password2 ){
$error= 'Password do not match.';
} else {
$user_id = wp_insert_user( array ('first_name' => apply_filters('pre_user_first_name', $first_name), 'last_name' => apply_filters('pre_user_last_name', $last_name), 'user_pass' => apply_filters('pre_user_user_pass', $password1), 'user_login' => apply_filters('pre_user_user_login', $username), 'user_email' => apply_filters('pre_user_user_email', $email), 'role' => apply_filters('pre_user_user_role', $role_option)) );
if( is_wp_error($user_id) ) {
$error= 'Error on user creation.';
} else {
do_action('user_register', $user_id);
$current_url = home_url($_SERVER['REQUEST_URI']);
wp_redirect( $current_url );
exit;
$success = 'You\'re successfully register';
}
}
}
?>

Error In Session With Login Scripts

I'm receiving the error Error In Session when I run my login script:
session_start();
if (isset($_POST['login'])) {
$logusername=$_POST['username'];
$logpassword=sha1(md5($_POST['password']));
$redirectLoginSuccess = "dashboard.php";
$result=mysqli_query($con, "SELECT * FROM users WHERE username='$logusername' AND password='$logpassword'")or die('Error In Session');
$row=mysqli_fetch_array($result);
if($result>0){
$access = mysqli_fetch_assoc($result,0,'access');
$userID = mysqli_fetch_assoc($result,0,'id');
$username = mysqli_fetch_assoc($result,0,'username');
$name = mysqli_fetch_assoc($result,0,'name');
//declare two session variables and assign them
$_SESSION['username'] = $username;
$_SESSION['userID'] = $userID;
$_SESSION['name'] = $name;
$_SESSION['access'] = $access;
}
header("Location: " . $redirectLoginSuccess );
}
I receive this error everytime I run the script not sure exactly where the error is.
The code below works but for some reason, I am not able to echo the session variable name. Is it possible that the session variable not being stored?
if ( isset( $_POST[ 'login' ] ) ) {
$errMsg = '';
// Get data from FORM
$username = $_POST[ 'username' ];
$password = sha1($_POST[ 'password' ]);
if ( $username == '' )
$errMsg = 'Enter username';
if ( $password == '' )
$errMsg = 'Enter password';
if ( $errMsg == '' ) {
try {
$stmt = $connect->prepare( 'SELECT id, name, username, password, access FROM users WHERE username = :username AND password = :password' );
$stmt->execute( array(
':username' => $username,
':password' => $password
) );
$data = $stmt->fetch( PDO::FETCH_ASSOC );
if ( $data == false ) {
$errMsg = "User $username not found.";
} else {
if ( $password == $data[ 'password' ] ) {
$_SESSION[ 'name' ] = $data[ 'fullname' ];
$_SESSION[ 'username' ] = $data[ 'username' ];
$_SESSION[ 'password' ] = $data[ 'password' ];
$_SESSION[ 'access' ] = $data[ 'access' ];
header( 'Location: dashboard.php' );
exit;
} else
$errMsg = 'Password not match.';
}
} catch ( PDOException $e ) {
$errMsg = $e->getMessage();
}
}
}

how to resolve if condition inside foreach loop

<?php
$login_info = array('batman'=>'404','superman'=>'502');
$form_usr = "$_POST[usr]";
$form_pass = "$_POST[pass]";
$form_submit = "$_POST[submit]";
if ( isset($form_submit) ) {
foreach ( $login_info as $user => $pass ) {
if ($form_usr == $user && $form_pass == $pass) {
echo "Successfully logged in !";
}
else {
echo "bad username or password !";
}
}
}?>
I'm getting bad username or password !bad username or password ! and Successfully logged in !bad username or password ! How can i solve this? I'm trying to show up Successfully logged in ! when condition is true else bad username or password !
Here is the screenshot: https://i.stack.imgur.com/rmTiG.png
You can avoid the loop altogether, instead just check your variables exist in the array.
<?php
$login_info = array('batman'=>'404','superman'=>'502');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$user = isset($_POST['usr']) ? $_POST['usr'] : null;
$pass = isset($_POST['pass']) ? $_POST['pass'] : null;
if (isset($login_info[$user]) && $login_info[$user] == $pass) {
echo "Successfully logged in !";
} else {
echo "bad username or password !";
}
}
?>
A variation on a theme perhaps but you could try like this, again not using a loop.
<?php
$message='bad username or password!';
$login_info = array('batman'=>'404','superman'=>'502');
$form_usr = isset( $_POST['usr'] ) ? $_POST['usr'] : false;
$form_pass = isset( $_POST['pass'] ) ? $_POST['pass'] : false;
$form_submit = isset( $_POST['submit'] ) ? $_POST['submit'] : false;
if ( $form_submit && $form_usr && $form_pass ) {
if( array_key_exists( $form_usr, $login_info ) && $login_info[ $form_usr ]==$form_pass ){
$message="Successfully logged in !";
}
echo $message;
}
?>

Can't get login success to view

so I've put in the write credentials to the login form, and it's supposed to redirect me to the home.php page which displays a successful login, however when I hit submit, the page just refreshes and doesn't do anything. If I change what the login_action loads after login it does it right, but then if I tell it to load home.php it just does nothing....Any Help?
Here's my home.php code:
<?php
session_start() ;
if( !isset($_SESSION['username']))
{
require('login_tools.php');
load();
}
$page_title = 'Home';
echo"<h1>HOME</h1>
<p>You are now logged in, {$_SESSION['username']}</p>";
echo'<p>Logout</p>';
?>
and the login_action.php
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST')
{
require ('../connect_db.php') ;
require ('login_tools.php') ;
list ($check, $data) =
validate($dbc, $_POST['username'], $_POST['password']);
if ($check )
{
session_start() ;
$_SESSION['user_id'] = $data['user_id'] ;
$_SESSION['username'] = $data['username'] ;
load('home.php') ;
}
else {$errors = $data ;}
mysqli_close( $dbc);
}
include('login.php');
?>
**login.php:**
<?php
$page_title = 'Login';
if ( isset( $errors ) && !empty( $errors))
{
echo'<p id="err_msg">Oops! There was a problem:<br>';
foreach ( $errors as $msg )
{
echo " - $msg<br>";
}
echo 'Please try again or Register</p>';
}
?>
<h1>Login</h1>
<form action="login_action.php" method="POST">
<p>
Username: <input type="text" name="username">
</p><p>
Password: <input type="password" name="password">
</p><p>
<input type="submit" value="Login" >
</p>
</form>
According to your code it is supposed to refresh. Indeed, it is not a refresh, it is the infinite loading of login.php by include it in the end of login_action.php
You should use header redirect instead of including as follows:
<?php
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
if ( $_SERVER['REQUEST_METHOD'] == 'POST')
{
require ('../connect_db.php') ;
require ('login_tools.php') ;
list ($check, $data) =
validate($dbc, $_POST['username'], $_POST['password']);
if ($check )
{
session_start() ;
$_SESSION['user_id'] = $data['user_id'] ;
$_SESSION['username'] = $data['username'] ;
$extra = 'home.php';
header("Location: http://$host$uri/$extra");
exit;
}
else {
$errors = $data ;
$_SESSION['Errors'] = $errors;
$extra = 'login.php';
header("Location: http://$host$uri/$extra");
exit;
}
mysqli_close( $dbc);
}
?>
In login.php
$page_title = 'Login';
if ( isset( $_SESSION['Errors'] ) && !empty( $_SESSION['Errors'])){
$errors = $_SESSION['Errors'];
//continue your code...
// at the end of the code:
unset($_SESSION['Errors']);
Based on your reply, i guess session doesn't get anything to load home. It is from:
$_SESSION['user_id'] = $data['user_id'] ;
$_SESSION['username'] = $data['username'] ;
this makes validate get unvalidated to send the session.
Try to change the $data into variables, as follows:
$_SESSION['user_id'] = $user_id ;
$_SESSION['username'] = $username ;
to show the message, you can use meta refresh to encertain that it really sends the login data.
If this not works, there must be something wrong with the grabbing data from the connection.
<?php # LOGIN HELPER FUNCTIONS.
# Function to load specified or default URL.
function load( $page = 'login.php' )
{
# Begin URL with protocol, domain, and current directory.
$url = 'http://' . $_SERVER[ 'HTTP_HOST' ] . dirname( $_SERVER[ 'PHP_SELF' ] ) ;
# Remove trailing slashes then append page name to URL.
$url = rtrim( $url, '/\\' ) ;
$url .= '/' . $page ;
# Execute redirect then quit.
header( "Location: $url" ) ;
exit() ;
}
# Function to check email address and password.
function validate( $dbc, $email = '', $pwd = '')
{
# Initialize errors array.
$errors = array() ;
# Check email field.
if ( empty( $email ) )
{ $errors[] = 'Enter your email address.' ; }
else { $e = mysqli_real_escape_string( $dbc, trim( $email ) ) ; }
# Check password field.
if ( empty( $pwd ) )
{ $errors[] = 'Enter your password.' ; }
else { $p = mysqli_real_escape_string( $dbc, trim( $pwd ) ) ; }
# On success retrieve user_id, first_name, and last name from 'users' database.
if ( empty( $errors ) )
{
$q = "SELECT user_id, first_name, last_name FROM users WHERE email='$e' AND pass=SHA1('$p')" ;
$r = mysqli_query ( $dbc, $q ) ;
if ( #mysqli_num_rows( $r ) == 1 )
{
$row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
return array( true, $row ) ;
}
# Or on failure set error message.
else { $errors[] = 'Email address and password not found.' ; }
}
# On failure retrieve error message/s.
return array( false, $errors ) ;
}

Wordpress after checking username and password how log in

hi i created this code below in my wordpress theme within my login.php page i created conditional statements successfully without any problem but in my last if statement when the username and password is correct i can't when this statement is correct i log in?
i want when the username and password is correct directly show legge in username and add the log out link to log out from the theme.
<?php
$error = '';
$success = '';
global $user_identity;
if(isset($_POST['task']) && $_POST['task'] == 'login') {
$username = esc_attr($_POST['login_username']);
$password = esc_attr($_POST['login_password']);
$remember = esc_attr($_POST['login_remember']);
$user = get_user_by('login', $username);
$user_id = $user->ID;
$user_data = get_userdata($user_id);
$user_login = $user_data->user_login;
$user_pass = $user_data->user_pass;
if($username == '' && $password == '') {
$error = 'Please Fill Required Fields!';
}
if($username == '') {
$error = 'Please Enter Your Username';
}
if($password == '') {
$error = 'Please Enter Your Password';
}
if($user_login != $username) {
$error = 'The Username is Incorrect';
}
if($user_pass != $password) {
$error = 'The Password is Incorrect';
}
if($user_login == $username && $user_pass == $password) {
}
}
?>
hey just create array of user data and passed into wp_signon($data,false) see below.
$login_data = array();
$login_data['user_login'] = $username;
$login_data['user_password'] = $password;
$login_data['remember'] = $remember; // set true or false for remember
$user_verify = wp_signon( $login_data, false );
if ( is_wp_error($user_verify) )
{
echo $user->get_error_message();
exit;
} else {
header("Location: " . home_url() . "/login/error/");
}
read document for more detail wp_signon()

Categories