php-general Digest 5 Apr 2004 09:24:06 -0000 Issue 2687

Topics (messages 182300 through 182337):

\n is not working!
        182300 by: Labunski
        182302 by: Robert Cummings
        182303 by: Monty
        182306 by: DvDmanDT
        182310 by: Chris Shiflett

$HTTP_SESSION_VARS still holds original values even after unset?
        182301 by: Andy B
        182304 by: John Holmes
        182307 by: DvDmanDT
        182308 by: Andy B
        182309 by: Andy B
        182311 by: Larry Brown
        182312 by: DvDmanDT
        182314 by: trlists.clayst.com
        182315 by: Andy B
        182317 by: John Holmes
        182319 by: Andy B
        182320 by: trlists.clayst.com
        182321 by: Andy B
        182327 by: Curt Zirzow

Re: Session hell: register_globals off
        182305 by: DvDmanDT

Re: php as default "value" in html
        182313 by: Larry Brown

[solved][PHP] php as default "value" in html
        182316 by: Andy B

Sorting array of objects
        182318 by: Richard Harb
        182325 by: Tom Rogers

Re: session_exist() ?? Can this be done?
        182322 by: Curt Zirzow
        182323 by: Aaron Christopher Vonderhaar

Re: Regular Expressions
        182324 by: Curt Zirzow

Weird variable issue encountered... help needed!
        182326 by: gvarosky.conversent.com

Mail sending from nobody to nobody...
        182328 by: Jonathan Villa
        182329 by: hitek
        182330 by: Jonathan Villa
        182335 by: hitek

Re: mysql - can't use the copy/paste function
        182331 by: Burhan Khalid

Re: Extracting Output from File...
        182332 by: Burhan Khalid

Passing array as subject to preg_replace has odd behaviour with limit
        182333 by: Leon Derczynski

Re: code design? modular?
        182334 by: Justin French

How to built desktop applications.
        182336 by: francesco.automationsoft.biz
        182337 by: Red Wingate

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 ---
\n isn't working properly.

Why do this line $new_topic = $post_0.'|'.$post_1.'|'.'$post_2\n'; writes to
the document
one|two|three\n
but not
one|two|three (and break)



whole code:

if ($a=="new"){
$post_0 = $_POST["one"];
$post_1 = $_POST["two"];
$post_2 = $_POST["three"];
 if ($post_1 !=""){
 if ($post_2 !=""){
$m_fails=fopen('../data/menu.txt','a');
$new_topic = $post_0.'|'.$post_1.'|'.'$post_2\n';
fwrite($m_fails,$new_topic);
fclose($m_fails);
}}
}

--- End Message ---
--- Begin Message ---
On Sun, 2004-04-04 at 17:58, Labunski wrote:
> \n isn't working properly.
> 
> Why do this line $new_topic = $post_0.'|'.$post_1.'|'.'$post_2\n'; writes to
> the document
> one|two|three\n
> but not
> one|two|three (and break)

Because it's in single quotes versus a double quotes.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
\n must be included in double-quotes because it's treated like a variable.

    echo "This works\n";  // Displays: This works

    echo "This doesn't work\n';  // Displays: This doesn't work\n


> From: [EMAIL PROTECTED] (Labunski)
> Newsgroups: php.general
> Date: Mon, 5 Apr 2004 00:58:35 +0300
> To: [EMAIL PROTECTED]
> Subject: \n is not working!
> 
> \n isn't working properly.
> 
> Why do this line $new_topic = $post_0.'|'.$post_1.'|'.'$post_2\n'; writes to
> the document
> one|two|three\n
> but not
> one|two|three (and break)
> 
> 
> 
> whole code:
> 
> if ($a=="new"){
> $post_0 = $_POST["one"];
> $post_1 = $_POST["two"];
> $post_2 = $_POST["three"];
> if ($post_1 !=""){
> if ($post_2 !=""){
> $m_fails=fopen('../data/menu.txt','a');
> $new_topic = $post_0.'|'.$post_1.'|'.'$post_2\n';
> fwrite($m_fails,$new_topic);
> fclose($m_fails);
> }}
> }

--- End Message ---
--- Begin Message ---
>     echo "This doesn't work\n';  // Displays: This doesn't work\n

Seems to me like that would generate parse error.. :s But yea, we get your
point.. :)
-- 
// DvDmanDT
MSN: dvdmandt€hotmail.com
Mail: dvdmandt€telia.com

--- End Message ---
--- Begin Message ---
--- Labunski <[EMAIL PROTECTED]> wrote:
> Why do this line $new_topic = $post_0.'|'.$post_1.'|'.'$post_2\n';
> writes to
> the document
> one|two|three\n

