Separate landing pages ... using session variables, cookies, or what ?

0 down vote favorite

This is probably a dumb question. I have a Web site that uses php and html with a bit of Javascript. I am trying to set it up so there are multiple landing pages. I think it would work if all the remaining .php files on the site were kept identical but there were separate index_a.php, index_b.php files, etc; there would need to be only slight differences between these. The only problem is when the user clicks “Home” they of course get the root index.php. Is there a way to store the name (or some other indication) of what the user’s landing page was for that session (using PHP session variables or I don’t know what) and have the user directed to that page again when they click Home ?

Any help would be much appreciated (keeping in my mind I am a relative newbie and any solution would need to be pretty simple and safe to load on a server). Any straightforward way way to do this ? It must be something that is fairly commonly required.

No problem, as long as you are using sessions you can store anything you like in the $_SESSION global array.

So when a user logins just do something like this

[php]if ($loginIsOk) {
$_SESSION[‘user’] = $user; // you probably do something like this already to know the user is logged in
$_SESSION[‘landing’] = ‘index_b’; // whatever logic to select the landing page could go here
}[/php]

As long as you use session on all pages it will work like this

[ol][li]User hits yourpage.com - included in the request is browser info, cookies, etc[/li]
[li]session_start() fires up the session system[/li]
[li]If the user has a PHPSESSID cookie, this id is used to load the users session data into $_SESSION[/li]
[li]If not then the user is assigned an ID and a PHPSESSID cookie is sent back to be used in future requests[/li][/ol]

so.

On login, set some session param (call it whatever you like) to hold your custom index id

[php]$_SESSION[‘landing’] = ‘index_b’;[/php]

On the home page, check your session param, if set, then show the custom page, if not, show some default page.

[php]$landingPage = isset($_SESSION[‘landing’]) ? $_SESSION[‘landing’] : ‘index_default’;[/php]

[hr]

Now the big question is, do you really need separate files? Since we know which user is logged in we can do small variations in a single file.

example:

[php]

<?php if(isset($_SESSION['user'])) { ?>
<span>Welcome back <?= $_SESSION['user']['name'] ?></span>
<?php } else { ?>
<span>Click <a href="/login">here</a> to login</span>
<?php } ?>
[/php]

That sounds great … no doubt it will take me a while to play with it since there are a ton of other things on my plate. Will post again if I hit a brick wall.

thanks for your help !

Ok, why is this not working ?

I have a header file (header.php) that runs all the menus and is included in the various index*.php files, at the top. Also a footer.php file, included in the same way.

I put

<?php session_start () ?>

at the start of header.php and also footer.php

Right after it, in header.php only, there is:

<?php $landingpage = isset($_SESSION["landingpage"]) ? $_SESSION["landingpage"] : "index.php" ; ?>

So in index.php, just after the , we have now got

<?php include "header.php"; ?> <?php $_SESSION["landingpage"] = "index.php"; ?> <?php $landingpage = isset($_SESSION["landingpage"]) ? $_SESSION["landingpage"] : "index.php" ; ?>

In the others, say index_a.php, we have, in place of this,

<?php include 'header.php'; ?> <?php $_SESSION["landingpage"] = "index_a.php"; ?> <?php $landingpage = isset($_SESSION["landingpage"]) ? $_SESSION["landingpage"] : "index.php" ; ?>

In header.php, at the point where the user clicks “Home”, we have (omitting irrelevant parameters)

<?php if ($landingpage = "index.php") : ?> <?php elseif ($landingpage = "index_a.php") : ?> <?php endif ?>

This does not work; i. e. if I start off and run index_a.php and in it I immediately click “Home”, I get index.php, notindex_a.php.

What am I missing here ?

Thanks for your help,

Charles S.

You only need session_start() on the file that is opened by the user. So if the user opens index_a.php it should look like this:

[php]<?php
session_start();

//stuff

require ‘header.php’;
require ‘footer.php’;[/php]

Header and footer will never we directly opened by the user, and does not need session_start(). See it as it should be the first (or of the first) lines that is run on a request.

[hr]

Right after it, in header.php only, there is: <?php $_SESSION["landingpage"] = "index.php"; ?>

