Jump to content

Recommended Posts

Hi, I have a session problem, I have a login and a page that I only want to be viewable if logged in.

 

Login_process.php - Session Part


if (($fieldcheck1 == 1) || ($fieldcheck2 = 1))
{

	session_start();
	session_register("login"); //set a variable for use later
	$_SESSION['username'] = $_POST['username'];
	$_SESSION['main_username'] = $_POST['username'];
	$id = session_id(); //let's grab the session ID for those who don't have cookies
	$url = "Location: login_done.php?sid=" . $id;
	header($url);
	//header("Location: log_done.php");
	mysql_close($connect);
	Die();

}

 

 

Cpanel.php

<?php
session_start ();
include("../include/config.php");
include("../include/header.php");


if (! session_is_registered ("login") ) //if your variable isn't there, then the session must not be
{
session_unset (); //so lets destroy whatever session there was and bring them to login page
session_destroy ();
echo ("You need to login to access this page!");

}
else //otherwise, they can see the page
{

 

 

Once I have logged in, I then go to cpanel.php and get this:

You need to login to access this page!

 

I don't understand because I am logged in?

 

 

Cheers

Sean Preston

Link to comment
https://forums.phpfreaks.com/topic/101678-sessions-problem/
Share on other sites

Well first of all the session_start() should be at the top of the page.

Second, why don't you use a session variable to find if someone's logged in?

Like this:

session_start();
if (($fieldcheck1 == 1) || ($fieldcheck2 = 1))
{
	$_SESSION['loggedin'] = 1; //set a variable for use later
	$_SESSION['username'] = $_POST['username'];
	$_SESSION['main_username'] = $_POST['username'];
	$id = session_id(); //let's grab the session ID for those who don't have cookies
	$url = "Location: login_done.php?sid=" . $id;
	header($url);
	//header("Location: log_done.php");
	mysql_close($connect);
	Die();

}

 

And then use an if statement to check if they're logged in like this:

session_start ();
include("../include/config.php");
include("../include/header.php");


if ($_SESSION['loggedin'] != "1") //if your variable isn't there, then the session must not be
{
session_destroy();
echo ("You need to login to access this page!");

}
else //otherwise, they can see the page
{

 

And you only need session_destroy() because it completly destroys the session.

Link to comment
https://forums.phpfreaks.com/topic/101678-sessions-problem/#findComment-520228
Share on other sites

OMG, I have been trying to fix this for 2/3 days now, Here is the full code for both pages (cpanel.php & login_process.php).

 

 

login_process.php

http://rafb.net/p/aJa3JZ93.html

 

cpanel.php

http://rafb.net/p/yX2heL23.html

 

 

Please, Please somebody help me.

Cheers

Sean

Link to comment
https://forums.phpfreaks.com/topic/101678-sessions-problem/#findComment-521962
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.