Actually, that's not true. Assuming:

$post_0 = 'one';
$post_1 = 'two';
$post_2 = 'three';

The code you posted will store the following in $new_topic:

one|two|$post_2\n

While others were able to answer your question anyway (anything within
single quotes is interpreted literally), this demonstrates how providing
misinformation can make it very difficult for people to help you.

As a courtesy to those who spend their time answering questions, please
test your code. It really only takes a moment of your time.

Thanks. :-)

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security - O'Reilly
     Coming Fall 2004
HTTP Developer's Handbook - Sams
     http://httphandbook.org/
PHP Community Site
     http://phpcommunity.org/

--- End Message ---
--- Begin Message ---
how would you empty all the contents of $HTTP_SESSION_VARS when you dont need them 
anymore but still keep the current session running? the problem seems to be that when 
i try to fill them with something else (dont need the original values anymore) but 
need new ones without destroying the session itself they wont fill with new things. 
unset($HTTP_SESSION_VARS); does nothing to help at all because they still have the 
same values that they were originally filled with (and not to mention they are still 
in the session)...

anybody have any ideas how to "delete" or empty out the array but keep the session 
active ??


--- End Message ---
--- Begin Message --- Andy B wrote:
how would you empty all the contents of
> $HTTP_SESSION_VARS when you dont need
> them anymore but still keep the current
> session running?

$HTTP_SESSION_VARS = array();

---John Holmes...
--- End Message ---
--- Begin Message ---
Umm.. Use $_SESSION for that, and session_unregister() for the unsetting
with the old style code..

-- 
// DvDmanDT
MSN: dvdmandt€hotmail.com
Mail: dvdmandt€telia.com
"Andy B" <[EMAIL PROTECTED]> skrev i meddelandet
news:[EMAIL PROTECTED]
how would you empty all the contents of $HTTP_SESSION_VARS when you dont
need them anymore but still keep the current session running? the problem
seems to be that when i try to fill them with something else (dont need the
original values anymore) but need new ones without destroying the session
itself they wont fill with new things. unset($HTTP_SESSION_VARS); does
nothing to help at all because they still have the same values that they
were originally filled with (and not to mention they are still in the
session)...

anybody have any ideas how to "delete" or empty out the array but keep the
session active ??

--- End Message ---
--- Begin Message ---
"$HTTP_SESSION_VARS = array();"

doesnt work... it kills the session and forces me to login as soon as the
array is empty

--- End Message ---
--- Begin Message ---
"Umm.. Use $_SESSION for that......"

wish i could but it wont work for the server that its going to run
on....they still use php4.0.4pl1 believe it or not

--- End Message ---
--- Begin Message ---
Something else in your code is messing you up if you cannot change the value
after setting it in the first place.  This happens all the time in my code
as my client makes decisions along the way.  Try a small test creating a
session variable, displaying it, changing it, and redisplaying it.  If it
does not show the change, post the test code so we might be able to tell why
it fails.

Provided you can change the variable, you should be able to create a loop
that sets all the variables to "" or some base values etc.

Larry