This means that every time the user hits this page the session variable we’re trusting to hold the index page of the user is reset to the default “index.php”. You should not assign this on every request, assign it after user login. I’m not sure how you figure out which user should have what index page, but on login you at least know which user it is. Then set the session variable.

[hr]

Don’t need to start and stop php tags everywhere, you can easily do this

[php]some title

<?php include "header.php"; $landingpage = isset($_SESSION["landingpage"]) ? $_SESSION["landingpage"] : "index.php"; ?> ...[/php]

[hr]

You don’t need this:

[php]<?php if ($landingpage = "index.php") : ?>

<?php elseif ($landingpage = "index_a.php") : ?> <?php endif ?> [/php]

As long as we know the $landingpage variable holds a valid value we can just echo it. We verify that this variable is ok here:

[php]$landingpage = isset($_SESSION[“landingpage”]) ? $_SESSION[“landingpage”] : “index.php” ;[/php]

So just do this:

[php][/php]

[hr]

It should work, as long as you set $_SESSION[‘landingpage’] on user login

Thanks much for all the help.

You can see I am new at this. The simplifications to the code and reducing unnecessary tags are helpful and I will put those in once I get the darn thing to work. Which at the moment I can’t, for the life of me.

So: at the moment, neither header.php not footer.php do any testing or setting of $landingpage on entry. They still need to test the value to execute the

index.php has the following at startup:

  <?php
  $landingpage = isset($_SESSION["landingpage"]) ? $_SESSION["landingpage"] : "index.php" ; 
  include "header.php"; 
  ?>

There is no login or anything like that. I assume that generally people will arrive at index.php since that is where we are mostly listed at, but for special cases we want to make available index_a.php, index_b.php, etc. People who enter with those should continue to see index_a.php, index_b.php so long as they continue to navigate the site. They should not see index.php. Should be simple I guess.

If that is the case, then both index_a.php and index.php should contain session_start() … if I am understanding this correctly … since a user will never execute both, only one or the other, in a single session.

index_a.php has the following at startup:

  <?php
  session_start();
  $_SESSION["landingpage"] = "index_a.php"; 
  $landingpage = isset($_SESSION["landingpage"]) ? $_SESSION["landingpage"] : "index_a.php" ; 
  include "header.php"; 
  ?>     

index.php has the following at startup:

  <?php
  $landingpage = isset($_SESSION["landingpage"]) ? $_SESSION["landingpage"] : "index.php" ; 
  include "header.php"; 
  ?>

We’ll assume at this point that all we want to do is get it to work for the user who enters the system at index_a.php.

So: we execute index_a.php, then click home. We still end up getting index.php. The actual selector, which is in header.php, is still as it was in my previous post; I have not yet tried to simplify it, but it should work as is, no ?

What am I STILL missing ? Feeling like an idiot.

when you do this

[php]<?php
session_start();
$a = ‘foo’;

require ‘header.php’;
require ‘footer.php’;[/php]

then the session system (and global array), and the variable $a is available in the current file, AND in header.php and footer.php

This is why you are starting the session system on every file the user will visit / every entry point to your application. You do not need to start it in every included file, nor do you have to re-set variables etc.

[hr]

It helped a lot to get a bit more info on this (no login, different entry points where you want to save that entry point, etc)

So let’s do this:

index.php
[php]<?php
session_start();

if (!isset($_SESSION[‘landingpage’])) {
// no landing page set, set this one as the one to use
$_SESSION[‘landingpage’] = ‘index.php’;
}

include “header.php”;
?>
Home[/php]

index_a.php
[php]<?php
session_start();

if (!isset($_SESSION[‘landingpage’])) {
// no landing page set, set this one as the one to use
$_SESSION[‘landingpage’] = ‘index_a.php’;
}

include “header.php”;
?>
Home[/php]

index_b.php
[php]<?php
session_start();

if (!isset($_SESSION[‘landingpage’])) {
// no landing page set, set this one as the one to use
$_SESSION[‘landingpage’] = ‘index_b.php’;
}

include “header.php”;
?>
Home[/php]

Note the new “if isset” in there, what this does is to set the session landingpage variable to a value if it’s not already set. Hence we no longer need the ternary operator check to see if we have a valid landing page in session.

