php-general Digest 9 May 2004 16:11:30 -0000 Issue 2753

Topics (messages 185727 through 185747):

using cookies
        185727 by: David T-G
        185729 by: Richard Harb
        185731 by: Aidan Lister

$myobject->$$varname doens't work ??
        185728 by: greg
        185730 by: Richard Harb
        185732 by: Aidan Lister
        185733 by: Aidan Lister
        185734 by: Aidan Lister
        185735 by: greg

Strange mails...
        185736 by: greg

preg_replace to delete all js code in string help needed
        185737 by: Dave Carrera
        185740 by: Curt Zirzow
        185746 by: Dave Carrera

icmp echo / ping
        185738 by: news.php.net
        185739 by: Aidan Lister

Putting a stop in a foreach
        185741 by: Verdon Vaillancourt
        185742 by: Torsten Roehr
        185743 by: Torsten Roehr
        185744 by: Aidan Lister
        185745 by: Verdon Vaillancourt

Re: protecting web page
        185747 by: Daniel Clark

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
Hi, all --

I guess I need a primer on cookie usage.  I've read the manual regarding
setcookie and have gone back to look at everything having to do with
cookies on this list in the past few months (it seems that I'm not the
only one with some troubles, but most of them appear to have been having
sent some HTML output before trying to set a cookie).

I want to check to see if the user has my cookie to then log him in
automatically, and if he doesn't then I show the login screen and he logs
in and then I set the cookie if the box is checked.

Of course, $_COOKIE is set, so I have to check for my cookie name.  Even
something as simple as

  $_COOKIE['test'] = 'tested';

followed by a load of the page and a print doesn't show it.

Do I only call setcookie if the cookie isn't set, or do i call it every
time I load the page to initialize it?  Once I set it, how do I read it?
Does anyone have any pointers to a basic tutorial?


TIA & HAND

:-D
-- 
David T-G
[EMAIL PROTECTED]
http://justpickone.org/davidtg/      Shpx gur Pbzzhavpngvbaf Qrprapl Npg!

Attachment: pgp00000.pgp
Description: PGP signature


--- End Message ---
--- Begin Message ---
-----Original Message-----
From: David T-G
Sent: Sunday, May 9, 2004, 6:09:06 AM
> Hi, all --

> I guess I need a primer on cookie usage.  I've read the manual regarding
> setcookie and have gone back to look at everything having to do with
> cookies on this list in the past few months (it seems that I'm not the
> only one with some troubles, but most of them appear to have been having
> sent some HTML output before trying to set a cookie).

> I want to check to see if the user has my cookie to then log him in
> automatically, and if he doesn't then I show the login screen and he logs
> in and then I set the cookie if the box is checked.

> Of course, $_COOKIE is set, so I have to check for my cookie name.  Even
> something as simple as

>   $_COOKIE['test'] = 'tested';

Under normal circumstances you don't need to do that (unless you know
what you are doing - but it will not set a cookie on the client side,
it only modifies your superglobal variable for as long as the script
runs. I guess I'm once again stating the obvious).


> followed by a load of the page and a print doesn't show it.

> Do I only call setcookie if the cookie isn't set, or do i call it every
> time I load the page to initialize it?  Once I set it, how do I read it?

Once you setcookie() you can optionally give it something on its way:
like name, value, expire time/date, etc.

You could for example set a cookie to expire in half an hour - i.e.
for a session. It might make sense to refresh that cookie on every
page request, so that a session timeout will be postponed on each
refresh.

If you omit the expire time, it will be valid until the end of the
session (browser closes) - see the docs.


How do you get your cookie back?

If your visitor has a browser that allows cookies and you already sent
him one (or more) before, he will automatically transmit it to you as
part of the HTTP request headers. PHP does its magic and places the
values received into that Superglobal $_COOKIE (as well as $_REQUEST)
for you to play with...

So, if that array is empty that could mean two things:
1) you didn't send any cookie before (either because you already sent
your headers and thus have a f***up in your code or because the
visitor hasn't been to your page before)
2) the visitor does not allow cookies - too bad.


I guess that's all there is to it. Someone correct me or add to that
:)


> Does anyone have any pointers to a basic tutorial?

I didn't use any other page except the obvious one ...
http://www.php.net/manual/en/function.setcookie.php

I hope that answers at least part of what you were looking for ...

Richard

--- End Message ---
--- Begin Message ---
Hi,

Richards email was kinda wierd, so I'll reply to your email directly

> Hi, all --