-----Original Message-----
From: Andy B [mailto:[EMAIL PROTECTED]
Sent: Sunday, April 04, 2004 6:03 PM
To: [EMAIL PROTECTED]
Subject: [PHP] $HTTP_SESSION_VARS still holds original values even after
unset?


how would you empty all the contents of $HTTP_SESSION_VARS when you dont
need them anymore but still keep the current session running? the problem
seems to be that when i try to fill them with something else (dont need the
original values anymore) but need new ones without destroying the session
itself they wont fill with new things. unset($HTTP_SESSION_VARS); does
nothing to help at all because they still have the same values that they
were originally filled with (and not to mention they are still in the
session)...

anybody have any ideas how to "delete" or empty out the array but keep the
session active ??

--- End Message ---
--- Begin Message ---
Use the session_* functions then..

-- 
// DvDmanDT
MSN: dvdmandt€hotmail.com
Mail: dvdmandt€telia.com
"Andy B" <[EMAIL PROTECTED]> skrev i meddelandet
news:[EMAIL PROTECTED]
> "Umm.. Use $_SESSION for that......"
>
> wish i could but it wont work for the server that its going to run
> on....they still use php4.0.4pl1 believe it or not

--- End Message ---
--- Begin Message ---
On 4 Apr 2004 Andy B wrote:

> how would you empty all the contents of $HTTP_SESSION_VARS when you
> dont need them anymore but still keep the current session running?

See http://www.php.net/manual/en/function.session-unset.php.  That is 
exactly what it does.  That page also explains why unset() is not the 
right way to go.

--
Tom

--- End Message ---
--- Begin Message ---
"Use the session_* functions then.."

still does nothing at all or it does the same as session_destroy. and that
forces a user back to the login screen right in the middle of an sql
insert/update....

--- End Message ---
--- Begin Message --- Andy B wrote:

"Use the session_* functions then.."

still does nothing at all or it does the same as session_destroy. and that
forces a user back to the login screen right in the middle of an sql
insert/update....


Okay, start showing some code so we can see wtf is going on. Constantly posting back "nope", "doesn't work" is just going to have us grasping at straws again and again.


Is register globals on or off?

---John Holmes...
--- End Message ---
--- Begin Message ---
"Okay, start showing some code so we can see wtf is going on. Constantly
posting back "nope", "doesn't work" is just going to have us grasping at
straws again and again.

Is register globals on or off?
"
register_globals=off

here is the code that gives me problems:
files are attached (if allowed) let me know if the attached files didnt make
it through and i will post it in the body of the message...the code is quite
huge chunck of stuff...

file guestbook_add.php: is a form used for admins to insert a guestbook
entry. it is linked from a admin page using session_start(); at the top of
the admin main page. the button that links to this page code is:
<input class="button" type="button" value="add guestbook listing"
accesskey="3" onclick="window.location='guestbook_add.php';"
onkeypress="window.location='guestbook_add.php';">

after filling out the form and submitting it to the file called
guestbook_review2.php it sets all of the $HTTP_SESSION_VARS[] vars to the
inital form submitted values and shows them on the page. after they hit the
continue>> button it checks for default/required vars and then inserts to
the db in the file called guestbook_save.php.. after a successful insert it
takes them back to the main admin page. the next time i hit the "add
guestbook" listing from the main screen, fill out the form with different
values and submit it all the $HTTP_SESSION_VARS have the same values as the
last add sequence...

its interesting...(i know the code may be sloppy but...im starting out with
complex stuff and was forced on a super fast turn around on it)...

--- End Message ---
--- Begin Message ---
On 4 Apr 2004 Andy B wrote:

> the next time i hit the "add guestbook" listing from the main screen,
> fill out the form with different values and submit it all the
> $HTTP_SESSION_VARS have the same values as the last add sequence... 

There are two problems there.  First, your session data is not getting 
cleared.  Second, it is not getting reset with the new values.

For the former, in guestbook_save.php you can simply do a session_unset 
(if you really want to clear all of them).  However this does not 
handle an abnormals equence -- for example someone hits Back in the 
middle of the process.

I don't understand why they are not getting set properly the second 
time around.  It could be a condition in your code.  I'd have to see 
the code to have an idea.

Try stripping the code WAY down so you have a test_add.php with a 1-
field form, and a test_review.php that displays that data and saves it 
in a session variable, and test_save.php that just displays what it 
receives in the session variables.  If that still fails, post the code. 
If not, your problem lies in the difference between that and the 
original code.

--
Tom

--- End Message ---
--- Begin Message ---
"There are two problems there.  First, your session data is not getting
cleared.  Second, it is not getting reset with the new values.

For the former, in guestbook_save.php you can simply do a session_unset
(if you really want to clear all of them).  However this does not
handle an abnormals equence -- for example someone hits Back in the
middle of the process.

I don't understand why they are not getting set properly the second
time around.  It could be a condition in your code.  I'd have to see
the code to have an idea.

Try stripping the code WAY down so you have a test_add.php with a 1-
field form, and a test_review.php that displays that data and saves it
in a session variable, and test_save.php that just displays what it
receives in the session variables.  If that still fails, post the code.
If not, your problem lies in the difference between that and the
original code.
"

ok heres the code in the message body: hope it fits....
note: im in the middle of porting it from new code to old code so some
$_SESSION vars might show themselves time to time...
[guestbook_add.php: form to fill out:linked from admin.php(not shown)]
<?
session_start();
if(!empty($_SESSION['username'])){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/transitional.dtd";>
<html>
<head>
<title>Add GuestBook Entry</title>
<LINK rel="stylesheet" href="style/rnjresort.php" type="text/css">
</head>

<body>
<h1>Add guestbook entry</h1>
<div>
<table>
<tr>
<td>
NOTE: when filling out this form, make sure you fill the date out in the
format yyyymmddhhmmss. example: 20040401000000 would be april 1 2004 12
midnight. To insert the current date leave the date blank.
</td>
</tr>
</table>
</div>
<div>
<form method="post" action="guestbook_review2.php">
<table summary="Below is the entire form for adding your entry to the
guestbook">
<tr>
<td>
<label for="id-date">Date: </label><input type="text" name="date"
accesskey="a">  <label for="id-name">Name:</label> <input type="text"
name="name" accesskey="n" id="id-name">
</td>
<td>
<label for="id-email">Your Email address:</label> <input type="text"
name="email" accesskey="e" id="id-email">
</td>
</tr>
<tr>
<td>
<label for="id-website"> Your URL:</label> <input type="text" name="website"
accesskey="w" id="id-website">
</td>
<td>
<label for="id-referred"> How did you hear about us?</label> <input
type="text" name="referred" accesskey="d" id="id-referred">
</td>
</tr>
<tr>
<td>
<label for="id-comments"> Comments:</label> <textarea name="comments"
rows="5" cols="40" accesskey="c" id="id-comments"></textarea>
<center>
<input class="button" type="submit" value="submit" accesskey="s">  <input
class="button" type="reset" value="start over" accesskey="o">
</center>
</td>
</tr>
</table>
</form>
</div>


<img
src="http://www.w3.org/Icons/valid-html401";
alt="Valid HTML 4.01!" height="31" width="88">

</body>
</html>
<?} else {
header("location: login.html");}?>
[end of guestbook_add.php]
then the form gets submitted to the next one:
[guestbook_review2: give users the choice to back out before its too late
and type in again]
<?php
session_start();
$_SESSION['date']=$date;
$HTTP_SESSION_VARS['name']=$name;
$HTTP_SESSION_VARS['email']=$email;
$HTTP_SESSION_VARS['website']=$website;
$HTTP_SESSION_VARS['referred']=$referred;
$HTTP_SESSION_VARS['comments']=$comments;
if(!empty($_SESSION['username'])){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/transitional.dtd";>
<html>
<head>
<title>Confirm your post</title>
<LINK rel="stylesheet" href="style/rnjresort.php" type="text/css">
</head>

<body>
<h1>Confirm your post</h1>
<div>
<table summary="Instructions for Confirming your post">
<tr>
<td>
<p>Review your information below. If you need to return to the form to
change anything just click the start over button. If you are satisfied with
your listing click the continue button.</p>
</td>
</tr>
</table>
</div>

<div>

<table summary="Your posting information is below">
<tr>
<td>
Name:
<?php echo "$HTTP_SESSION_VARS[name]";?>
</td>
</tr>
<?php
if(!empty($HTTP_SESSION_VARS['email'])){?>
<tr>
<td>
<?php echo "<a href=\"mailto:$HTTP_SESSION_VARS[email]\";>My Email
address</a> ($HTTP_SESSION_VARS[email])";?>
</td>
</tr>
<?php }else{echo "";}
if(!empty($HTTP_SESSION_VARS['website'])) {?>
<tr>
<td>
<?php echo "<a href=\"http://$HTTP_SESSION_VARS[website]\";>My web site</a>
($HTTP_SESSION_VARS[website])";?>
</td>
</tr>
<?php } else {echo "";}
if(!empty($HTTP_SESSION_VARS['referred'])){?>
<tr>
<td>
Where did you hear about us?<br>
<?php echo "$HTTP_SESSION_VARS[referred]";?>
</td>
</tr>
<?php } else { echo "";}?>
<tr>
<td>
Comments:<br>
<?php echo "$HTTP_SESSION_VARS[comments]";?>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td>
<input class="button" type="button" value="continue>>" accesskey="c"
onclick="window.location='guestbook_save.php';"
onkeypress="window.location='guestbook_save.php';">
</td>
<td>
<input class="button" type="button" value="start over" accesskey="s"
onclick="window.location='admin.php';"
onkeypress="window.location='admin.php';">
</td>
</tr>
</table>
</div>


<img
src="http://www.w3.org/Icons/valid-html401";
alt="Valid HTML 4.01!" height="31" width="88">

</body>
</html>
<?}else{
header("location: login.html");}?>
[end of guestbook_review2.php]
if they hit the continue button it takes them to guestbook_save.php, saves
to db and returns to admin.php...
[guestbook_save.php: saves and returns to admin page]
<?php
session_start();
if(!empty($_SESSION['username'])){
//check if name and comments are empty
//if so then return an error
//otherwise save
if($_SESSION['date']=="")
$_SESSION['date']=NULL;

if(!empty($HTTP_SESSION_VARS['name']) &&
!empty($HTTP_SESSION_VARS['comments'])){
include("libs/conf.db");
mysql_connect($host, $mysqluser, $mysqlpwd);
mysql_query("insert into rnjresort.guestbook values(NULL, '$_SESSION[date]',
'$HTTP_SESSION_VARS[name]', '$HTTP_SESSION_VARS[email]',
'$HTTP_SESSION_VARS[website]', '$HTTP_SESSION_VARS[referred]',
'$HTTP_SESSION_VARS[comments]')")||die(mysql_error());
//include("mail.php");
header("location: admin.php");
} else { ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/transitional.dtd";>
<html>
<head>
<title>error!</title>
<LINK rel="stylesheet" href="style/rnjresort.php" type="text/css">
</head>


<body>
<h1>ERROR</h1>


<div>
<table>
<tr>
<td>
<p>Error: At least name and comments are required to post here.</p>
</td>
</tr>
</table>
</div>

<div>
<input class="button" type="button" value="OK" accesskey="o"
onclick="window.location='form.html';"
onkeypress="window.location='form.html';">
</div>
</body>
</html>
<?php }}
else {
header("location: login.html");}?>

[end of guestbook_save.php]

--- End Message ---
--- Begin Message ---
* Thus wrote Andy B ([EMAIL PROTECTED]):
> <?php
> session_start();
> $_SESSION['date']=$date;
> $HTTP_SESSION_VARS['name']=$name;
> $HTTP_SESSION_VARS['email']=$email;
> $HTTP_SESSION_VARS['website']=$website;
> $HTTP_SESSION_VARS['referred']=$referred;
> $HTTP_SESSION_VARS['comments']=$comments;

You should probably read http:/php.net/session more carefully.

> <snip oodles of unread code>


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

--- End Message ---
--- Begin Message ---
"Randall Perry" <[EMAIL PROTECTED]> skrev i meddelandet
news:[EMAIL PROTECTED]
> Solved my main problem. I was assuming that variables registered with
> $_SESSION were passed by reference. Apparently they're passed by value.

Wouldn't make very much sense to pass by reference, would it? Seems to me
like that would kill all values after the script finish... The logic would
be the reverse, to make $order a reference to $_SESSION['order']... Oh, and
have a look at serialize().. :)

