Page 1 of 1

$_session not working

Posted: Fri Apr 20, 2012 4:55 am
by adhi91
Hi,

I am doing a simple login where user fill up form using username password email and on it will be directed to another page where they can see their username.
I have succesfully done this on a wamp server
Below is my code

Code: Select all

<?php

$server="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test_db"; // Database name
$tbl_name="test"; // Table name

//Connect to server
mysql_connect("$server", "$username", "$password")or die("cannot connect to server");
//Connect to database
mysql_select_db("$db_name")or die("cannot select database");

if(!isset($_POST['username']) && !isset($_POST['password']) && !isset($_POST['email']))
{
    die('Error : Please complete the login form');
}

// username and password sent from form
$membername=$_POST['username'];
$memberpass=$_POST['password'];
$memberpass=md5($memberpass);
$memberemail=$_POST['email'];

// To protect MySQL injection
$membername = stripslashes($membername);
$memberpass = stripslashes($memberpass);
$memberemail= stripslashes($memberemail);
$membername = mysql_real_escape_string($membername);
$memberpass = mysql_real_escape_string($memberpass);
$memberemail= mysql_real_escape_string($memberemail);

$sql="SELECT * FROM $tbl_name WHERE username='$membername' and password='$memberpass' and email='$memberemail'";
$result=mysql_query($sql);
$db_field=mysql_fetch_assoc($result);
$userid=$db_field['id'];


$count=mysql_num_rows($result);

if($count==1){
session_register("membername");
session_register("memberpass");
session_register("memberemail");
session_register("userid");
header("location:index.php");
}
else 
{
echo "Login Failed<br>Please check your username and password";
}
?>
and the next page is

Code: Select all

//LOGIN
ob_start();
session_start();
if(!isset($_SESSION['membername']))
{
header("location:login.html");
}
ob_flush();
//END OF LOGIN
echo "Hi, ". $_SESSION['membername'];
echo "<br><a href='logout.php'>Log Out</a>";
Now, I am moving to a ubuntu server.
and

Code: Select all

if(!isset($_SESSION['membername']))
{
header("location:login.html");
}
ob_flush();
this statement always making sure that I can't go to the next page.
$_SESSION['membername'] is always empty.
If I delete this statement, I can enter.
Why is it so? What am I missing?
Thanks in advance

Re: $_session not working

Posted: Fri Apr 20, 2012 7:57 am
by social_experiment

Code: Select all

<?php
// try
 $_SESSION['membername'] = $membername;
// etc.
// instead of
session_register("membername");
?>
Have a look at this url for more information
viewtopic.php?f=28&t=135287

Re: $_session not working

Posted: Sun Apr 22, 2012 11:49 pm
by adhi91
It try using $_SESSION and it is still not working.
I can't view the next page.

I still need to comment on
if(!isset($_SESSION['membername'])
in order to access the next page.

How can I fix this?
Is this configuration issue because I move from wamp to ubuntu?

Re: $_session not working

Posted: Mon Apr 23, 2012 12:21 am
by requinix
Where's the session_start() in the first file?

Re: $_session not working

Posted: Mon Apr 23, 2012 12:43 am
by adhi91
Thanks. That has solved the problem.