0
I’m having trouble with my PHP code for the User Login. This code is working on local machine, however I went up in my instance Amazon and the moment I log in the server does not authenticate the session. The page login.php is in a login.xxxx.com.br subdomain and the restricted page is in another subdomain strict paginar.xxxx.com.br, so when I go to debug the code and call the login page variables in the restricted area apache informs that the variable has not been set or is apache on my server can not open a session www domains different, as I performed tests with the files in the same subdomain.
login:
<?php session_start(); ?>
<?php
require('db_conn.php');
if(isset($_POST['entrar'])){
$usuario = $_POST['usuario'];
$senha = $_REQUEST['senha'];
$sql= ("SELECT * FROM login WHERE usuario ='$usuario' AND senha ='$senha'");
$query=mysql_query($sql) or die (mysql_error());
$results= mysql_num_rows($query);
if($results == 0){
echo "<script>alert('Erro ao logar')</script>";
echo "<meta HTTP-EQUIV='refresh' CONTENT='5;URL=http://portal.xxxxx.com.br'>";
}else{
// Cria uma sessão que identifica se o usuário efetuou o login
session_start();
$_SESSION["usuario"]=$usuario;
echo "<script>alert('Usuário autenticado com sucesso')</script>";
echo "<meta HTTP-EQUIV='refresh' CONTENT='0;URL= http://user.xxxxxx.com.br'>";
}
}
?>
restricted page:
<?php
$usuario=$_SESSION["usuario"];
if(isset($usuario)){
echo "<script>alert('Usuário autenticado com sucesso')</script>";
return true;
}else{
//session_destroy();
header( "Location:http://portal.xxxxx.com.br/" , TRUE , 302 );
}
// Logout
if( isset($_GET["acao"]) && $_GET["acao"]=="logout" ) {
// Destrói todos os dados da sessão
session_destroy();
// Redireciona o usuário para o formulário de login
header( "Location:http://portal.xxxxxx.com.br/" , TRUE , 302 );
exit;
}
?>
In your
login.php
the secondsession_start();
is too much and should be removed. On your restricted page, you lack thesession_start();
at file start so you can use the session variable$_SESSION["usuario"]
.– Zuul
Ivan, I reversed his last edition, as it seemed to be based on William’s reply. This ended up changing the question, and invalidating at least one of the existing answers. To clarify questions about each answer, use the comments below them, or chat. But none of the current answers solve your problem?
– bfavaretto
@bfavaretto None of the answers solved my problem.
– IvanFloripa