> I guess I need a primer on cookie usage.  I've read the manual regarding
> setcookie and have gone back to look at everything having to do with
> cookies on this list in the past few months (it seems that I'm not the
> only one with some troubles, but most of them appear to have been having
> sent some HTML output before trying to set a cookie).

The manual is all you need to know, you just need to experiment with the
information provided until it clicks in.

> I want to check to see if the user has my cookie to then log him in
> automatically, and if he doesn't then I show the login screen and he logs
> in and then I set the cookie if the box is checked.

Okay, simple:

<?php
if (isset($_COOKIE['yourcookie'])) { // they have the login cookie, so do
what you called "log them in", whatever that means }
else { // they are not logged in, so include your login page }
?>

> Of course, $_COOKIE is set, so I have to check for my cookie name.  Even
> something as simple as

>   $_COOKIE['test'] = 'tested';

> followed by a load of the page and a print doesn't show it.

The above stuff didn't really make sense, you havn't explained what you are
trying to do and what does not work.

> Do I only call setcookie if the cookie isn't set, or do i call it every
> time I load the page to initialize it?  Once I set it, how do I read it?
> Does anyone have any pointers to a basic tutorial?

setcookie() does what you would expect, sets a cookie. As explained on the
PHP manual, " setcookie -- Send a cookie "
So, logically, you only need to call it when you want to set a cookie...

If you want to read the contents of the cookie, print_r($_COOKIE); That will
show you all cookie information on their computer for your domain.

If you know the name of the cookie you set, simply print $_COOKIE['name']

There's not a lot to it, all the information you'll need is on the manual,
good luck!


> TIA & HAND


"David T-G" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]

--- End Message ---
--- Begin Message --- Hello,

I was just trying this but it doesn't work :
<?php

class a {
  public $foo = "hello world";
}

$obj = new a();
$varname = "foo";
echo $obj->$$varname;

$bar = "this is working";
$varname = "bar";
echo $$varname; // display "this is working" as expected
?>

Is it a bug or is it normal ?
I didn't find anything about it in documentation (php4 / php5).

Greg
--- End Message ---
--- Begin Message ---
Try
echo $obj->$varname;


-----Original Message-----
From: greg
Sent: Sunday, May 9, 2004, 9:21:52 AM
> Hello,

> I was just trying this but it doesn't work :
> <?php

> class a {
>    public $foo = "hello world";
> }

> $obj = new a();
> $varname = "foo";
echo $obj->>$$varname;

> $bar = "this is working";
> $varname = "bar";
> echo $$varname; // display "this is working" as expected
?>>

> Is it a bug or is it normal ?
> I didn't find anything about it in documentation (php4 / php5).

> Greg

--- End Message ---
--- Begin Message ---
Richard,

I think you need to read the questions more accurately - your last two
replies have been somewhat incorrect


"Richard Harb" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Try
> echo $obj->$varname;
>
>
> -----Original Message-----
> From: greg
> Sent: Sunday, May 9, 2004, 9:21:52 AM
> > Hello,
>
> > I was just trying this but it doesn't work :
> > <?php
>
> > class a {
> >    public $foo = "hello world";
> > }
>
> > $obj = new a();
> > $varname = "foo";
> echo $obj->>$$varname;
>
> > $bar = "this is working";
> > $varname = "bar";
> > echo $$varname; // display "this is working" as expected
> ?>>
>
> > Is it a bug or is it normal ?
> > I didn't find anything about it in documentation (php4 / php5).
>
> > Greg

--- End Message ---
--- Begin Message ---
Ooops, actually he was pretty ambiguous, please disregard my previous
comment :)


"Richard Harb" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Try
> echo $obj->$varname;
>
>
> -----Original Message-----
> From: greg
> Sent: Sunday, May 9, 2004, 9:21:52 AM
> > Hello,
>
> > I was just trying this but it doesn't work :
> > <?php
>
> > class a {
> >    public $foo = "hello world";
> > }
>
> > $obj = new a();
> > $varname = "foo";
> echo $obj->>$$varname;
>
> > $bar = "this is working";
> > $varname = "bar";
> > echo $$varname; // display "this is working" as expected
> ?>>
>
> > Is it a bug or is it normal ?
> > I didn't find anything about it in documentation (php4 / php5).
>
> > Greg

--- End Message ---
--- Begin Message ---
"Greg" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hello,
>
> I was just trying this but it doesn't work :
> <?php
>
> class a {
>    public $foo = "hello world";
> }
>
> $obj = new a();
> $varname = "foo";
> echo $obj->$$varname;
>

