Jump to content

Passing Login Info To 2Nd Page, And Echoing Statement Accordingly


davidcriniti

Recommended Posts

Hi,

 

I've been working on a website for both students and teachers at my school, and I would like to preface this post by saying that I'm a teacher who is just trying to teach himself a bit of php / mysql, so please bear that in mind if I ask questions you may regard as basic.

 

I've got:

- some pages which are open to the public,

- some which are available to anyone logged in (be they students or teachers), and

- some which are available to teachers only.

 

Teachers and students all log in on the same page. If someone goes to a page which requires a log in, I redirect them to the login page (login_required.php) with code like this:

 

<?php
session_start();
if (empty($_SESSION['userName'])  || $_SESSION['member_type'] == 'Student' )  {
header("Location: login_required.php");
die();
} ?>

 

The above code is for a page that requires users to be logged in as a teacher to access.

 

Once this re-direct to login_required.php has occured, is there any way for the information to be passed to the login_required.php page that this person has come from a page which requires a teacher log in, and echo the message "The page you tried to access requires you to log in as a teacher. Please do so now."

 

...and similarly, if a person comes from another page which simply requires them to be logged in (as either student or teacher), such as the following:

 

 

<?php
session_start();
if (empty($_SESSION['userName']) )  {
header("Location: login_required.php");
die();
} ?>

 

...the message would say "The page you tried to access requires you to be logged in. Please do so now".

 

Any advice on how to achieve the above would be very gratefully appreciated.

 

Thanks for your time,

 

Dave

Link to comment
Share on other sites

Thanks for your quick response Jessica. I did it (following your advice) and it seems to have worked from the tests I've done so far.

 

Might just post what I've done in case others may find it useful. If there's anything that's a bit strange about my coding, please feel free to let me know too.

 

On the page that requires users to be logged in as a teacher, I changed the code (from what it was in my initial post) to:

 

<?php
session_start();
if (empty($_SESSION['userName'])  || $_SESSION['member_type'] == 'Student' )  {


/* New code inserted below */

$_SESSION['teacher_required'] = 'You must be a teacher to access this page';
/* End new code inserted */

header("Location: login_required.php");
die();
} ?>

 

Then on the login_required.php page I added the code:

 

  <?php

 echo $_SESSION['teacher_required'];

 $_SESSION['teacher_required'] = "";

 ?>

 

Thanks again Jessica.

 

Cheers,

 

Dave

Link to comment
Share on other sites

Thanks for the tips Jessica.

 

When you say change the key to 'msg', am I correct in assuming you are referring to the session name?

 

I've played around and manged to use unset and it seems to work (Never used that before, so thanks!)

 

Can you give me any tip on how to check isset() ?

 

This is the code I now have on the login_required.php page:

 

    <?php

	 echo $_SESSION['msg'];

    unset ($_SESSION['msg'] )

	 ?>

 

Thanks again,

 

Dave

Link to comment
Share on other sites

Ok,

 

This is the latest code I have:

 

		  <?php
if (isset($_SESSION['msg'])  )  { 

	 echo $_SESSION['msg'];

    unset ($_SESSION['msg'] );
}

	 ?>

 

 

 

From what I can tell, it works the same as without the "if (isset($_SESSION['msg']) )" line, however, I gather from your advice that there is a good reason for using the isset. What would this be?

 

I looked at the manual here:

http://au1.php.net/manual/en/function.isset.php

 

...and know it's about determing if the variable (or session in this case) has been set, but would be grateful to get my head around why it is important when it seems to work without it.

 

Thanks for all your help,

 

Dave

Link to comment
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.