How to create a button if you are logged in to an account

Asked

Viewed 838 times

0

Hello, I wonder when someone is logged in to create a FILE type button. The registration and login system is created, but I do not know how to know when it is logged in or not.

index php.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LuppBox</title>
<link rel="stylesheet" type="text/css" href="estilo_index.css"/>
</head>
<body bgcolor="#0099FF">
	<ul id="cabecario">
    	<li id="logo"><img src="fotos_site/logo.png" width="auto" height="60" /></li>  
        <li id="login_cadastro_css"><a href="login_cadastro.php">Login | Cadastro</a></li>         
    </ul>  
</body>
</html>

make login.php

<?
	include "connection.php";
	
	$login = $_POST['login_entrar'];
	$senha = $_POST['senha_entrar'];
	
	$sql = mysqli_query($coneccao, "SELECT * FROM usuarios WHERE login = '$login'");	
	
	
	while($linha = mysqli_fetch_array($sql))
	{
		$senha_db = $linha['senha'];
	}
	
	$cont = mysqli_num_rows($sql);
	
	if($cont == 0)
	{		
		echo "<meta http-equiv='refresh' content='0; url=index.php'>
		<script type='text/javascript'>alert('Este usuario não existe')</script>";		
	}
	else
	{
		if($senha_db != $senha)
		{
			echo "<meta http-equiv='refresh' content='0; url=index.php'>
			<script type='text/javascript'>alert('Senha incorreta')</script>";	
		}
		else
		{
			session_start();
			
			$_SESSION['login_usuario'] = $login;	
			$_SESSION['senha_usuario'] = $senha;
			
			header("location: perfil.php");	
		}
	}
	
	mysqli_close($coneccao);
?>

make registration.php

<?
	include "connection.php";
	
	$login = $_POST['login_entrar'];
	$senha = $_POST['senha_entrar'];
	
	$sql = mysqli_query($coneccao, "SELECT * FROM usuarios WHERE login = '$login'");	
	
	
	while($linha = mysqli_fetch_array($sql))
	{
		$senha_db = $linha['senha'];
	}
	
	$cont = mysqli_num_rows($sql);
	
	if($cont == 0)
	{		
		echo "<meta http-equiv='refresh' content='0; url=index.php'>
		<script type='text/javascript'>alert('Este usuario não existe')</script>";		
	}
	else
	{
		if($senha_db != $senha)
		{
			echo "<meta http-equiv='refresh' content='0; url=index.php'>
			<script type='text/javascript'>alert('Senha incorreta')</script>";	
		}
		else
		{
			session_start();
			
			$_SESSION['login_usuario'] = $login;	
			$_SESSION['senha_usuario'] = $senha;
			
			header("location: perfil.php");	
		}
	}
	
	mysqli_close($coneccao);
?>

I would like to put a FILE type button under that case you are logged in.

Grateful from now on.

2 answers

2

Check that the session is active using the method isset().

    if ( isset($_SESSION["login_usuario"]) && isset($_SESSION["senha_usuario"])) {
        echo "<button type=\"button\">Botão</button>";
    } else {
        echo "Usuário não entrou.";
    }

A tip: you can set the session as a array, is easier to use, see:

$login = "john_doe";
$senha = password_hash("soeuseiasenha", PASSWORD_DEFAULT);

$_SESSION["usuario"] = array();
$_SESSION["usuario"] = $login;
$_SESSION["senha"]   = $senha;

So to manipulate her just do this:

if ( isset($_SESSION["usuario"]) ) {
    echo "...";
}

  • Hello Eduardo, but he can’t check, he always says I’m not logged in even though I’m.

  • Do you want to put this button on which page, @Lucascarezia? Remember to put the session_start() in the first line of your php script.

  • Set to index.php

0


How do I: I divide page into small HTML blocks and I will assemble under conditions, conditions that can be the user login in this way:

session_start();
if((!isset($_SESSION['email']) == true) and (!isset($_SESSION['pass']) == true)){
        unset($_SESSION['email']); 
        unset($_SESSION['pass']);
        require_once 'components/modals/login-modal.php';
        require_once 'components/modals/create-account-modal.php';
    }else{
        require_once 'components/modals/logout-modal.php';
    }

within each file with extension . php has a pure HTML that may or may not be attached to the page. I use the super global $_SESSION to store log-in values.

  • .@Lucascarezia please upvote on my reply as it was helpful to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.