> I moved the line '$_SESSION['order'] = $order;' from the top of the page 1
> php code (before any properties for the order object are set), to the end
of
> the php code -- now everything's getting passed to page 2 ok.
>
> This is an important point that should probably be in the docs section on
> sessions.
>
> When I coded using session_register() with register_globals on registering
> the variable before making changes to it worked.
>
> The '[error] PHP Notice:  Undefined variable:  _SESSION' happened, I
think,
> because there were no session variables to pass so PHP killed the session.
>
That sound VERY wierd to me.. But possible I guess..
>
> > Had a previous thread on this issue. Started working on it again and am
> > running into new bugs, so thought I'd start a new thread.
> >
> > Read thoroughly through session examples in docs and here's how I tried
to
> > pass an object from one page to another:
> >
> >
> > page 1
> > ______
> >
> > // include class definition file
> > include_once "order_classes.php";
> > // start session
> > session_start();
> > // create instance of class Order
> > $order = New Order();
> > // assign class instance to $SESSION[]
> > $_SESSION['order'] = $order;
> >
> > page 2
> > ______
> >
> > // include class definition file
> > include_once "order_classes.php";
> > // start session
> > session_start();
> > // assign $_SESSION['order'] object to variable
> > $order = $_SESSION['order'];
> >
> >
> > I'm getting the following error when page 2 loads:
> > [error] PHP Notice:  Undefined variable:  _SESSION
> >
> > Any ideas?
> >
> >
> >
> >
> >
> > Pertinent PHP vars:
> > ___________________
> >
> > Darwin systame.cniweb.net 7.2.0 Darwin Kernel Version 7.2.0: Thu Dec 11
> > 16:20:23 PST 2003; root:xnu/xnu-517.3.7.obj~1/RELEASE_PPC Power
Macintosh
> >
> > Build Date Apr 2 2004 12:58:17
> >
> > Configure Command  './configure' '--with-apxs=/usr/sbin/apxs'
'--with-pgsql'
> > '--with-xml' '--with-openssl=/usr/local/ssl' '--with-pear'
> > '--with-curl=/usr/lib'
> >
> > Apache/1.3.29 (Darwin) DAV/1.0.3 mod_perl/1.29 PHP/4.3.5 mod_jk/1.2.4
> > mod_ssl/2.8.16 OpenSSL/0.9.7c
> >
> > register_globals        Off     Off
> >
>
> -- 
> Randall Perry
> sysTame
>
> Xserve Web Hosting/Co-location
> Website Development/Promotion
> Mac Consulting/Sales
>
> http://www.systame.com/