$$varname is turned into $foo, what is the value of $foo?

If you're trying to access "public $foo" then Richard was entirely correct
(sorry richard!), just do it like so: $a->foo

If, like in the below example, $foo is the name of a variable that you want
to access, then try ${$foo}, everynow and then there's a problem to that
effect

Good luck, welcome to oop :)


> $bar = "this is working";
> $varname = "bar";
> echo $$varname; // display "this is working" as expected
> ?>
>
> Is it a bug or is it normal ?
> I didn't find anything about it in documentation (php4 / php5).
>
> Greg

--- End Message ---
--- Begin Message ---

Good luck, welcome to oop :)


PHP 5 is great ! :D
My favorites are __get() and __set()
It allows a very flexible programmation and a very simple programmation when using huge objects correctly implemented.

Greg
--- End Message ---
--- Begin Message --- Each time I post a message on p.general, i receive two strange mails from ADVANCE CREDIT SUISSE BANK.

What's this spam ? It looks like an auto-responder is subscribed on the newsgroup. Spammers really s*x !

Greg
--- End Message ---
--- Begin Message ---
$text2 = preg_replace("/<script[^>]+>.*?<\/script>/is","",$text2);

Leaves output with "language="javascript" blah blah

Dose anyone know of a pattern that will get rid of ALL javascript from a
string.

Thank you for any help

Dave Carrera


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004
 

--- End Message ---
--- Begin Message ---
* Thus wrote Dave Carrera ([EMAIL PROTECTED]):
> $text2 = preg_replace("/<script[^>]+>.*?<\/script>/is","",$text2);
> 
> Leaves output with "language="javascript" blah blah

I tested that and it strips the script tags with "language" too. I
do know that it will not strip script tags that have no attributes:
   <script> var yada;... </script>

This will fix that though:
   /<script[^>]*?>.*?<\/script>/is

   or
   !<script[^>]*>.*</script>!/isU


Curt
-- 
"I used to think I was indecisive, but now I'm not so sure."

--- End Message ---
--- Begin Message ---
Thanks Greg,
That sorted that out nicely :-)

Dave Carrera


-----Original Message-----
From: greg [mailto:[EMAIL PROTECTED] 
Sent: 09 May 2004 14:17
To: Dave Carrera
Subject: Re: preg_replace to delete all js code in string help needed


Dave Carrera wrote:
> $text2 = preg_replace("/<script[^>]+>.*?<\/script>/is","",$text2);
> 
> Leaves output with "language="javascript" blah blah
> 
> Dose anyone know of a pattern that will get rid of ALL javascript from 
> a string.
> 
> Thank you for any help
> 
> Dave Carrera
> 
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004
>  

Try $text2 = preg_replace("/<script[^>]*>.*<\/script>/is","",$text2)

It works for me.

Greg


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004
 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004
 

--- End Message ---
--- Begin Message ---
Hi,

I was wondering if it's possible to issue a icmp echo / ping without
actually using the ping program supplied with the operating system.

Anyone has an idea?

Thanks in advance.



--- End Message ---
--- Begin Message ---
Yep, check out:

http://pear.php.net/net_ping


"News.Php.Net" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi,
>
> I was wondering if it's possible to issue a icmp echo / ping without
> actually using the ping program supplied with the operating system.
>
> Anyone has an idea?
>
> Thanks in advance.
>
>

--- End Message ---
--- Begin Message ---
Hi :)

I'm trying to put a stop in a foreach statement following the user
suggestion here, php.net/manual/en/control-structures.foreach.php

Not really knowing what I am doing, I am running into some synatx problems.
I'm sure I'm doing something really stupid, can anybody point it out?

This is the original statement that works...

foreach ($this->_content as $item) {
    if ($item['type'] == 'item'){
        $elements['ITEM_LINK'] = $item['link'];
        $elements['ITEM_TITLE'] = $item['title'];
        $elements["TARGET"] = $this->_target;
        $items .= PHPWS_Template::processTemplate($elements,
"phpwsrssfeeds", "block_item.tpl");
    }
} 


This is my attempt to count items and put a stop in the foreach so it only
returns 5 items.

foreach ($this->_content as $n => $item) {
    if ($n=="5") { 
        break;
    } else { 
        if ($this->_content[$n] => $item['type'] == 'item'){
            $elements['ITEM_LINK'] = $this->_content[$n] => $item['link'];
            $elements['ITEM_TITLE'] = $this->_content[$n] => $item['title'];
            $elements["TARGET"] = $this->_target;
            $items .= PHPWS_Template::processTemplate($elements,
"phpwsrssfeeds", "block_item.tpl");
        }
    }
}

