icenine icenine - 2 months ago 20
PHP Question

Log in redirect PHP

How can i log in and stay in the current page? With this structure whatever the page i'm visiting, when i log in, i always land on the home page: here's the code from the config, login and main.php files:

MGconfig file

<?php

$user = 'root';
$pwd = '';
$server = 'localhost';
$bdschema = 'MG';

// mysql(i) form improved
$connection = mysqli_connect($server,$user, $pwd, $bdschema);

if (mysqli_connect_error()) {
echo "Error to DB ..." .mysqli_error($connection);
exit;
};

mysqli_set_charset($connection, "utf8");
//print_r($connection);


?>


Login.php (login file)

<?php

session_start();

$error="";
$successMessage="";

if ($_POST){
if(!isset($_POST["salada"]) || $_POST["salada"]===""){
$error = "PHP: An email is required <br>";
}


//If some field is missing, there's an error

if(!isset($_POST["arroz"]) || $_POST["arroz"]===""){
$error .= "PHP: A password is required";
}

if ($error !=""){
$error = '<div class="error-login">'.$error.'</div>';

//try to do login

}else {

//connect to DB
require("MGconfig.php");

//read parameters (fields)

$email = mysqli_real_escape_string($connection, $_POST["salada"]);
$pwd = md5(mysqli_real_escape_string($connection, $_POST["arroz"]));

//var_dump($email.$pwd);

$result = mysqli_query($connection, "select name, id from users where email = '".$email."' and password = '".$pwd."'");

// alerta erro
if (mysqli_num_rows($result) !==1){
$error='<div class="error-login">PHP: Invalid email or password</div>';
header("Location:index.php?error=".$error);

}else {

$nome = mysqli_fetch_row($result);

$_SESSION["name"] = $nome[0];
$_SESSION["id"]=$nome[1];

header ("Location: main.php");
}
}


}

?>


MAIN.PHP FILE

<?php
session_start();
?>

<?php

include "menu.php";


if(isset($_GET["area"])){
$destino = $_GET["area"];
} else {
$destino = "index";
}

switch($destino){

case "formsubmit":{
include("formsubmit.php");
} break;
case "index": {
include("inicio.php");
} break;

case "videos": {
include("videos.php");
} break;

case "FAQs": {
include("FAQs.php");
} break;

case "quemsomos": {
include("quemsomos.php");
} break;

case "form": {
include("form.php");
} break;

default: {
include("inicio.php");
} break;

}



?>
</div>

<div>

<?php

include "footer.php";

?>
</div>

Answer

Scott and MounirOnGithub: this worked(login file)

     header ("Location: main.php?area=".$_POST["area"]);
Comments