Got it; will try it out and let you know how it goes. The help is much appreciated.

Thanks
Charles S

Ok, this is getting to be a real pain.
So we have the two files, say, index.php and index_a.php
At the top of each file there is

<!DOCTYPE... <html lang...

and then immediately (in index_a.php):

  <?php
  session_start();
  if (!isset($_SESSION["landingpage"]))
     {
	 $_SESSION["landingpage"] = "index_a.php";
	 } 
  include "header.php"; 
  ?>  

and (in index.php)

 <?php
  session_start();
  if (!isset($_SESSION["landingpage"]))
     {
	 $_SESSION["landingpage"] = "index.php";
	 } 
  include "header.php"; 
  ?>     

The test in header.php is as follows:

                 <?php if ($_SESSION ["landingpage"] = "index.php") : ?>
                	<a href="index.php">Home</a>	
                 <?php elseif ($_SESSION ["landingpage"] = "index_a.php") : ?>
                     <a href="index_a.php">Home</a>		
                 <?php endif ?>	   

Not elegant, I know, but let me get it to work first.

It doesn’t. If I run index_a.php and then click Home, I still get index.php

This has gotten to be so frustrating that, to get an idea of what is happening, I decided to change the assignment statement in index.php to ALSO assign index_a.php as the landingpage. So index.php is actually NEVER assigned as the landingpage.

And still, regardless of which I run first, I get the same behaviour. Run index_a.php, click Home, get index.php. Run index.php, click Home, get index.php.

There is nothing in the entire site that references “index.php”, other than the above, and footer.php, which should not affect anything because I am not clicking in there when I test.

What is going on here ? Any clues ? Suggestions ?

Thanks so much

Charles S

I think you are making it more painful than it has to be. A mistake many new programmers do is to try to guess what the problem is, which will wear you out real fast.

There is hope though! :slight_smile: You have every opportunity to figure out what the problem is.

Try to do this after session_start() on page
[php]echo ‘

’;
var_dump($_SESSION);[/php]

I guess you will see that the $_SESSION[‘landingpage’] value is ‘index.php’

Why?

Because first you set it to index.php no matter what you visited, and if you haven’t killed the session yet that is what it’s going to use. So either do a session_destroy(), or delete the cookie to get a new session.

[hr]

Which leads me to, you might want another logic behind this.

Do you want it to change the users landing page if he visits another index page after the first one?

[ol][li]user visits index.php[/li]
[li]index.php set as landing page[/li]
[li]user visits index_b.php[/li]
[li]index_b.php set as landing page[/li][/ol]

Because this will not happen atm. If this is the logic you want then remove this if from all index pages

[php] if (!isset($_SESSION[“landingpage”]))
{
$_SESSION[“landingpage”] = “index_a.php”;
} [/php]

–>

[php]$_SESSION[“landingpage”] = “index_a.php”; // the respective file to use as landing page[/php]

For your second question: no, the index page a user gets should always stay the same for a particular session, i. e. if he lands at index_a.php, he should always get index_a.php at that session, never any other.

Have not yet figured it out but have been using your suggestions.

Next installment:

So now we have this at the start of index.php:

      session_start();
  if (!isset($_SESSION["landingpage"]))
         {
	 $_SESSION["landingpage"] = "index.php";
	 } 
      else
  	 {
	 session_destroy();
	 session_start();
	 $_SESSION["landingpage"] = "index.php";
	 }
      include "header.php"; 

and this at the start of index_a.php:

      session_start();
  if (!isset($_SESSION["landingpage"]))
     {
	 $_SESSION["landingpage"] = "index_a.php";
	 }
      else
	 {
	 session_destroy();
	 session_start();
	 $_SESSION["landingpage"] = "index_a.php";
	 }
      include "header.php"; 

I have echoed landingpage at the start of both files, both before and after the if … else … statements, and they are always correct.