--- End Message ---
--- Begin Message ---
When someone pulls up a form and fills it out, they subsequently submit that
form.  When submitting a form your browser creates a one on one connection
with the server where the server inputs the data from your form before or
after the next person without mixing the two.  (no matter if the two
submitted simultaneously it know who is who)

-----Original Message-----
From: Andy B [mailto:[EMAIL PROTECTED]
Sent: Sunday, April 04, 2004 1:35 PM
To: [EMAIL PROTECTED]
Subject: [PHP] php as default "value" in html


> > is there any way to keep multiple users using the same form from mixing
up
> > variables

>>I have no idea what you mean here.

if user #1 logs in and starts using a form and then user #2 logs in and
starts using the same one as user #1, say by chance they submit it at the
same time... is there any way to keep the forms from overwriting each
other...

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
--- Begin Message ---
"When someone pulls up a form and fills it out, they subsequently submit
that
form.  When submitting a form your browser creates a one on one connection
with the server where the server inputs the data from your form before or
after the next person without mixing the two.  (no matter if the two
submitted simultaneously it know who is who)
"

didnt know that tnx for the info

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

Supposed I have an array of objects, like:

$this[$i]->property1
$this[$i]->property2

is there any 'cheap' way to sort the array according to the values of
property1?

The only way I thought I would be able to accomplish this is to
transform this into an assoc array or something like it and then sort
it ...

I found plenty of functions for dealing with plain arrays, but nothing
- easy to use - for this. Am I just blind or do I finally have to write
a function doing that on my own?
And if so - anyone who had to do this before and come up with a really
fast solution (I guess my attempts would work - but not very efficient)

Thanks

Richard

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

Monday, April 5, 2004, 1:03:17 PM, you wrote:
RH> Hi there,

RH> Supposed I have an array of objects, like:

$this[$i]->>property1
$this[$i]->>property2

RH> is there any 'cheap' way to sort the array according to the values of
RH> property1?

RH> The only way I thought I would be able to accomplish this is to
RH> transform this into an assoc array or something like it and then sort
RH> it ...

RH> I found plenty of functions for dealing with plain arrays, but nothing
RH> - easy to use - for this. Am I just blind or do I finally have to write
RH> a function doing that on my own?
RH> And if so - anyone who had to do this before and come up with a really
RH> fast solution (I guess my attempts would work - but not very efficient)

RH> Thanks

RH> Richard


This should do what you want, though I think your example is
confusing...

<?php
function cmp_ascending($k1, $k2) {
        global $a;
        if ($a[$k1]->property == $a[$k2]->property) {
                        return 0;
        }
        return ($a[$k1]->property < $a[$k2]->property) ? -1 : 1;
}
class test{
        var $property = 0;
        function test($val){
                $this->property = $val;
        }
}
$a[] = new test(7);
$a[] = new test(1);
$a[] = new test(5);
$a[] = new test(3);
print_r($a);
uksort($a, "cmp_ascending");

while (list($key, $value) = each($a)) {
        echo "$key: $value->property <br>";
}

-- 
regards,
Tom

--- End Message ---
--- Begin Message ---
* Thus wrote Monty ([EMAIL PROTECTED]):
> 
>     session_start()
> 
>     if (!$_SESSION['loggedin']) {
> 
>         session_destroy();
>         header("Location:/login.php");      // Send to Log-In page.
>     }
> 
> Is this the most efficient way to do this? I'd prefer to not have to start
> then immediately destroy the session if it's possible to first know whether
> a session exists without starting it.

Yes.  But you dont *need* to destroy() the session.  btw, your
Location header should be like this:

   Location: http://domain.com/login.php


> 
> I have my site set to store the PHPSESSID in a cookie only (not passed via
> URL), so, would checking for the existence of $_COOKIE['PHPSESSID'] be a
> reliable way of doing this?

A cookie can easiy be sent without ever being assigned one. Doing
this will open a large hole in your security model.

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

--- End Message ---
--- Begin Message --- Monty writes:

Hi, is there any way to know if a site visitor has an existing session
without having to first start the session using session_start()?


I'm trying to avoid starting a valid session unless the visitor has been
authenticated and logged in, so, here's what I do now at the top of every
page:


session_start()

if (!$_SESSION['loggedin']) {

session_destroy();
header("Location:/login.php"); // Send to Log-In page.
}


Is this the most efficient way to do this? I'd prefer to not have to start
then immediately destroy the session if it's possible to first know whether
a session exists without starting it.


I have my site set to store the PHPSESSID in a cookie only (not passed via
URL), so, would checking for the existence of $_COOKIE['PHPSESSID'] be a
reliable way of doing this?

I've been doing exactly that, it works great. I use,


$sessid = $_COOKIE[PHPSESSID];

if ( isset($sessid) ) {
session_start();
}


I use 'if( isset($sessid) )' in the rest of the code if there are things that should only be done if there is a session. Only my login authentication page starts the session if there isn't a cookie. Of course, for security you ought to verify the session after starting it, and unset $sessid (and destroy_session() ) if something screwy is going on.

The reason I set things up like this is so that users are not bothered with cookies unless they need to be. I use cookies for the administration side of the site, but casual users don't need a session, so why should they have a cookie? -- I'm a not a proponent of passing around useless data :).