Php doesn't like the syntax on any of the,
$this->_content[$n] => $item['type']
, etc lines


TIA,
Verdon

--- End Message ---
--- Begin Message ---
"Verdon Vaillancourt" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi :)
>
> I'm trying to put a stop in a foreach statement following the user
> suggestion here, php.net/manual/en/control-structures.foreach.php
>
> Not really knowing what I am doing, I am running into some synatx
problems.
> I'm sure I'm doing something really stupid, can anybody point it out?
>
> This is the original statement that works...
>
> foreach ($this->_content as $item) {
>     if ($item['type'] == 'item'){
>         $elements['ITEM_LINK'] = $item['link'];
>         $elements['ITEM_TITLE'] = $item['title'];
>         $elements["TARGET"] = $this->_target;
>         $items .= PHPWS_Template::processTemplate($elements,
> "phpwsrssfeeds", "block_item.tpl");
>     }
> }
>
>
> This is my attempt to count items and put a stop in the foreach so it only
> returns 5 items.
>
> foreach ($this->_content as $n => $item) {
>     if ($n=="5")

>         break;
>     } else

>         if ($this->_content[$n] => $item['type'] == 'item'){
>             $elements['ITEM_LINK'] = $this->_content[$n] => $item['link'];
>             $elements['ITEM_TITLE'] = $this->_content[$n] =>
$item['title'];
>             $elements["TARGET"] = $this->_target;
>             $items .= PHPWS_Template::processTemplate($elements,
> "phpwsrssfeeds", "block_item.tpl");
>         }
>     }
> }

Why don't you use a for() loop to restrict to 5 loops?:

$count = count($this->_content);
for ($i; $i < 5; $i++) {
...
}

>
> Php doesn't like the syntax on any of the,
> $this->_content[$n] => $item['type']

Because => is a foreach() specific syntax. As far as I understand you don't
need $this->_content[$n] as this is the same as $item, so just omit
'$this->_content[$n] =>':

         if ($item['type'] == 'item'){
             $elements['ITEM_LINK'] = $item['link'];
             $elements['ITEM_TITLE'] = $item['title'];
             $elements["TARGET"] = $this->_target;
             $items .= PHPWS_Template::processTemplate($elements,
"phpwsrssfeeds", "block_item.tpl");
         }

Please try and report if it helped.

Regards, Torsten

> , etc lines
>
>
> TIA,
> Verdon

--- End Message ---
--- Begin Message ---
> > This is my attempt to count items and put a stop in the foreach so it
only
> > returns 5 items.
> >
> > foreach ($this->_content as $n => $item) {
> >     if ($n=="5")
>
> >         break;
> >     } else
>
> >         if ($this->_content[$n] => $item['type'] == 'item'){
> >             $elements['ITEM_LINK'] = $this->_content[$n] =>
$item['link'];
> >             $elements['ITEM_TITLE'] = $this->_content[$n] =>
> $item['title'];
> >             $elements["TARGET"] = $this->_target;
> >             $items .= PHPWS_Template::processTemplate($elements,
> > "phpwsrssfeeds", "block_item.tpl");
> >         }
> >     }
> > }
>
> Why don't you use a for() loop to restrict to 5 loops?:
>
> $count = count($this->_content);
> for ($i; $i < 5; $i++) {
> ...
> }
>
> >
> > Php doesn't like the syntax on any of the,
> > $this->_content[$n] => $item['type']
>
> Because => is a foreach() specific syntax. As far as I understand you
don't
> need $this->_content[$n] as this is the same as $item, so just omit
> '$this->_content[$n] =>':
>
>          if ($item['type'] == 'item'){
>              $elements['ITEM_LINK'] = $item['link'];
>              $elements['ITEM_TITLE'] = $item['title'];
>              $elements["TARGET"] = $this->_target;
>              $items .= PHPWS_Template::processTemplate($elements,
> "phpwsrssfeeds", "block_item.tpl");
>          }

Sorry, I mixed up the for and the foreach syntaxes. Here my (hopefully
correct) loop proposal:

for ($i; $i < 5; $i++) {

     if (is_array($this->_content) && is_array($this->_content[$i]) &&
$this->_content[$i]['type'] == 'item') {
       $elements['ITEM_LINK']  = $this->_content[$i]['link'];
       $elements['ITEM_TITLE'] = $this->_content[$i]['title'];
       $elements['TARGET']     = $this->_target;
       $items .= PHPWS_Template::processTemplate($elements, 'phpwsrssfeeds',
'block_item.tpl');
    }
}

