Include () ___ Very noob problem.

I am trying to use include(“xxx.php”); to streamline my code and make it much easier to debug. Unfortunately I don’t think the include command is working at all and I’m not sure what I’m doing wrong.

Here’s the php of the two files I am using Index.php
[php]<?php
session_start();

// LOGIN DETAILS
if(isset($_POST[‘submit’])){
$firstname = $_POST[‘firstname’];
$surname = $_POST[‘surname’];
$email = $_POST[‘email’];

include (“nmschars.php”);

header(‘Location: test.php’);
exit;
}
?>[/php]

As I understand it this simply sets the username, email etc and then tried to include the php code in nmschars.php Below is nmschars.php

NB. test.php is simply a webpage which attempts to print out the variables generated in nmschars.php. As I said it is a test script to help me learn how to include things.

[php]<?php
session_start();

// Determining Patient Age
$age = rand(18,65);

// Determining Patient Gender
$gendernumeric = rand(1,100);
If ($gendernumeric <=66) {
$gender = “male”;
$pronoun = “he”;
$Pronoun = “He”;
$Their = “His”;
$their = “his”;
} else if ($gendernumeric >66) {
$gender = “female”;
$pronoun = “she”;
$Pronoun = “She”;
$Their = “Her”;
$their = “her”;
}

// Determining Underlying Illness
$illnessnumber = rand(1,100);
If ($illnessnumber <=85) {
$illness = “psychosis”;
} else if ($illnessnumber <=95) {
$illness = “parkinsons”;
} else if ($illnessnumber <=98) {
$illness = “depression”;
} else if ($illnessnumber == 99) {
$illness = “bpad”;
} else if ($illnessnumber == 100) {
$illness = “ocp”;
}

$_SESSION[‘age’] = $age;

$_SESSION[‘gendernumeric’] = $gendernumeric;
$_SESSION[‘gender’] = $gender;
$_SESSION[‘pronoun’] = $pronoun;
$_SESSION[‘Pronoun’] = $Pronoun;
$_SESSION[‘Their’] = $Their;
$_SESSION[‘their’] = $their;

$_SESSION[‘illnessnumeric’] = $illnessnumber;
$_SESSION[‘illness’] = $illness;

?>[/php]

Test.php simply says something like "print "This person’s gender is " . $_SESSION[‘gender’] . “.”

It is generating no errors according to php.ini but netierh does it result in any variables being generated so the test.php page reads “This person’s gender is .”

I’m sure it must be something simple but damned if I can figure it out.

Hi there,

Does test.php have session_start() at the top of the file?

Here’s the entire text of test.php

[php]<?php session_start();?>[/php]

HTML STUFF

[php]

<?php session_start(); print "Patient's age is " . $_SESSION['age'] . ".
"; print "Their gender is " . $_SESSION['gender'] . " and Pronoun is " . $_SESSION['Pronoun'] . ".
"; print "Their illness is " . $_SESSION['illness'] . ". Illnessnumeric is " . $_SESSION['illnessnumeric'] . ".
"; ?>

[/php]

finishing html stuff.

That’s it.

One possibility is that I’ve misunderstood session start and only need to call it at the top of the file, not every time I write a bit of php ?

Firstly, no session_start() is not required for each php block, but it is required in each php file (just once at the top of each file that uses session information).

Secondly, have you tried removing the header() redirection and just including the test.php file in that place instead?

Smokey,

Thanks, I’ve amended the files to just have the session_start() at the top of each page.

I’ve changed the header() redirection to include but it still makes no difference to the outcome.

Basically when I go to index.php and click submit the test.php page prints out the text with blank spaces where the output of the variables should be. When I go directly to nmschars.php and then go to test.php variables print and every time I run nmschars.php it correctly generates new variables.

So, as near as I can see test.php and nmschars.php are working but the include into index.php simply isn’t for some reason. Would it be helpful for me to post the URL I’m uploading to here so you could check it out in practice?

Sponsor our Newsletter | Privacy Policy | Terms of Service