Any other suggestions are appreciated!


Monty

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Aaron VonderHaar
([EMAIL PROTECTED])

--- End Message ---
--- Begin Message ---
* Thus wrote Matt Palermo ([EMAIL PROTECTED]):
> I have a page where I am stripping the html tags down.  Basically, it will
> allow certain tags, but still strips out ALL attributs of the tag, so it
> works something like this:
> 
> $allowed_tags = "<p><br><strong><b><i><u>";
> //  State which tags are allowed
> $info = strip_tags($info, $allowed_tags);
> // Strip all unwanted tags out
> $allowed_tags = "p|br|strong|b|i|u";
> // Convert tags for regular expression
> $info = preg_replace("/<(?!".$allowed_tags.")[^>]*>/", '<\1>', $info);    //
> Strip out all attributes from allowed tags

You might wont to try a look behind assertion:
  /< (?<=p|br) [^>]+ >/x


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

--- End Message ---
--- Begin Message ---
This problem only seems to be happening on one of the virtual hosts on this
system, and I cannot seem to figure out just what it is.

I have a simple form posting to search.php, with a text input field, with a
name of 'search'. And since this site is an internal site, register globals
are on, as we are not worried about anyone misuing any of the variables in
use.

In any even, this problem was noticed about a week ago, and it did not exist
before.