$elements and items should be initialized before the loop.

>
> Please try and report if it helped.
>
> Regards, Torsten
>
> > , etc lines
> >
> >
> > TIA,
> > Verdon

--- End Message ---
--- Begin Message ---
Simple!

$i = 0;
foreach ($foos as $foo)
{
 // do stuff

    $i++;
    if ($i > 5) break;
}

Cheers


"Verdon Vaillancourt" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi :)
>
> I'm trying to put a stop in a foreach statement following the user
> suggestion here, php.net/manual/en/control-structures.foreach.php
>
> Not really knowing what I am doing, I am running into some synatx
problems.
> I'm sure I'm doing something really stupid, can anybody point it out?
>
> This is the original statement that works...
>
> foreach ($this->_content as $item) {
>     if ($item['type'] == 'item'){
>         $elements['ITEM_LINK'] = $item['link'];
>         $elements['ITEM_TITLE'] = $item['title'];
>         $elements["TARGET"] = $this->_target;
>         $items .= PHPWS_Template::processTemplate($elements,
> "phpwsrssfeeds", "block_item.tpl");
>     }
> }
>
>
> This is my attempt to count items and put a stop in the foreach so it only
> returns 5 items.
>
> foreach ($this->_content as $n => $item) {
>     if ($n=="5") {
>         break;
>     } else {
>         if ($this->_content[$n] => $item['type'] == 'item'){
>             $elements['ITEM_LINK'] = $this->_content[$n] => $item['link'];
>             $elements['ITEM_TITLE'] = $this->_content[$n] =>
$item['title'];
>             $elements["TARGET"] = $this->_target;
>             $items .= PHPWS_Template::processTemplate($elements,
> "phpwsrssfeeds", "block_item.tpl");
>         }
>     }
> }
>
> Php doesn't like the syntax on any of the,
> $this->_content[$n] => $item['type']
> , etc lines
>
>
> TIA,
> Verdon

--- End Message ---
--- Begin Message ---
Hmm, yes I see. Thank you.

I guess what I should look at then is getting the key value (internal
counter/pointer) in the array going to the foreach. Like this example...

$a = array( 1,2,3,17 );

$i =0;/* for illustrative purposes only */

foreach ( $a as $v ) {
    echo "\$ a[$i ]=> $v. \n ";
   $i ++; 
}

Once I have that key value (internal counter) I should be able to do
something with it? What I really want to do (since I can't easily get at the
query building the array being passed to the foreach) is get the foreach to
stop outputting results at a certain limit.

Thanks again,
Verdon


On 5/9/04 11:34 AM, "Matt Schroebel" <[EMAIL PROTECTED]> wrote:

> It's good to understand what foreach is doing:
> <?php
> $car['GM'] = 'Impala';
> $car['TOYOTA'] = 'Corolla';
> $car['HONDA'] = 'Civic';
> 
> foreach ($car as $manufacturer => $ model) {
> echo "$manufacturer - $model <br />\n";
> }
> ?>
> 
> Results:
> GM - Impala
> TOYOTA - Corolla
> HONDA - Civic
> 
> 
> 

--- End Message ---
--- Begin Message ---
I agree.  Once the screen, text, or picture is on the clients machine ....they have a 
copy of it.

>>Petr U. wrote:
>>> On Sat, 08 May 2004 21:00:43 -0500
>>> Anguz <[EMAIL PROTECTED]> wrote:
>>> 
>>>  > > Do you know any solution that can help me. I've find an application named
>>>  > > HTML guard but it only work for static html pages. I need more a class or
>>>  > > function to prevent for printing.
>>> 
>>> There is _no way_ to really hide/protect html page on client side.. If someone
>>> (some knowing) would like to show your source and it's important for him, then
>>> he spend some time to break this ugly protection.
>>
>>Just to strengthen/clarify this statement, look at it this way.  The user has 
>>downloaded the data to his machine.  You don't have any control over the data 
>>after that point.  Heck, the user can just print the screen they are looking 
>>at.  How you gonna stop that?
>>
>>If you're just looking to discourage casual copying, then carry on. :-)  If 
>>it's really important, you might be able to generate non-printable PDF files, 
>>or generate images containing the desired text, or stuff like that.  Just to 
>>make copying/printing harder.



--- End Message ---

Reply via email to