My Sessions not working

My Sessions not working

Page 1 of 1

8 Replies - 1340 Views - Last Post: 16 April 2009 - 07:06 AM Rate Topic: -----

#1 colorodo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-February 09

My Sessions not working

Posted 15 April 2009 - 12:35 PM

I am not sure whats going on and now am getting frustrated.. Need help with preventing users from accessing specific pages. Heres the code for my checklogin.php page

<?php
session_start();
  if(isset($_SESSION["myusername"]))
  
  {
	 header('Location: newheader.php');
  }
  else
  {
	header('Location: main_login.php');
  }
  ?>



Here is my newheader.php page that I want protect unless they are signed in..

<?php
session_start();
  if(isset($_SESSION['myusername']))
  
  {
	 header('Location: main_login.php');
  
  else
  {
	header('Location: main_login.php');
  }

  ?>



I have a few pages I need protected and just can't figure out what I am doing wrong
Thanks Sincerely Frustrated

This post has been edited by colorodo13: 15 April 2009 - 12:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: My Sessions not working

#2 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: My Sessions not working

Posted 15 April 2009 - 12:42 PM

ok i dont know what your problem is as you havent explained it properly

however. in your login make a session

$_SESSION['loggedIn'] = 'true';

then at the top of each of your pages you want protecting put this

if($_SESSION['loggedIn'] != 'true'){
  header("location: main_login.php");
}

Was This Post Helpful? 0
  • +
  • -

#3 colorodo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-February 09

Re: My Sessions not working

Posted 15 April 2009 - 01:26 PM

View Postcolorodo13, on 15 Apr, 2009 - 11:35 AM, said:

I am not sure whats going on and now am getting frustrated.. Need help with preventing users from accessing specific pages. Heres the code for my checklogin.php page

<?php
session_start();
  if(isset($_SESSION["myusername"]))
  
  {
	 header('Location: newheader.php');
  }
  else
  {
	header('Location: main_login.php');
  }
  ?>



Here is my newheader.php page that I want protect unless they are signed in..

<?php
session_start();
  if(isset($_SESSION['myusername']))
  
  {
	 header('Location: main_login.php');
  
  else
  {
	header('Location: main_login.php');
  }

  ?>



I have a few pages I need protected and just can't figure out what I am doing wrong
Thanks Sincerely Frustrated

Was This Post Helpful? 0
  • +
  • -

#4 colorodo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-February 09

Re: My Sessions not working

Posted 15 April 2009 - 01:34 PM

Well what I want is for it to check that the user has signed in using the username store in the mysql database. The reply has not made a difference in protecting the pages that i want protected. I have a login page that checks the checklogin.php page to make sure they are a member. If they are not a member I do not want them to see any more of the site. Here is my full code that has the session start and the database info.
[CODE]
<?php
$_SESSION['loggedIn'] = 'true';


//echo $_POST['myusername']." - ". $_POST['mypassword']."<br>";


$host="db770.perfora.net"; // Host name
$username="example"; // Mysql username
$password="example"; // Mysql password
$db_name="example"; // Database name
$tbl_name="Registration"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE user='".$myusername."' and password='".$mypassword."'";
//echo $sql."<br>";

$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

//echo $count."<<<<< the number of rows";
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION["myusername"]=$myusername;
$_SESSION["mypassword"]=$mypassword;
header('Location: newheader.php');
}
else {
echo "Your user name or password is incorrect. Please try again<br>";
echo "<a href=registration_form.html target=mainFrame>Become a Member</a>";

}
?>

Then here is an example of the page I want protected newheader.php
[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
if($_SESSION['loggedIn'] != 'true'){
header("location: main_login.php");
}

?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>GlobalConferencing.net &quot;The Videoconferencing Database&quot;</title>
</head>

<frameset rows="188,*" cols="*" framespacing="1" frameborder="yes" border="1" bordercolor="#009966">
<frame src="header13.html" name="topFrame" scrolling="No" noresize="noresize" id="topFrame" />
<frame src="main1.php" name="mainFrame" id="mainFrame" />
</frameset>
<noframes><body>
</body>
</noframes></html>
[CODE]

I hope someone can help fix whats wrong......
Was This Post Helpful? 0
  • +
  • -

#5 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: My Sessions not working

Posted 15 April 2009 - 01:40 PM

try putin the session stuff at the top of page and just above it pu session_start();
Was This Post Helpful? 0
  • +
  • -

#6 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: My Sessions not working

Posted 15 April 2009 - 01:41 PM

right here

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION["myusername"]=$myusername;
$_SESSION["mypassword"]=$mypassword;
header('Location: newheader.php');



what he suggested is adding a session var like

$_SESSION['logged_in'] = true;



and at the top of every page you want protected use

// if true continues if false redirects....
if (!$_SESSION['logged_in']) {  
   header("Location: main_login.php");
}



that would be the cleanest way is using a boolean variable
Was This Post Helpful? 0
  • +
  • -

#7 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: My Sessions not working

Posted 15 April 2009 - 06:02 PM

anytime you leader a header you must register the session or it goes poof.

i actually forget if before or after, php.net seesion_register (or one the session functions) is user notes on this exact topic.


$_SESSION["myusername"]=$myusername;
$_SESSION["mypassword"]=$mypassword;
session_register('myusername');
session_register('mypassword');
header('Location: newheader.php');



Was This Post Helpful? 0
  • +
  • -

#8 colorodo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-February 09

Re: My Sessions not working

Posted 16 April 2009 - 06:12 AM

Do I need to put a session_start(); before this code
[CODE]
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['logged_in'] = true;
$_SESSION["myusername"]=$myusername;
$_SESSION["mypassword"]=$mypassword;
header('Location: newheader.php');
[CODE]
Was This Post Helpful? 0
  • +
  • -

#9 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: My Sessions not working

Posted 16 April 2009 - 07:06 AM

just FYI

Quote

php.net says

session_register()

Warning

This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.


all you should need is the session_start() at the head of your file... it needs to be at the VERY beginning of every file that you want sessions available...

This post has been edited by RPGonzo: 16 April 2009 - 07:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1