A couple of very basic problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nike
Forum Newbie
Posts: 14
Joined: Mon Apr 29, 2002 1:38 pm

A couple of very basic problems

Post by nike »

Hi,
I am starting out and have various problems.
I have a VB Backround so please
don`t shoot me if I ask stupid/obvious questions ;-)

thanks to a nice member of this forum I received some of the following code and tried to extend it a bit. Unfortunately it just doesn`t work, the way I am trying to make it work :-(
Your help would be very much appreciated.

Code: Select all

if (isset($_SERVERї'HTTP_REFERER']))
#ok this works fine
	{
    print 'HTTP_REFERER = ' .$_SERVERї'HTTP_REFERER'];

#get the referer into a Variable and print it out...
#this doesn´t work, why?
#get the value into the variable
    $strHTTPRef = $_SERVERї'HTTP_REFERER'] 
# print the variable
    print $strHTTPRef; // 
    #create a pagebreak
    echo '<br>'; #this works
    echo "n";    #this doesn`t
    #echo ''n'''; #this doesn`t, too.
    echo "This is a pagebreak \n";#why does it not create a pagebreak? 
#Do I have to change a setting in my php.ini file or what am I doing wrong?
    #print out the last Part of e.g. http://www.mysite.com/test.htm ->test.htm;
    print substr(strrpos($_SERVER&#1111;'HTTP_REFERER'],"&quote;),strlen($_SERVER&#1111;'HTTP_REFERER'])-strrpos($_SERVER&#1111;'HTTP_REFERER'],"&quote;));
#ok, you could do this with this ereg thing, 
#but I would like to chop the string into pieces, but it doens`t work, 
#can anybody tell me why?
#Did I forget something?
    &#125;

else
    print 'no HTTP_REFERER given';
?>
Hope you can help me.

Bye

Nike
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Re: A cople of very basic problems

Post by Heavy »

Hello and welcome to heaven!

Code: Select all

$strHTTPRef = $_SERVER&#1111;'HTTP_REFERER']
First, every command in PHP is to be separated with a semicolon ";", otherwise causes a parse error.

Code: Select all

echo "n";    #this doesn`t
Youv'e used the magic backslash character in the wrong place. What you wrote has the following VBScript equvivalent:

Code: Select all

response.write"n""
while what you probably wanted was:

Code: Select all

response.write VbCrLf
is translated into PHP as:

Code: Select all

echo "\n";
This also means that the first output string after "<BR>" in not terminated on that row.
YES, PHP supports strings that span over multiple lines.
http://www.php.net/manual/en/language.types.string.php

Further:

Code: Select all

#echo ''n'''; #this doesn`t, too.
two errors within a small scope :wink: Since the string is started with the single quote character, it is ended with a single quote character. if you want to output ' you will need to add a backslash in front of it, like: ''
Also if it has worked just a bit, the third singlequote character had been escaped by that backslash. So... it wouldn't have worked...

Code: Select all

echo "This is a pagebreak \n";#why does it not create a pagebreak?
The above might have worked if the first error wasn't there. Since then, all of the code is a multiline string! 8O

And pagebreak is not the same asnew line. The character \n is a new-line character, with an ASCII-value of 13. As far as I know, there are no page breaks for HTML.

To put it easier, this might be what you are looking for:

Code: Select all

&lt;HTML&gt;
&lt;HEAD&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;?

//Manipulation:
//$_SERVER&#1111;'HTTP_REFERER'] = "www.php.net/manual.htm";


if (isset($_SERVER&#1111;'HTTP_REFERER']))
   {
    print 'HTTP_REFERER = ' .$_SERVER&#1111;'HTTP_REFERER'];
    $strHTTPRef = $_SERVER&#1111;'HTTP_REFERER'];
    //print $strHTTPRef;
    echo '&lt;br&gt;&lt;br&gt;';
    echo "\n";   // this works for THE HTML CODE! HTML output won't notice any difference between a new line "\n" and a space " ".
    echo "There is a is a linebreak here\n visible only in the HTML code.&lt;br&gt;";
    echo "And HERE\n&lt;br&gt;is a is a linebreak visible in the HTML code and the visitor.";
#Do I have to change a setting in my php.ini file or what am I doing wrong?
//Nope!

    #print out the last Part of e.g. http://www.mysite.com/test.htm -&gt;test.htm;
    echo "&lt;br&gt;&lt;br&gt;&lt;br&gt;";

// Please note the syntax:
// string substr ( string string, int start &#1111;, int length])
// If length is omitted, it will return the rest of the string...
    $start = strpos($_SERVER&#1111;'HTTP_REFERER'],"/",7);
    $lenght = strlen($_SERVER&#1111;'HTTP_REFERER']) - $start - (strlen($_SERVER&#1111;'HTTP_REFERER']) -strrpos($_SERVER&#1111;'HTTP_REFERER'],"/"));

    print "&lt;br&gt;Dir:".substr($_SERVER&#1111;'HTTP_REFERER'],$start,$lenght);
    print "&lt;br&gt;File:".substr($_SERVER&#1111;'HTTP_REFERER'],strrpos($_SERVER&#1111;'HTTP_REFERER'],"/"));

    }
else
    print 'no HTTP_REFERER given';
?&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
I know its a lot, but let's say:
You gave us a lot! :wink:
nike
Forum Newbie
Posts: 14
Joined: Mon Apr 29, 2002 1:38 pm

Thank you

Post by nike »

Hi Heavy,
thanks a bunch.
I feel like I made all mistakes that were possible to make :-(
Thanks again for helping me.

Bye

Nike
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Please come back soon!
It's nice to be able to help! :D
AND, every time one sees another's problem, one might just learn something :!:
nike
Forum Newbie
Posts: 14
Joined: Mon Apr 29, 2002 1:38 pm

Sure

Post by nike »

Hi Heavy,
rest assured, like Arnold said:
I´ll be back ;-)

I am currently trying to develop a feedback form
(I know, there are a couple of tutorials out there,
but e.g. not one where the referenced page is transmitted
as a possible Subject line - which is why i use the http ref thing...
And anyway, you only learn it for real, when you bite the bullet and
get out there coding and make mistakes - like me ;-)
More the better if you have a source of help at hand.

Funny thing with helping is though, I am used to give help with VB Questions in MS Excel, I´m not used to actually get help.
It´s a nice feeling though - getting help once in a while ;-)

The funny thing is, when you´re really good in a programming
language, you sometimes just don`t value your knowledge
as much as the person that needs the help,
as for you it`s easy/obvious...

Bye

Nike
Post Reply