Forum Moderators: coopster

Message Too Old, No Replies

Check for Variables in URL

         

LinusIT

11:44 am on Jan 11, 2011 (gmt 0)

10+ Year Member



Hi

I have page that is used to display various thank you messages through different variables in the URL. The code I use currently is


<?php if ($_GET["message"]=="sent")
{
?>
Blah Blah
<?php } ?>


I would like to restrict access to this page because if I access thankyou.php it's a blank page and looks silly.

I tried adding the following on that last message but this just redirected the page all the time.

<?php if ($_GET["email"]=="sent")
{
?>
Blah Blah
<?php } else { {header( "Location: index.php" );}} ?>


Any help would be great thanks.

omoutop

11:54 am on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have:
if ($_GET["message"]=="sent")
if ($_GET["email"]=="sent")

One of that must be true. Fix this error and your second condition should work

LinusIT

12:04 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



I'm pretty new to PHP so bear with me.

Should there be an else if somewhere in the code?

Would it be better if I posted the entire relevant code?

omoutop

3:17 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if ($_GET["message"]=="sent") 
{
// execute code here if statement is true
}
else
{
header( "Location: index.php"); // redirect user back
}

LinusIT

9:18 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



Maybe I'm going around the issue the wrong way.

I need both if statements in there to display different messages based on what's in the URL.


<div class="content">
<?php if ($_GET["kit"]=="sent")
{
?>
<h1>Thank you for applying for your free kit!</h1>
<p>We will dispatch your kit immediately.</p>
<?php } ?>
<?php if ($_GET["email"]=="sent")
{
?>
<h1>Thank you for your enquiry</h1>
<p>Your message has been sent, we will get back to you as soon as possible</p>
<?php //} ?>
<?php } else { {header( "Location: index.php" );}} ?>
</div>

Matthew1980

9:32 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there LinusIT,


<div class="content">
<?php

if(isset($_GET["kit"]) && ($_GET["kit"] == "sent")){
?>
<h1>Thank you for applying for your free kit!</h1>
<p>We will dispatch your kit immediately.</p>
<?php
}

if (isset($_GET["email"]) && ($_GET["email"] =="sent")){
?>

<h1>Thank you for your enquiry</h1>
<p>Your message has been sent, we will get back to you as soon as possible</p>
<?php
}else{
header("Location: index.php");
exit;
}
?>
</div>


Not as elegant as I would like, but from what you quoted in the above post this is what you could do, but if this was just an example, perhaps you can extrapolate from this to achieve what you want?

Please note that there is NO error checking in this, and if variables are not set then the page will just display blank..

Cheers,
MRb

LinusIT

11:47 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



Thanks for the replies.

I haven't tested the code you posted, I trust it would work but I'm intrigued by you saying there is a better way of doing this. I'm learning php as I go along so would be very interested in seeing a better way please.

omoutop

11:54 am on Jan 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I need both if statements in there to display different messages based on what's in the URL.



if (statement1 validates)
{
// execute code block 1
}
elseif (statement2 validates)
{
// execute code block 2
}
elseif (statement3 validates)
{
// execute code block 3
}
...
else
{
// execute header redirection
}


You can also use the switch() syntax if you have too many if..else statements

LinusIT

8:51 pm on Jan 12, 2011 (gmt 0)

10+ Year Member



I've tried what you suggested but must have missed something as the page doesn't work at all now, oooops.

Here's what I've got:


<?php if ($_GET["kit"]=="sent")
{
echo "<h1>Thank you for applying for your free kit!</h1>"
}
elseif ($_GET["email"]=="sent")
{
echo "<h1>Thank you for your enquiry</h1>"
}
else
{
header("location: index.php");
}
?>


Looking at it seems fine but obviously I don't know enough about php (yet.

Matthew1980

9:12 pm on Jan 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>page doesn't work at all now, oooops.

Nothing? Odd..

<?php
//simple debugging first see what your getting through to the page!
echo "<pre>";
print_r($_GET);
echo "</pre>";

//if/else though your $_GET array to see if they match
if(isset($_GET['kit']) && ($_GET['kit'] == "sent")){
echo "Get kit was successful as it was set";
}
elseif(isset($_GET['email']) && ($_GET['email'] == "sent"){
echo "Get email was successful as it was set";
}
else{
echo "Oops nothing was set in the url that matched your criteria";
}

?>

That's as plain as I can put it to you, only other thing to check is that the spellings are correct and ChEcK CaSe ToO!

Cheers,
MRb

omoutop

7:14 am on Jan 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



also:


echo "<h1>Thank you for applying for your free kit!</h1>"


is missing a ( ; ) at end
it should read:
echo "<h1>Thank you for applying for your free kit!</h1>";

Same goes for the rest of echoes.
If page is blank, i suspect that error reporting is off in your server

Matthew1980

8:57 am on Jan 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^^

Well spotted omoutop, error_reporting(); would have alerted him to that, should always be switched on when your developing.

Cheers,
MRb

LinusIT

9:01 am on Jan 13, 2011 (gmt 0)

10+ Year Member



I've added the missing ( ; ) at the end of each line and it's working great now, thanks.

I'm guessing error reporting is turned off as whenever there is a problem all I get is a blank page, not helpful.

One last question, I've added a link within an echo line and can't get the quotes around the link, If I add " then it screws the line up, how do I achieve this?

echo "<p>Click <a href='index.php'>here</a> to go back to the home page.</p>";


This works perfectly except the output is:

<a href='index.php'>here</a>
.

Thanks for everyones help :)

LinusIT

9:03 am on Jan 13, 2011 (gmt 0)

10+ Year Member



@Matthew1980

If I were having any problems with pages in the future, do I just add error_reporting(); at the top of the page and this would generate relevant error messages?

omoutop

3:00 pm on Jan 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



since you echo your html code with echo ".....";
the " must be escaped.

Try this:
echo "<p>Click <a href=\"index.php\">here</a> to go back to the home page.</p>";
or
echo '<p>Click <a href="index.php">here</a> to go back to the home page.</p>';

As for error reporting, I am not sure if error_reporting(); will work if its disabled in server.
You can try and find out.
As an alernative, if you have access to php.ini you can enable it there
OR if you have access to .htaccess add this line:
php_flag display_errors 1

Each solution depends on your hosting server

Matthew1980

6:45 pm on Jan 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LinusIT:-

This will explain it [uk3.php.net]

I use this version by default now:-

<?php
error_reporting(E_ALL|E_STRICT|E_DEPRECATED);
?>

This tells the parser at runtime to display ALL messages INCLUDING STRICT coding standards and any old unsupported functions (E_DEPRECATED)

Hope that makes it clearer for you!

Cheers,
MRb

g1smd

1:28 am on Jan 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your link back to the home page should be to "/", not a named page.

Not only does this future proof your site from technology changes, it also immediately eliminates a duplicate content problem.

LinusIT

3:40 pm on Jan 14, 2011 (gmt 0)

10+ Year Member



@moutop

Thanks for the suggestions, the first one worked, the second one made index.php black and upon testing, I got a blank page.

@matthew1980 I've saved that link about error reporting, thanks. After all this time of dealing with blank pages, I could have done something about it, lol.

@g1smd I've never heard of linking to the home page being done like that, have seen it but never knew of the differences. I manage several sites, I should I implement like across them all?