After inputting some text into the search box, in this example, 'var', sans
quotes, and i hit submit... on the next page, I am just having it return
just $search for testing, and, it comes back sporadically with: 'var', but
most of the time, it comes back with:

var&^!#ndda</form>

or

var4194092d098240928d12ed

or 

var#c0c0c0

or

varput type="text"

etc. etc. it seems soemthing is somehow corrupting the variables, and I
cannot seem to figure out what it might be. This has been working fine until
about the past week, and I cannot think of any major changes that may have
happened within the past week that could have caused this... and as I
mentioned above, it only seems to be this one virtual host. And ai also
compared with backups of all of y included files at the beginnings and ends
of each script from before this problem started happening, and I can see no
major changes in any of them. And, this is happening around all php scripts
on the site...

I am running 4.3.4, with apache 2, FreeBSD 4.8, and the data is all stored
on a vinum partition. The server has been rebooted, a fresh install of
php... tried setting output buffering on and flushing it at the end of the
script(s), however, they seem to get hacked up when doing that, so I stay
away from that band-aid fix...I am using sessions as well, as a few scripts
within the site store session variables... and also using SMBAuth to do
authentication from our domain controller...

I've been pulling out my hair for 4 days straight on this issue, and I am
all out of ideas, any help would be *GREATLY* appreciated!

--- End Message ---
--- Begin Message ---
For some reason mail is being sent from nobody to nobody every time.  I
have tried hardcoding the sendmail path, which is the default anyway,
and it still doesn't work?  

Has anyone encountered this before?

--- End Message ---
--- Begin Message ---
Details?
How are you calling the mail function?

At 11:59 PM 4/4/2004, Jonathan Villa wrote:
For some reason mail is being sent from nobody to nobody every time.  I
have tried hardcoding the sendmail path, which is the default anyway,
and it still doesn't work?

Has anyone encountered this before?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
--- Begin Message ---
$from = "From: ".$email."\n";
$from .= "Reply-to: ".$email."\n";
$message = "The following person has been added to the Mailing
List.\n\n";
$message .= $name." (".$email.") from ".$city." has been added to the
mailing list.";

