Help me understand sessions....please

Thread Solved

Join Date: Feb 2008
Posts: 478
Reputation: maydhyam is an unknown quantity at this point 
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Help me understand sessions....please

 
0
  #1
May 23rd, 2008
How do I set the session on a page, so that if I were to type the direct link for the page into the address bar, it doesn't display, instead the user would be redirected to the homepage or login page?
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 31
Reputation: rgviza is an unknown quantity at this point 
Solved Threads: 5
rgviza rgviza is offline Offline
Light Poster

Re: Help me understand sessions....please

 
0
  #2
May 23rd, 2008
you need to add a session checker to everything...

pseudocode:
  1. if([user is not logged in])
  2. {
  3. header("Location: /login.php\r\n");
  4. }

Determining what an authenticated session is is more or less unique to every implementation so that check depends on how you define "valid". By default, I'm pretty sure php always starts a session if autostart is set, so simply checking for a started session is no good. You need to look at something that only authenticated users have in their session or you are just slowing an attacker down.

-r
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 478
Reputation: maydhyam is an unknown quantity at this point 
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Help me understand sessions....please

 
0
  #3
May 26th, 2008
Hi,

Is the following piece of code what you are talking about?
  1. <?php
  2. session_start();
  3. include('./connect.php');
  4. if (isset($_SESSION['username']))
  5. {
  6. $name = $_SESSION['username'];
  7. header("Location: DatabaseRecorder.php");
  8. }
  9. ?>
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 74
Reputation: Vai is an unknown quantity at this point 
Solved Threads: 5
Vai Vai is offline Offline
Junior Poster in Training

Re: Help me understand sessions....please

 
0
  #4
May 26th, 2008
I used the following code:
  1. <?php
  2. session_start();
  3. if (empty($_SESSION['username']))
  4. {
  5. header("location:index.php");
  6. exit;
  7. }
  8. ?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 478
Reputation: maydhyam is an unknown quantity at this point 
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Help me understand sessions....please

 
0
  #5
May 26th, 2008
here is what i have on my login page...:


[ICODE=php]$_SESSION['username'] = $_POST['user'];
$_SESSION['password'] = $md5pass;
logininfo();
header("Location: apHome.php"); [/ICODE=php]

here is what i have on my home page...:

[code=php]
<?php
session_start();
include('./connect.php');
if (isset($_SESSION['username']))
{
$name = $_SESSION['username'];
}
?>
[/code=php]

i have other pages linked from this homepage, and i want to include them in this session ( so that even if i type the direct link in the address bar, i would ot be able to access the page)...
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 74
Reputation: Vai is an unknown quantity at this point 
Solved Threads: 5
Vai Vai is offline Offline
Junior Poster in Training

Re: Help me understand sessions....please

 
1
  #6
May 26th, 2008
Here is what I suggest:

Create a page called secure.php
add the following code as it is: ONLY change " your_login_page "

  1. <?php
  2. session_start();
  3. if (empty($_SESSION['username']))
  4. {
  5. header("location:your_login_page.php");
  6. exit;
  7. }
  8. ?>

Then in your other pages that you want secure, add:
  1. <?php
  2. require_once('secure.php');
  3. ?>


Originally Posted by maydhyam View Post
here is what i have on my login page...:


[ICODE=php]$_SESSION['username'] = $_POST['user'];
$_SESSION['password'] = $md5pass;
logininfo();
header("Location: apHome.php"); [/ICODE=php]

here is what i have on my home page...:

[code=php]
<?php
session_start();
include('./connect.php');
if (isset($_SESSION['username']))
{
$name = $_SESSION['username'];
}
?>
[/code=php]

i have other pages linked from this homepage, and i want to include them in this session ( so that even if i type the direct link in the address bar, i would ot be able to access the page)...
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 478
Reputation: maydhyam is an unknown quantity at this point 
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Help me understand sessions....please

 
0
  #7
May 26th, 2008
So I created secure.php and also in my apHome.php script, i added the code snippet you told me to...and when i typed the direct link into the address bar to open apHome.php....I am getting the "Internet Explorer cannot display the webpage" message..so this is how it is done? the hiding/protection of the page "apHome.php"?
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 478
Reputation: maydhyam is an unknown quantity at this point 
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Help me understand sessions....please

 
0
  #8
May 26th, 2008
Hey,

I typed the incorrect page name in the header....I corrected it, and now it works...I just want to make sure and enter it into my other pages, and verify that they all work...
I'll keep you posted...
May
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 478
Reputation: maydhyam is an unknown quantity at this point 
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Help me understand sessions....please

 
0
  #9
May 26th, 2008
Hey Vai,

So I put the piece of code into all my pages, and they worked nicely...I now understand how to set the sessions for my application....Thanks...

May
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC