Jump to content

Including more than once with PHP


ev5unleash

Recommended Posts

Hi, I'm currently making a website with PHP Include so that I can easily edit it. I currently have this code to include different pages

<?php

include('indextop.php');

if( !isset($_GET['id']) )
{
    $xid = 'default';
}
else
{
    $xid = strtolower($_GET['id']);
}
switch($xid)
{
case 'default':
  include('indexhome.php');
break;
case 'teacherlogin':
  include('pages/teacherresources/login.php');
break;
case 'teacherauth':
  include('pages/teacherresources/auth.php');
break; 
}

include('indexbottom.php');

?>

Which let's me make a certain page show when the ?id=teachlogin is shown.

 

Now I have the part teacher auth, which leads to the php file of this code

 

<?php


if( !isset($_GET['code']) )
{
    $xid = 'default';
}
else
{
    $xid = strtolower($_GET['id']);
}
switch($xid)
{
case 'default':
  include('pages/teacherresources/loginfailed.php');
break;
case 'Lnvb773c2y':
  include('pages/teacherresources/index.php');
break;
case 'spage1':
  include('pagespage1.php');
break; 
}


?>

 

When I try "?id=teacherauth?code=Lnvb773c2y" or "?id=teacherauth&code=Lnvb773c2y" The page other than the top and the bottom code is blank. Can anyone tell me why when I include multiple times it won't show the page that's included? Thanks.

Link to comment
Share on other sites

auth.php is in the directory "pages/teacherresources"

 

so you need to update the paths

try this

switch($xid)
{
case 'default':
  include('loginfailed.php');
break;
case 'Lnvb773c2y':
  include('index.php');
break;
case 'spage1':
  include('../../pagespage1.php');
break; 
}

Link to comment
Share on other sites

Okay, I updated auth.php file so now it's

 

<?php


if( !isset($_GET['code']) )
{
    $xid = 'default';
}
else
{
    $xid = strtolower($_GET['id']);
}
switch($xid)
{
case 'default':
  include('loginfailed.php');
break;
case 'Lnvb773c2y':
  include('index.php');
break;
case 'spage1':
  include('../../pagespage1.php');
break;
}


?>

 

Now when I try "?id=teacherauth" I get the message

 

Warning: include(/loginfailed.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\websites\biologywebsite\pages\teacherresources\auth.php on line 15

Link to comment
Share on other sites

Okay here's the root of the main file index.php (which has teacherresource and teacherauth include in it.

 

\index.php (withholds the code below)

<?php

include('indextop.php');

if( !isset($_GET['id']) )
{
    $xid = 'default';
}
else
{
    $xid = strtolower($_GET['id']);
}
switch($xid)
{
case 'default':
  include('indexhome.php');
break;
case 'teacherlogin':
  include('pages/teacherresources/login.php');
break;
case 'teacherauth':
  include('pages/teacherresources/auth.php');
break; 
}

include('indexbottom.php');

?>

\pages\teacherresources\auth.php (withholds the code below)

<?php


if( !isset($_GET['code']) )
{
    $xid = 'default';
}
else
{
    $xid = strtolower($_GET['id']);
}
switch($xid)
{
case 'default':
  include('loginfailed.php');
break;
case 'Lnvb773c2y':
  include('index.php');
break;
case 'spage1':
  include('../../pagespage1.php');
break;
}


?>

\pages\teacherresources\index.php (Shows the message when the login is successful)

\pages\teacherresources\indexfailed.php (Shows the message when the login has failed)

\pages\teacherresources\login.php (has the login page) will

Link to comment
Share on other sites

<?php


if( !isset($_GET['code']) )
{
    $xid = 'default';
}
else
{
    $xid = strtolower($_GET['id']);
}
switch($xid)
{
case 'default':
  include('loginfailed.php');
break;
case 'Lnvb773c2y':
  include('index.php');
break;
case 'spage1':
  include('../../pagespage1.php');
break;
}


?>

 

I would recommend modifying the code like this:

 

<?php
switch(strtolower($_GET['id']))
{
case 'Lnvb773c2y':
  include('index.php');
break;
case 'spage1':
  include('../../pagespage1.php');
break;
default:
  include('loginfailed.php');
break;
}
?>

Link to comment
Share on other sites

Although I marked this as topic solved I have one problem for the default code. When you submit the wrong include like ?code=sdjfh9 people get this error message. Notice: Undefined index: id in C:\wamp\www\websites\biologywebsite\pages\teacherresources\auth.php on line 10 any ideas?

Link to comment
Share on other sites

The code is this now

<?php


if(isset($_GET['code'])) //add this line
   switch(strtolower($_GET['code']))
{
case 'lnvb773c2y':
  include('indexsucessful.php');
break;
default:
  include('indexfailed.php');
break;
}


?>

 

Now when nothing is put in for the code it's blank and does not point to the login failed page. Although, now it does when a wrong include is put in with no error.

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.