943,706 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1018
  • PHP RSS
May 23rd, 2008
0

Help me understand sessions....please

Expand Post »
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?
Similar Threads
Reputation Points: 48
Solved Threads: 1
Posting Pro
maydhyam is offline Offline
555 posts
since Feb 2008
May 23rd, 2008
0

Re: Help me understand sessions....please

you need to add a session checker to everything...

pseudocode:
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 18
Solved Threads: 5
Light Poster
rgviza is offline Offline
31 posts
since May 2008
May 26th, 2008
0

Re: Help me understand sessions....please

Hi,

Is the following piece of code what you are talking about?
PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 48
Solved Threads: 1
Posting Pro
maydhyam is offline Offline
555 posts
since Feb 2008
May 26th, 2008
0

Re: Help me understand sessions....please

I used the following code:
php Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. if (empty($_SESSION['username']))
  4. {
  5. header("location:index.php");
  6. exit;
  7. }
  8. ?>
Vai
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
Vai is offline Offline
74 posts
since Jan 2008
May 26th, 2008
0

Re: Help me understand sessions....please

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)...
Reputation Points: 48
Solved Threads: 1
Posting Pro
maydhyam is offline Offline
555 posts
since Feb 2008
May 26th, 2008
1

Re: Help me understand sessions....please

Here is what I suggest:

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

PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. require_once('secure.php');
  3. ?>


Click to Expand / Collapse  Quote originally posted by maydhyam ...
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)...
Vai
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
Vai is offline Offline
74 posts
since Jan 2008
May 26th, 2008
0

Re: Help me understand sessions....please

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"?
Reputation Points: 48
Solved Threads: 1
Posting Pro
maydhyam is offline Offline
555 posts
since Feb 2008
May 26th, 2008
0

Re: Help me understand sessions....please

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
Reputation Points: 48
Solved Threads: 1
Posting Pro
maydhyam is offline Offline
555 posts
since Feb 2008
May 26th, 2008
0

Re: Help me understand sessions....please

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
Reputation Points: 48
Solved Threads: 1
Posting Pro
maydhyam is offline Offline
555 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Display content based on login information
Next Thread in PHP Forum Timeline: How to get rid of frames





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC