Sie sind auf Seite 1von 3

Hi!

Now I've got this code:

...
$_SESSION['log'] = true;

$_SESSION['username'] = $username;

?>

<form method="post" action=main.php>

<br><input class="submitStyle" type=submit value="Go back"></br>

</form>

So, if the user click on the Go back button, the main.php page is called. Can I do
the same thing but without having a button? I mean, I want to re-direct my page to
main.php but no when the users do something (click a button or a link like href).

Thank you

--------------------------------------------------------------------------------

khpNovember 29th, 2004, 07:23 AM


You can redirect like this

<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

If you do this your script must not emit any html code.

--------------------------------------------------------------------------------

TxanTxanNovember 29th, 2004, 10:56 AM


Thank you, but I tried like you say and I recieve this warning:

Warning: Cannot modify header information - headers already sent by (output


started at /home/student/cs/cs01ao/public_html/prog/sidenav.php:78) in
/home/student/cs/cs01ao/public_html/prog/info.php on line 35

The code you say I put in this file (info.php):

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css"/>
<title>Demo</title>
</head>

<body>

<div id="container">
<div id="header"><?php require('header.php'); ?></div>
<div id="topnav"><?php require('topnav.php'); ?></div>
<div id="sidenav"><?php require('sidenav.php'); ?></div>
<?php
$what=$_GET['what'];
if(...){
header( 'refresh: 0; url=http://www.csd.abdn.ac.uk' ); ------> this is line 35
exit;
}
...

This is the sidenav.php file:

<?php
session_start();
?>

<div id="login">
<?php
if (!isset($_SESSION['log'])) {
$_SESSION['log'] = false;
echo("not defined");
}
else{
echo("defined");
}
if($_SESSION['log']==false){ /*not logged in*/
?>
<form method="post" action=info.php?what=login>
<br>Username: <input class="inputStyle" type=text name="username"></br>
<br>Password: <input class="inputStyle" type=password name="password"></br>
<br><input class="submitStyle" type=submit value="Login"></br>
</form>
<br><a href=info.php?what=forgot>Forgot password?</a></br>
<br><a href=info.php?what=register>Not a member? Click here for more information
to register</a></br>
<?php
}
else { /*logged in*/
echo ("Welcome jkljl
<br><a href=info.php?what=logout>Logout</a> |
<a href=info.php?what=profile>My profile</a></br>");
}
/*$_SESSION['username']*/
?>
</div>
...

The sidenav.php line 78 is the last line of the file and it isn't any code in this
line.
Do you know why it doesn't work?
Thanks

--------------------------------------------------------------------------------

khpNovember 29th, 2004, 05:48 PM


You cannot send headers after beginning your html code.
--------------------------------------------------------------------------------

visualAdNovember 30th, 2004, 04:32 PM


You cannot use the header function if you have made any previous calls to any of
PHP's output functiuns such as echo and print or written code out side the PHP
tags.

There are two ways to eliminate that error.

Make sure your first PHP tag is a the very top of the page and there is no
preceeding whitespace. You use the header function before you output any HTML.

On the very first line of your script, open an output buffer:<?php ob_start();
Again make sure there is no preceeding white space and this is the first line of
your script. Once you have made a call to ob_start(), you can use the header
function anywhere in your code. Regardless of whether or not you have preduced any
output.

--------------------------------------------------------------------------------

codeguru.com

Das könnte Ihnen auch gefallen