$_session not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adhi91
Forum Newbie
Posts: 22
Joined: Wed Apr 04, 2012 8:54 pm

$_session not working

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: $_session not working

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
adhi91
Forum Newbie
Posts: 22
Joined: Wed Apr 04, 2012 8:54 pm

Re: $_session not working

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: $_session not working

Post by requinix »

Where's the session_start() in the first file?
adhi91
Forum Newbie
Posts: 22
Joined: Wed Apr 04, 2012 8:54 pm

Re: $_session not working

Post by adhi91 »

Thanks. That has solved the problem.
Post Reply