Can somebody please take a quick look at my php code

bagging

Newbie
Joined
Jan 15, 2009
Messages
39
Reaction score
29
Hi all BHW members!

This is kinda my first post in here so i would like to start by saying "thank you" to all you guys that make this forum so nice! Hopefully I too could start sharing some nice info when I get better at all this BH stuff :)

I have a problem with some php code (I am not an expert) and I cant seem to get it right. Maybe someone in here could help me out.. Thanks in advance..

Here is my prob:

I have a php page that starts a session and then redirect the user to another page where i would like to show 2 different html pages based on if/else the session is true or not (Did the user come from the previous site or not).

page 1 looks like this:
Code:
<?php
 session_start();
 $_SESSION['BeenToA']='yes';
 header('Location: page2.php');
?>

Page 2 looks like this:
Code:
<?php 
 session_start();
 
  if  ($_SESSION['BeenToA']=='yes')
 unset($_SESSION['BeenToA']);
{
   print ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
 <div><h1>Hello world!</h1>
    </div>
 
</body>
    </html>');
 }
  else ($_SESSION=='no')
 
   {
 
print ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</body>
</html>');
}
?>

In case you dont see my point I will try to explain the usage of the script. I would like to start a session "page1" and then when the session starts bring the user to "page2" where the person then see 1 html code if they came from "page1" otherwise 2 html code.

Maybe this code is totally wrong or it could be made more simple! if so then please advise me :)


Please help!
Bagging
 
You've got

Code:
unset($_SESSION['BeenToA']);

after the if braces on page 2 which is wrong. Nothing but { should come after if(){

also your else should be 'else if'

L
 
Hi lixint,

Thanks alot :) I will try that now.. I will post again to let you all know what happens :D

Bagging
 
Hi Luxint,

I still cant get it to work. Am I still doing something wrong?

Here is my page2:

<?php
session_start();

if(){ $_SESSION['BeenToA']=='yes'
unset($_SESSION['BeenToA']);
print ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<div><h1>Hello world!</h1>
</div>

</body>
</html>');
}
else if()

{

print ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
</head>
<body>
</body>
</html>');
}
?>


Thanks for helping me out!

Bagging
 
I think i am pretty close now but could somebody tell me what i am missing in the code? I get this error: Parse error: syntax error, unexpected T_ELSE on line 20

Code:
<?php 
 session_start();
 
if
   ($_SESSION['BeenToA']=='yes') 
  unset($_SESSION['BeenToA']);
 {
  echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <html>
   <head>
   </head>
   <body>
   <div><h1>Hello world!</h1>
   </div>
 
   </body>
   </html>');
 }
else // This is line 20
 {
 
  echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
   <html>
   <head>
   </head>
   <body>
   </body>
   </html>');
 }
 
?>


Bagging
 
<?php
session_start();

if
($_SESSION['BeenToA']=='yes')
{
unset($_SESSION['BeenToA']);
echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<div><h1>Hello world!</h1>
</div>

</body>
</html>');
}
else // This is line 20
{

echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
</head>
<body>
</body>
</html>');
}

?>
 
try this one

Code:
<?php 
 session_start();
 
if   ($_SESSION['BeenToA']=='yes') 
  
 {
  unset($_SESSION['BeenToA']);

  echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <html>
   <head>
   </head>
   <body>
   <div><h1>Hello world!</h1>
   </div>
 
   </body>
   </html>');
 }
else // This is line 20
 {
   echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
   <html>
   <head>
   </head>
   <body>
   </body>
   </html>');
 }
 
?>

EDIT: well, detox1978 was quicker than me ;)

you got this error because after an IF statement you either execute 1 line of code, or more than 1 line of code in brackets { }
 
Last edited:
try this one

Code:
<?php 
 session_start();
 
if   ($_SESSION['BeenToA']=='yes') 
 
 {
  unset($_SESSION['BeenToA']);
 
  echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <html>
   <head>
   </head>
   <body>
   <div><h1>Hello world!</h1>
   </div>
 
   </body>
   </html>');
 }
else // This is line 20
 {
   echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
   <html>
   <head>
   </head>
   <body>
   </body>
   </html>');
 }
 
?>

EDIT: well, detox1978 was quicker than me ;)

you got this error because after an IF statement you either execute 1 line of code, or more than 1 line of code in brackets { }

It works! Thanks alot detox1978 and silvermario.. You guys just made my day alot better :D

But thats pretty amazing that 1 line of empty code did all that!
 
It works! Thanks alot detox1978 and silvermario.. You guys just made my day alot better :D

But thats pretty amazing that 1 line of empty code did all that!

You're welcome :)

But it wasn't 1 line of empty code that did it, it was moving 1 line of code

this one >> unset($_SESSION['BeenToA']);

into the brackets ;)
 
Thanks for letting me know that!
 
Back
Top