Sessions?

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
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Sessions?

Post by ScottCFR »

For some reason, when I login to the admin panel I've made it won't let the session log anymore. It worked fine a few days ago..

How I set the var:

$_SESSION['logged'] = 1;

How I check the var:

Code: Select all

<?php 
session_start(); 
if(!isset($_SESSION['logged'])) { 
    header("Location: index.php"); 
} 
?>
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Sessions?

Post by JakeJ »

Are you getting an error message? Maybe the session variable is the problem and not the session in general. We need more info.
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Re: Sessions?

Post by ScottCFR »

No, it just simply redirects me to index.php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Sessions?

Post by McInfo »

ScottCFR wrote:it won't let the session log anymore.
Please rephrase that.
ScottCFR wrote:it just simply redirects me to index.php.
What are you expecting to happen?
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Re: Sessions?

Post by ScottCFR »

The idea is, if logged == 1 then it wont redirect me. But, the session isn't setting.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Sessions?

Post by McInfo »

Where did you set it?
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Re: Sessions?

Post by ScottCFR »

It's in the submit code. Above:

Code: Select all

echo "<p><font color='green'>You have logged in successfully. <a href='/admin/home.php'>Click Here</a> to go to the admin area</a></font></p>"; 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Sessions?

Post by McInfo »

It is very difficult for me to debug code that I can't see.

Do you call session_start() before you set the session variable?
ScottCFR
Forum Commoner
Posts: 33
Joined: Sat Jun 19, 2010 7:36 pm

Re: Sessions?

Post by ScottCFR »

That was my problem. I didn't realize I had to start the session there.

Thanks,
Scott
jbarner
Forum Newbie
Posts: 3
Joined: Thu Sep 02, 2010 7:02 am

Re: Sessions?

Post by jbarner »

so, I'm having almost the opposite problem.

I've set up almost exactly what you've done here, however, I'm unsure if this is secure, because, when I am on the page, and I refresh, no problem. it redirects me back to the page that requires a login. however, even if I then enter it wrong and get directed to the site index, if i type the url of the original page directly, Boom, i'm in. what gives?

here's my page code:

Code: Select all

<?php
session_start();
if (empty($_SESSION['logged'])) {
  header('Location: passlist.php');
  exit();
}
 session_unset(); 
 
 session_destroy();


include("dbinfo.inc.php");
mysql_connect($servname,$dbusername,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM ($newdbname) ";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

$i=0;
while ($i < $num) {
$user_id=mysql_result($result, $i, "user_id");
$username=mysql_result($result,$i,"username");
$password=mysql_result($result,$i,"password");
echo "$username, $password, $user_id";
?>

<form action="updated.php">
<input type="hidden" name="user_id" value="<?php echo "$user_id"; ?>">
Username: <input type="text" name="username" value="<?php echo "$username"?>"><br>
Password: <input type="text" name="password" value="<?php echo "$password"?>"><br>
<input type="Submit" value="Update" name="submit">
</form>
<?php
++$i;
}
  
?>
what gives?
jbarner
Forum Newbie
Posts: 3
Joined: Thu Sep 02, 2010 7:02 am

Re: Sessions?

Post by jbarner »

oh, and I just noticed the little difference in the two scripts, and tried replacing

Code: Select all

if (empty($_SESSION['logged'])) {
with

Code: Select all

if (!isset($_SESSION['logged'])) {
to no avail.
john4u
Forum Newbie
Posts: 3
Joined: Sat Sep 04, 2010 2:45 pm

Re: Sessions?

Post by john4u »

Hi,

you can use this

<?php
session_start();
if(empty($_SESSION['logged'])) {
echo "<script>window.location.href='index.php'</script>";
}
?>


so is the user is not mlogged in and try to access a page directly hge willl be directed to index page to login.

Thanks,
John
speed.prateek@gmail.com
john4u
Forum Newbie
Posts: 3
Joined: Sat Sep 04, 2010 2:45 pm

Re: Sessions?

Post by john4u »

Hi,

I am 100% confident it will work

<?php
session_start();
if(empty($_SESSION['logged'])) {
echo "<script>window.location.href='index.php'</script>";
}
?>

Thanks,
John
speed.prateek@gmail.com
Post Reply