if(mail("[EMAIL PROTECTED]","Added To Mailing
List",stripslashes($message),$from) == false)
die('failed');


On Mon, 2004-04-05 at 02:13, hitek wrote:
> Details?
> How are you calling the mail function?
> 
> At 11:59 PM 4/4/2004, Jonathan Villa wrote:
> >For some reason mail is being sent from nobody to nobody every time.  I
> >have tried hardcoding the sendmail path, which is the default anyway,
> >and it still doesn't work?
> >
> >Has anyone encountered this before?
> >
> >--
> >PHP General Mailing List (http://www.php.net/)
> >To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
--- Begin Message --- Try getting rid of the stripslashes, or if you need to remove them, add an additional "\n" to the message after the strip slashes, like so:

if(mail("[EMAIL PROTECTED]","Added To Mailing List",stripslashes($message)."\n",$from) == false) die('failed');


Also, see http://www.php.net/manual/en/function.mail.php if you haven't checked there yet. There are some good examples.



At 12:17 AM 4/5/2004, Jonathan Villa wrote:
$from = "From: ".$email."\n";
$from .= "Reply-to: ".$email."\n";
$message = "The following person has been added to the Mailing
List.\n\n";
$message .= $name." (".$email.") from ".$city." has been added to the
mailing list.";

if(mail("[EMAIL PROTECTED]","Added To Mailing
List",stripslashes($message),$from) == false)
die('failed');


On Mon, 2004-04-05 at 02:13, hitek wrote: > Details? > How are you calling the mail function? > > At 11:59 PM 4/4/2004, Jonathan Villa wrote: > >For some reason mail is being sent from nobody to nobody every time. I > >have tried hardcoding the sendmail path, which is the default anyway, > >and it still doesn't work? > > > >Has anyone encountered this before? > > > >-- > >PHP General Mailing List (http://www.php.net/) > >To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
--- Begin Message --- Five wrote:
Windows 98 se

When using the dos window command line for mysql commands everything works fine except 
if I try to use the paste function to paste
pre-typed commands, the window freezes. This is aaannnoooyyyiiinnnggg.

This is PHP -- MySQL is down the hall to the left.

--- End Message ---
--- Begin Message --- Russell P Jones wrote:
Basically there is a php page... lets say

http://www.myserver.com/outputpage.php

that picks up some info from a database and echos it out as plain text to
the browser.

I want to use php in a different file to go pick up whatever is printed
out to that file and save it to a variable..

You can use file() fopen() (if you have url wrappers on), and then deal with the data.


You can also use a browser emulator (snoopy.sourceforge.net) to get the information.

Finally, you can create a webservice at outputpage.php and get the results as XML.
--- End Message ---
--- Begin Message --- Hi everybody,

I'm sure this has been mentioned hundreds of times before, but why is preg_replace apparently ignoreing my limit when I pass it an array of text to work on (with just one string each for pattern and replacement)? Is it only obeying per element of the subject? If so, is there a way to correct this unwanted behaviour, maybe through setting a constant or something?

Cheers!

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

--- End Message ---
--- Begin Message --- On 03/04/2004, at 5:37 AM, Andy B wrote:

is it ok practice to put code like the code to put results from an mysql query into a combo box into an include file and then just include it wherever its needed? the code i can see would have to be used at least 5 times in the same page....

Typically if there's something you want to use/do more than once, you'd either:


a) include it from it's own file (as suggested), or
b) declare it as a function, and then group the functions into logical include files for certain parts of the website


If you had this one-off problem, then you'd just include it PHP code inline where needed:

<? include("inc/combobox.php"); ?>

If you had 4 or 5 date & time functions you used site-wide, I'd declare them each as functions, then group those date and time functions into a single include file (eg datetime.php), include it at the top of each page (or where needed), and call the functions as needed.

---
Justin French
http://indent.com.au

--- End Message ---
--- Begin Message ---
Hi all,
I know that is possible to built desktop applications with PHP (applications that run 
only on desktop and not client applications).
How is possible this?
Is necessary use the php standard or there is help from other application or 
particular tool?
Can anyone show me how is possible this with example or link to resorces about this?
Thanks in advance,

Francesco
[EMAIL PROTECTED]


--- End Message ---
--- Begin Message ---
PHP-GTK

Am Montag, 5. April 2004 10:43 schrieb [EMAIL PROTECTED]:
> Hi all,
> I know that is possible to built desktop applications with PHP
> (applications that run only on desktop and not client applications). How is
> possible this?
> Is necessary use the php standard or there is help from other application
> or particular tool? Can anyone show me how is possible this with example or
> link to resorces about this? Thanks in advance,
>
> Francesco
> [EMAIL PROTECTED]

--- End Message ---

Reply via email to