Little PHP help

Butler

Registered Member
Joined
Feb 4, 2008
Messages
71
Reaction score
1
Just started learning php and it's not going well. I am trying to get this code to work

PHP:
<?php
    $referer = $_SERVER['HTTP_REFERER'];
    if($referer == "")
    {
        echo "hxxp://www.mydomain.com/trackingscript.php"; 
else
        echo "<img src="hxxp://www.mydomain.com/image.jpg">";   
    }
?>

But it keeps giving me errors? Can anyone let me know what I have done wrong, I have tried a few things but nothing is working for me.

Also, what is the best site to learn php from scratch?
 
Hi try that:

PHP:
<?php
    $referer = $_SERVER['HTTP_REFERER'];
    if($referer == "")
    {
        echo 'hxxp://www.mydomain.com/trackingscript.php'; 
    }
else
   {
        echo '<img src="hxxp://www.mydomain.com/image.jpg">';   
    }
?>

... your problem were the quotes used 2 times behind each other ... better use ' " " ' or " ' ' " --> see the use of single quote ;-)
And you forgot to close and reopen the bracket { before and after the else

cheers catman

P.S. Do not forget to change the urls though --> also note to repace the hxxp with http ;-)
 
Last edited:
best way to learn php is reading a few tutorials and looking at the code of free php scripts.
 
Hi try that:

PHP:
<?php
    $referer = $_SERVER['HTTP_REFERER'];
    if($referer == "")
    {
        echo 'hxxp://www.mydomain.com/trackingscript.php'; 
    }
else
   {
        echo '<img src="hxxp://www.mydomain.com/image.jpg">';   
    }
?>

... your problem were the quotes used 2 times behind each other ... better use ' " " ' or " ' ' " --> see the use of single quote ;-)
And you forgot to close and reopen the bracket { before and after the else

cheers catman

P.S. Do not forget to change the urls though --> also note to repace the hxxp with http ;-)

Ah, thanks, it's the small things like this I need to pick up on.

Yeah, I was just using the xx's cause it's become a habit when posting on this forum.

Is there any way I can include this code in the echo ' ' part

header( 'Location: http://www.mydomain.com/new_page.html' ) ;

So it loads the requested page instead of showing a link?
 
I was trying to put it in the '' after the echo, am such a dumb ass.

Thanks for the help guys, off to do some learning.
 
Hey Butler ...

... am glad we could help you.

One page that i found is very usefull for the students that i teach in php is the page hxxp://tiz*ag.c0m

there you have a crash course inclusive easy to understand tutorials for several programming languages (like php, javascrip, aspnet, mysql, sql, ajax, ...)

I think this will help you a lot.

Many Greetings
Catman

P.S. I know thwe beggining of learning the first programming language is sometimes hard sometimes even frustrating :pcguru: ... but the time where you will have a clear understanding of everthing will come fast. Then programming is a lot of fun and learning new languages has become a lot easier too. :)
 
Last edited:
You might want to check out the PHP section of this forum, which is where I'm moving this thread now...
 
There is no reason to avoid HTTP_REFERER. Just when using it, be aware that it's not always set, and that it can be freely manipulated by the client, so it is untrusted data.
The vast majority of clients sets the variable, and does so correctly.
The main reason for blocking it is privacy: For example, when opening an E-Mail in a web mail client, links to external images would carry the web mail service's address in the HTTP_REFERER header. That's why GMail and Yahoo make efforts to block it.
The workaround you suggest works only for movements within the same site, and will break if the user has more than one tab/browser window open with which they browse your site. If you need to know the referring page, the superior method is to add a GET parameter like
<a href="otherpage.html?from=thispage.html">
One example where relying on HTTP_REFERER is dangerous would be showing a live-updated list of referring sites on your main page ("Visitors came from...") it would be easy to smuggle arbitrary URLs into that list by visiting your site with a fake HTTP_REFERER set.
 

<?php

//either this one
$referer
= $_SERVER['HTTP_REFERER'];
if(
$referer == "")
{
echo
"http://www.mydomain.com/trackingscript.php";
else {
echo
"<img src='
http://www.mydomain.com/image.jpg'>";
}
?>



<?php
//or this one
$referer
= $_SERVER['HTTP_REFERER'];
if(
$referer == "")
{
echo
'http://www.mydomain.com/trackingscript.php';
else {
echo
'<img src="
http://www.mydomain.com/image.jpg">';
}
?>






Please take a review about using '' and "" in php. It's one of the most important thing to consider specially in working with php variables.

Cheers mate! :)
 
Back
Top