Where the value gets overwritten is within header.php. It gets overwritten within the following chunk. I have dumped the values at various points to show where it is correct and incorrect. I have omitted some extraneous parameters, menu options, etc. for simplicity but this is the essential:

       	<div class="col-md-12 header-wrapper">
                  <div class="col-md-9 logo">  

                  <?php 
	      // Value is correct here
                  echo "<pre>";
              var_dump ($_SESSION);
	      ?>             

                  // The following never seems to get executed;  at least, nothing gets echoed.  Unless my syntax is wrong 
                 <?php if ($_SESSION ["landingpage"] = "index.php") : { echo "1"; ?>
                	<a href="index.php">
                    <?php ;} ?>
                    
                 <?php elseif ($_SESSION ["landingpage"] = "index_a.php") : { echo "2"; ?>
                     <a href="index_a.php">
                     <?php ;} ?>                         		
	     <?php endif;

                 // Value is incorrect here (i. e. if it should be index_a.php, it is index.php)
                 echo "<pre>";
             var_dump ($_SESSION);
                 ?>                    

          
                 </div><!-- end of col-md-9 logo -->
                 <div class="col-md-12">
                 	<nav role="navigation" class="navbar navbar-default">
                        <div class="navbar-header">
                            <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                                <span class="sr-only">Toggle navigation</span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                            </button>
                        </div><!-- end of navbar-header-->
                        <div id="navbarCollapse" class="collapse navbar-collapse">
                            <ul class="nav navbar-nav">

                                 <?php 
		   	     // Value is wrong here
                                 echo "<pre>";
                             var_dump ($_SESSION);
			     ?>    
 
                                <?php if ($_SESSION ["landingpage"] = "index.php") : ?>                               
                                   <li id="home"><a href="index.php">Home</a></li>   
                                <?php elseif ($_SESSION ["landingpage"] = "index_a.php") : ?>
                                   <li id="home"><a href="index_a.php">Home</a></li>   
                                 <?php endif ?>	                                        
                               

                                <li class="dropdown" id="dropdown1">
                                	<a data-toggle="dropdown" class="dropdown-toggle" href="#">dropdown1 text<b class="caret"></b></a>
                                    <ul role="menu" class="dropdown-menu">
                                        <li><a href="dropdown_option_1.php">dropdown option 1 text</a></li>
                                        <li><a href="dropdown_option_2.php">dropdown option 2 text </a></li>
                                    </ul><!--end of dropdown-menu -->
                                </li><!-- end of dropdown--> 
   
                                <!-- More dropdown options -->                             
                                                            
                             </ul><!-- end of nav navbar-nav -->
                    	</div><!-- end of navbarCollapse and collapse navbar-collapse -->
                    </nav>
                </div><!-- end of col-md-12 -->
            </div><!--end of col-md-12 header-wrapper -->

Any suggestions ? I have run out of ideas. Of course, I don’t know what I am doing.

Actually I have programmed a fair bit in the past but that is much more years ago than I would care to mention. I am way out of practice, I guess, since that is no longer my daily activity but only something I might do once or twice a year (pressure of other tasks). To show you how long ago, the first languages I learned were FORTRAN and COBOL, LOL ! Much of what I did later was C, Pascal, Basic and (believe it or not) dBaseII.

Did programming in COBOL involve punch cards? I remember doing that…LOL ;D

I wasn’t going to muddy up the process, but have you thought of doing a different approach?

For example if the user was coming from widget_a.php page.

[php]$phpSelf = filter_input(INPUT_SERVER, ‘PHP_SELF’, FILTER_SANITIZE_URL);
$path_parts = pathinfo($phpSelf);
$basename = $path_parts[‘basename’];
$filename = $path_parts[‘filename’];

if ($basename === ‘widget_a.php’) {
header(‘Location: index_a.php?page=apple’);
exit();
}[/php]

then in your utility.php file (or something) maybe something like :
[php]session_start();
if (isset($_GET[‘page’]) && $_GET[‘page’] === ‘apple’) {
$_SESSION[‘landing_page’] = ‘index_a.php’;
}[/php]

or something like that. Like I said I’m not trying to muddy the picture.

in all your ifs/elses you actually set the value instead of comparing it.

= to set values
== to loosely compare values
=== to strictly compare values

[php]$a = 1; // $a is set to 1
var_dump($a == true); // TRUE! 1 is generally considered true, while 0 is considered false.
var_dump($a === true); // false! the integer 1 isn’t technically equal to the boolean value true[/php]

