Jump to content
DevilRocks92

Login - Criar $_SESSION

Recommended Posts

DevilRocks92

Boas,

Eu estou a tentar criar um sistema de login mas estou com problemas em criar a $_SESSION.

Este o meu index.php (form login):

<?php
include("../require/db_connection.php");
include("top_foot/header.php");
if(isset($_GET['msg'])){
if($_GET['msg']=="error"){
echo ' <span class="notification n-error" style="margin:auto; margin-top:2%; width:50%;">Error!! Wrong username or password!!</span>';
}
elseif($_GET['msg']=="logout"){
echo ' <span class="notification n-success" style="margin:auto; margin-top:2%; width:50%;">Logged out successfully!!</span>';
}
}
?>
<form method="post" action="login.php" style="margin-top:7%; margin-left:25%;">
<table id="login_form" width="100%" border="0" align="center" cellpadding="4" cellspacing="4">
<tr>
<td>
<!--<img src="../images/" alt="" title="" />-->
</td>
</tr>

<tr>
<td width="20%" align="right" valign="middle">Username</td>
<td width="3%" align="left" valign="middle">:</td>
<td width="77%" align="left" valign="middle">
<label>
 <input name="login" type="text" style="width:200px; height:15px; padding:5px;" />
</label>
</td>
</tr>

<tr>
<td align="right" valign="middle">Password</td>
<td align="left" valign="middle">:</td>
<td align="left" valign="middle">
<label>
 <input name="password" type="password" style="width:202px; height:15px; padding:5px;" />
</label>
</td>
</tr>

<tr>
<td align="left" valign="middle"> </td>
<td align="left" valign="middle"> </td>
<td align="left" valign="middle">
<label>
 <input type="submit" name="button" id="button" value="Login" />
</label>
</td>
</tr>
</table>
</form>
<?php
include("top_foot/footer.php");
?>

login.php

<?php
ob_start();
require('../require/db_connection.php');
// Define $myusername and $mypassword
$login = $_POST['login'];
$password = $_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = mysql_real_escape_string($login);
$mypassword = sha1($password);

$sql="SELECT * FROM admin WHERE admin_user='".$myusername."' and admin_password='".$mypassword."'";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_assoc($result);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
if($count==1){
$_SESSION['user']=$rows['admin_user'];
header("location:pages/dashboard.php");
}
else {
header("location:index.php?msg=error");
}
ob_end_flush();
?>

dashboard.php (se login estiver correcto vem para esta pagina)

<?php
session_start();
if(!$_SESSION['user']){
echo $_SESSION['user']; //da erro nesta linha, nao conhece 'user'
die();
}

include('top_foot/header.php');
?>

<!-- Header. Main part -->
<div id="header-main">
<div class="container_12">
<div class="grid_12">
<div id="logo">
 <ul id="nav">
 <li id="current"><a href="">Dashboard</a></li>
 <li><a href="">Users</a></li>
 <li><a href="">Banners</a></li>
 <li><a href="">Bands</a></li>
 <li><a href="">News</a></li>
<!-- CONTINUA (....) -->

Cumprimentos

Share this post


Link to post
Share on other sites
pmg

Tens que usar session_start() em TODAS as paginas que usem variaveis de sessao!

Isso inlcui, para o teu exemplo, a pagina login.php (nao precisas do session_start() em index.php).

Edited by pmg

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.