Page 1 of 1

Problem with variable

Posted: Tue Jan 13, 2004 3:41 am
by olaolu
Im working on a simple content management system using a sample from a book. I have two files headlines.php and page.php

the content of my headline.php are:
[Admin Edit: added for clarity - please use them to make your PHP code easier to read.][/b]

Code: Select all

<?php

include "include_fns.php";
include "header.php";

$conn = db_connect();

$pages_sql = "select * from pages order by code";
$pages_result = mysql_query($pages_sql, $conn);

while ($pages = mysql_fetch_array($pages_result)) {

  $story_sql = "select * from stories
                where page = '$pages

Code: Select all

'
                and published is not null
                order by published desc";
  $story_result = mysql_query($story_sql, $conn);
  if (mysql_num_rows($story_result)) {
    $story = mysql_fetch_array($story_result);
    print "<TABLE BORDER=0 WIDTH=400>";
    print "<TR>";
    print "<TD ROWSPAN=2 WIDTH=100>";
    if ($story['picture']) {
      print "<IMG SRC="$story[picture]">";
    print "</TD>";
    print "<TD>";
    print "<H3>$pages[description]</H3>";
    print $story['headline'];
    print "</TD>";
    print "</TR>";
    print "<TR><TD ALIGN=RIGHT>";
    print "<A HREF="page.php?page=$pages

Code: Select all

">";
    print "<FONT SIZE=1>Read more $pages

Code: Select all

...</FONT>";
    print "</A>";
    print "</TABLE>";
  }
}
}
include "footer.php";
?>

while the content of page.php are:

<?php

require_once("include_fns.php");
require_once("header.php");

$conn = db_connect();

if (!$page) {
  header("Location: headlines.php");
  exit;
}

$sql = "select * from stories 
        where page = '$page'
        and published is not null
        order by published desc";
$result = mysql_query($sql, $conn);

while ($story = mysql_fetch_array($result)) {
  print "<H2>".$story['headline']."</H2>";
  if ($story['picture']) {
    $size = getImageSize($story['picture']);
    $width  = $size[0];
    $height = $size[1];
    print "<IMG SRC="$story[picture]" HEIGHT=$height WIDTH=$width ALIGN=LEFT>";
  }
  print $story['story_text'];
  $w = get_writer_record($story['writer']);
  print "<br><FONT SIZE=1>";
  print $w['full_name'].", ";
  print date("M d, H:i", $story['modified']);
  print "</FONT>";
}


include "footer.php";
?>
the following segment of headlines.php

Code: Select all

print "<A HREF="page.php?page=$pages

Code: Select all

">";
    print "<FONT SIZE=1>Read more $pages

Code: Select all

...</FONT>";
create a hyperlink to page.php which populate page.php from the underline mysql database.

when i click on the hyperlink the following error comes up:
Notice: Undefined variable: page in c:\program files\apache group\apache\htdocs\gprojects\cms\page.php on line 8

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\gprojects\cms\header.php:14) in c:\program files\apache group\apache\htdocs\gprojects\cms\page.php on line 9
I need help on how page.php can recognize the variable $page as it is been used here.

Thanks

Re: Problem with variable

Posted: Tue Jan 13, 2004 4:04 am
by JayBird
olaolu wrote:Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\gprojects\cms\header.php:14) in c:\program files\apache group\apache\htdocs\gprojects\cms\page.php on line 9
For this problem....

Look here Warning: Cannot add header information


Mark[/url]

Posted: Tue Jan 13, 2004 8:44 am
by scorphus
And if you still have problem with the session variable, read this tutorial: Sessions with a Minor in User Logins.

Regards,
Scorphus.

Re: Problem with session variable

Posted: Thu Jan 15, 2004 4:12 am
by olaolu
Jason, I run your sample scripts on viewtopic.php?t=6521 but i got the following error messages:

Notice: Undefined index: username in c:\program files\apache group\apache\htdocs\testsession\page3.php on line 5

and

Notice: Undefined index: auth in c:\program files\apache group\apache\htdocs\testsession\page5.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\testsession\page5.php:4) in c:\program files\apache group\apache\htdocs\testsession\page5.php on line 5

Is there anything i leave out in my php.ini configuration. Please help
:(

Posted: Thu Jan 15, 2004 4:46 am
by twigletmac
Please post your current code so we can help you debug it, the problems shown in the error messages you received cannot be solved by adjusting your php.ini.

Mac

Re: Problem with session variable

Posted: Thu Jan 15, 2004 4:55 am
by olaolu
These are the codes in question:

Page1.php

<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['username']='jason';
}
echo '$_SESSION[username] equals '.$_SESSION['username'];
echo '<br /><br /><a href="page2.php">Go to Page 2</a>';
?>

Page2.php

<?php
session_start();

echo '$_SESSION[username] equals '.$_SESSION['username'];
echo '<br /><br /><a href="page3.php">Go to Page 3</a>';
?>

Page3.php

<?php
session_start();

echo 'You need to log in.<br /><form action="page4.php" method="post">
<input type="text" name="username" value="'.$_SESSION['username'].'" /><br />
<input type="password" name="password" /><br />
<input type="submit" value="Submit" />
</form>';
?>

Page4.php

<?php
session_start();

if ($_POST['username']=='jason' && $_POST['password']=='password') {
$_SESSION['auth']=true;
$_SESSION['username']=$_POST['username'];
header("Location: page5.php");
} else {
$_SESSION['auth']=false;
$_SESSION['username']='';
header("Location: page3.php");
}
?>

Page5.php

<?php
session_start();

if (!$_SESSION['auth']) {
header("Location: page3.php");
}
echo 'Congratulations, you have logged in!';
echo '<br /><a href="page6.php">Log out</a>';
?>

Moving from Page2 to page3 generate the first error while moving from page3 to page5 generated the second error message.

Thanks

Posted: Thu Jan 15, 2004 5:06 am
by twigletmac
Which version of PHP are you using?

Mac

Re: Problem with Session Variable

Posted: Thu Jan 15, 2004 5:14 am
by olaolu
Version 4.3.3

Re: problem with session variable

Posted: Thu Jan 15, 2004 6:47 am
by olaolu
Mac, have a look at this code and tell me how it works, it will go a long way to solve my problem.

Code A

print "<A HREF=\"page.php?page=$pages

Code: Select all

\">";
    print "<FONT SIZE=1>Read more $pages

Code: Select all

...</FONT>";
    print "</A>";

Code B

if (!$page) {
  header("Location: headlines.php");
  exit;
}

Code B depends on the outcome of the value of [b]page [/b]variable in Code A. The problem now is that [b]page [/b]as defined in Code A cannot be understood by Code B, so  the statement if(!$page) is always returning false. What do I do to pass the value of page in Code A to Code B. 

Another thing is that if you are trying to print the variable [b]page[/b] using print $page statement, it returns an error, variable not define.