Ah… maybe I have messed up on that. That might explain a lot. A dumb mistake. Will check it out again.

thanks once more

Yes, punch cards. My first year you got a deck of about 1000 cards and sat with the hole punchers and then handed them in at the window to be fed into the mainframe (it was a CDC 6000 I think). Then you came back half an hour later to get your reams of paper output. We envied the grad students who were allow to use actual TERMINALS, wow. With an actual screen. This must have been about 1980.

Your code is way over my head for now but I will have to take a look at it if the other approach does not work. All this is seat of the pants, just need to get the thing done so I can go back to what I am really supposed to be doing (which is not actually programming atm)

Thanks

Getting closer … the problem WAS with using “=” instead of “==”. Dumb newbie mistake.

Now I have this at the start of index.php:

  session_start();
  if (!isset($_SESSION["landingpage"]))
     {
	 $_SESSION["landingpage"] = "index.php";
	 } 
  else
  	 {
	 $_SESSION["landingpage"] = "index.php";
	 }
  include "header.php"; 

and this at the start of index_a.php:

  session_start();
  if (!isset($_SESSION["landingpage"]))
     {
	 $_SESSION["landingpage"] = "index_a.php";
	 }
  else
	 {
	 $_SESSION["landingpage"] = "index_a.php";
	 }
  include "header.php"; 

However, for the select code in header.php, this works ok:

  <?php if ($_SESSION ["landingpage"] == "index.php") : ?>                               
     <li id="home"><a href="index.php">Home</a></li> 
  <?php elseif ($_SESSION ["landingpage"] == "index_a.php") : ?>
     <li id="home"><a href="index_a.php">Home</a></li> 
  <?php endif ?>	 

but this (which you suggested couple days ago):

  <li id="home"><a href="<?php $_SESSION["landingpage"] ?>">Home</a></li>

does not. Does not go anywhere when I click home, just stays on the page it’s at. Any suggestions ?

The second would be much nicer since there are going to be about 20 files of the type index_a.php, index_b.php, etc.

thanks
Charles S

Great :slight_smile:

[hr]

this if/else is either wrong or unnecessary.

if session landingpage is not set set session landingpage to index.php else (session landingpage IS set) set session landingpage to index.php

With the code like this you are basically voiding the if/else as you do the same action no matter what. So instead you could just do

[php]$_SESSION[“landingpage”] = “index.php”;[/php]

This however contradicts what you said before about not wanting the index page to change. So I suggest you change it to this

[php]session_start();
if (!isset($_SESSION[“landingpage”]))
{
$_SESSION[“landingpage”] = “index.php”;
}
include “header.php”;[/php]

[hr]

It was not what I suggested :wink:

I used a shorthand PHP echo, which is written like this:

[php]

  • <a href="<?= $_SESSION["landingpage"] ?>">Home
  • [/php]

    It’s the exact same as this

    [php]

  • <a href="<?php echo $_SESSION["landingpage"] ?>">Home
  • [/php]

    Just looks nicer and fits well with outputting data / templating :slight_smile:

    With the code as you have it you are not echoing the landing page, which means the href attribute is an empty string and you go nowhere when clicking it :slight_smile:

    Will take you up on both those suggestions, thanks !

    Charles S

    Not done yet, unfortunately.

    Everything works beautifully on my localhost setup: I can click on any menu option and when I click Home, I return to index_a, index_b, etc. depending on which of those I first started with.

    I optimized the php to use the echo of $_SESSION[“landingpage”} within the quote of the “a href” and that worked too.

    Uploaded it all to the live site on the server; does not work.

    Went back to the more complicated if … else … construction in header.php and footer.php and uploaded that. Does not work either.

    Any suggestions as to why it works in one place and not the other ? Something I would have to check to see whether it is set up on the server, or what ?

    Thanks yet again
    Charles S

    Resolved … nice to have some knowledgeable support at my ISP for a change. Something I am not used to !

    Apparently, session save path was not set correctly in the .ini and output buffering was not enabled. With those two changes, works great on the server.

    thanks for all your help … hopefully that is the end of the story

    Charles S

    Sponsor our Newsletter | Privacy Policy | Terms of Service