php-general Digest 7 Jul 2008 10:27:09 -0000 Issue 5555

Topics (messages 276338 through 276346):

Re: Multiple words str_shuffle
        276338 by: David Giragosian
        276339 by: Brady Mitchell

Re: No Database Connection possible (mySQL)
        276340 by: Chris Haensel

Problem with special characters - PHP & AJAX
        276341 by: bperquku
        276345 by: Michael Kubler
        276346 by: Jason Norwood-Young

Looking for a reasonable explanation as to why $_REQUEST exists
        276342 by: mike

Re: Session variables disappear (some of them only)
        276343 by: karma
        276344 by: Chris

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 ---
On 7/6/08, Ron Piggott <[EMAIL PROTECTED]> wrote:
>
>
> I am trying to scramble individual words and/or phrases.
>
> When it is a phrase I would like to keep the letters of each word
> together, with a space between each one.  The code I have so far is
> below.  I use PHP 4.4.7.  The code below is fine for a single word; it
> is phrases that I am now trying to accommodate.
>
>
> An example:
>
> rise and shine
>
> Desired output:
>
> I S R E  N A D   E H I S N
>
> Thanks for your help,
>
> Ron
>
>
>
> $keyword might be
>
> $keyword = str_shuffle(strtoupper($keyword));
>
> $buffer = "";
>
> for ($count = 0; ($count < strlen($keyword)); $count++) $buffer .=
> $keyword{$count}." ";
>
> $keyword = trim($buffer);
>
> unset($buffer);


Once the individual words have had their letters shuffled, explode the
sentence on a space, then use the shuffle function (
http://us3.php.net/manual/en/function.shuffle.php) to, um, shuffle the
array.

David

--- End Message ---
--- Begin Message ---
On Jul 6, 2008, at 305PM, Ron Piggott wrote:


I am trying to scramble individual words and/or phrases.

When it is a phrase I would like to keep the letters of each word
together, with a space between each one.  The code I have so far is
below.  I use PHP 4.4.7.  The code below is fine for a single word; it
is phrases that I am now trying to accommodate.

$orig_phrase = 'rise and shine';

// Split the phrase into an array with each word as an element
$array_phrase = explode(' ',$orig_phrase);

// Cycle through the array processing one word at a tie
foreach($array_phrase as $key => $value)
{
// $orig_value is used in the do while loop to ensure that the "shuffled" string is not the original string.
        $orig_value = $value;
        
// Shuffle the string, and continue to do so until the returned string is not the original string
        do{
                $value = str_shuffle($value);   
        } while($value == $orig_value);

        // Uppercase value
        $value = strtoupper($value);
        
        // Insert a space after every letter
        $value = chunk_split($value,1,' ');
        
        // Set array value to newly formatted version
        $array_phrase[$key] = $value;
}

// I'm using &nbsp; so it will echo and be obvious that there are two spaces between words.
$scramble_phrase = implode('&nbsp;&nbsp;',$array_phrase);

echo $orig_phrase;
echo '<br />';
echo $scramble_phrase;

Everything after the do...while loop can be easily combined into one line; I left it as separate lines for clarity.

Brady

--- End Message ---
--- Begin Message ---
-----Original Message-----
From: M. Sokolewicz [mailto:[EMAIL PROTECTED] 
Sent: Friday, July 04, 2008 10:18 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: [PHP] Re: No Database Connection possible (mySQL)

David Robley wrote:
> Aviation Coding wrote:
> 
>> Hi all,
>>
>> I am having problems with a connection to a mysql database.
>>
>> I am using
>>
>> ----
>> function con()
>> {
>> mysql_connect("localhost","user","pass") or die(mysql_error());
>> mysql_select_db("tava") or die(mysql_error());
>> }
>> ----
>>
>> Now, when I call the _function_ (!)
>> ----
>> con() or die("no con");
>> ----
>> I get the "no con" output.
>>
>> When I call the mysql_connect and mysql_select directly before executing
a
>> query, I get some DB output. But that won't work when I am using the
>> function...
>>
>> Any ideas would be greatly appreciated.
>>
>> Cheers!
>>
>> Chris
> 
> I think you need to return something from the function, like true if the
> connection/select worked, false if not.
> 
> 
> 
> Cheers
You are correct.

function foo() {
  // does something
}

var_dump(foo()); // returns NULL

why? because you don't explicitly return anything. If you did, that'd be 
the return value. So if you did:
function bar() {
    // does something
    return true;
}

var_dump(bar()); // return true

Now, your script assumes a return-value:
baz() or somethingElse();
is an expression. This basically says:
if(!baz()) {
    somethingElse();
}

Now, return (implicitly) null will result in (trough lazy comparison) a 
false value (*null == false*, null !== false), which then triggers your 
die() condition. Returning a meaningful value will get rid of that.

Other than that, choose 1 of 2 techniques:
1. error out inside the function itself (like you do now)
OR
2. error out based on the return-value of the function (like you do now 
ASWELL)
don't combine 1 and 2, stick with either, but not both.

function con()
{
mysql_connect("localhost","user","pass") or die(mysql_error());
mysql_select_db("tava") or die(mysql_error());
}
con();

works. The following would work too:
function con()
{
$r = mysql_connect("localhost","user","pass");
if(!$r) {
    return false;
}
$r2 = mysql_select_db("tava");
if(!$r2) {
    return false;
}
return true;
}

con() or die('no conn');

would also work correctly. So, stick with either, don't mix em.

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

That worked like a charm. Thanks a lot. That one really had me stuck for
quite some hours. Thank God for this list *g*

Wish you all a great start into the week!

Chris


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

I'm writing a simple dictionary with php and ajax. It works perfects with
firefox but not in IE.
Here is the link

http://kllapa.com/fjahalori/test.html

I used alerts in js and find out that in the following function:

function updateMsgOnBrowser(testXML) {

        var test = testXML.getElementsByTagName("test")[0];
        var message=new Array(20);
        var m = new Array(20);
        var td = new Array(20);
        var i;
        for (i=1;i<=10;i++){
        message[i]=testXML.getElementsByTagName("message"+i)[0];
        message[i+1]=testXML.getElementsByTagName("message"+i+"r")[0];
                if (message[i]!=null){m[i] = message[i].firstChild.nodeValue;}
else{m[i]=""}
                if (message[i+1]!=null){m[i+1] = 
message[i+1].firstChild.nodeValue;}
else{m[i+1]=""}
                td[i]= document.getElementById("td"+i);
                td[i+1]= document.getElementById("td"+i+"r");
        td[i].innerHTML=""+m[i];
                td[i+1].innerHTML=""+m[i+1];
    }
}

the line 

message[i]=testXML.getElementsByTagName("message"+i)[0];

becomes null in IE when tag message contains special character (ë, ç, Ë, Ç,
etc.). Why this works perfect in Firefox?

Any idea what could be the issue??


Thanks in advance
-- 
View this message in context: 
http://www.nabble.com/Problem-with-special-characters---PHP---AJAX-tp18311031p18311031.html
Sent from the PHP - General mailing list archive at Nabble.com.


--- End Message ---
--- Begin Message --- Are the messages being sent as UTF-8 or something else? Is the server sending the headers as something different to that listed in the header? Actually, looking at it, you don't have a valid DOC-TYPE <http://validator.w3.org/check?uri=http%3A%2F%2Fkllapa.com%2Ffjahalori%2Ftest.html&charset=%28detect+automatically%29&doctype=Inline&group=0>, nor character Encoding set.

It might be something else, but I haven't played with enough AJAX to debug the javascript.

Michael Kubler
*G*rey *P*hoenix *P*roductions <http://www.greyphoenix.biz>



bperquku wrote:
Hi all,

I'm writing a simple dictionary with php and ajax. It works perfects with
firefox but not in IE.
Here is the link

http://kllapa.com/fjahalori/test.html

I used alerts in js and find out that in the following function:

function updateMsgOnBrowser(testXML) {

        var test = testXML.getElementsByTagName("test")[0];
        var message=new Array(20);
        var m = new Array(20);
        var td = new Array(20);
        var i;
        for (i=1;i<=10;i++){
        message[i]=testXML.getElementsByTagName("message"+i)[0];
        message[i+1]=testXML.getElementsByTagName("message"+i+"r")[0];
                if (message[i]!=null){m[i] = message[i].firstChild.nodeValue;}
else{m[i]=""}
                if (message[i+1]!=null){m[i+1] = 
message[i+1].firstChild.nodeValue;}
else{m[i+1]=""}
                td[i]= document.getElementById("td"+i);
                td[i+1]= document.getElementById("td"+i+"r");
        td[i].innerHTML=""+m[i];
                td[i+1].innerHTML=""+m[i+1];
    }
}

the line
message[i]=testXML.getElementsByTagName("message"+i)[0];

becomes null in IE when tag message contains special character (ë, ç, Ë, Ç,
etc.). Why this works perfect in Firefox?

Any idea what could be the issue??


Thanks in advance

--- End Message ---
--- Begin Message ---
On Mon, 2008-07-07 at 19:35 +0930, Michael Kubler wrote:
> Are the messages being sent as UTF-8 or something else? Is the server 
> sending the headers as something different to that listed in the header?
> Actually, looking at it, you don't have a valid DOC-TYPE 
> <http://validator.w3.org/check?uri=http%3A%2F%2Fkllapa.com%2Ffjahalori%2Ftest.html&charset=%28detect+automatically%29&doctype=Inline&group=0>,
>  
> nor character Encoding set.

Yeah you might want to check out HTML Entities
(http://www.php.net/manual/en/function.htmlentities.php) - great help in
encoding. Then set a doc type too.


--- End Message ---
--- Begin Message ---
I have never had a use for this feature. To me it introduces another
register_globals style atttack vector. I see no need why people need
to combine post/get/etc variables into the same superglobal. I
actually run unset($_REQUEST) on it at the top of my library to
discourage its use.

For third party products which use it I tell people to combine it
themselves by using array_merge() - like $_REQUEST =
array_merge($_POST, $_GET) etc...

Anyway can someone here please give me a good reasoning why it should
exist? It isn't as easily abused as register_globals but when people
have a session variable they want to access and use $_REQUEST for it I
could easily override it by using a GET param on the url (depending on
how the order of globals get processed)

Simply put, I see no reason why people would not want to clearly
define where they are getting their input from. If for some reason
there is some need to lazily code something I would still say to do
something like:

if(isset($_GET['foo'])) {
 $foo = $_GET['foo'];
} elseif(isset($_POST['foo'])) {
 $foo = $_POST['foo'];
} else {
 $foo = 'default value';
}

... or just do the array merge.

But please someone maybe can justify this to me... I've been using
superglobals before I really understood how important they were and
then one day I see they introduced $_REQUEST and thought .. okay that
seems stupid. I finally am deciding to see if anyone can give me a
reason as to why this is useful and not just a lazy coding practice
that can lead to security risks.

You don't really know if your data is coming from GET, from POST, a
SESSION variable, etc...

I'd love to see a good discussion going on this. I might have
overlooked something important.

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

Hi,

Ted & Fabrice, thanks for your answers.

Sessions variables are only stored in a local file. The dir permissions are ok, and I've tried to store these files in another dir (/var/tmp/php) just to check.

The session id is transmitted via cookies only :

session.use_cookies = 1
session.use_only_cookies = 1    <== I've tried with 0 and 1
session.auto_start = 0
session.cookie_lifetime = 0

I guess the Session ID is correctly transmitted because the errors doesn't occur on the first 2 scripts. First, the login page requires cookies to be enabled, and this step is ok. Then the user has to choose something in a menu, this step is fine too : some variables are set according to the user choice and the user is redirected to the 3rd script. The errors occur on this one.

Between the 2nd and 3rd scripts, variables are created from a database query : it _can't_ fail and the results are cheched : no possible mistake here. I use this kind of method :

- the user chooses some "$id" (type and value tested, ok), then :

$res=pg_query($dbr, "select a, b, c, ..., from table where table_id='$id'");

if(pg_num_rows($res))
{
   list($_SESSION["a"], $_SESSION["b"], $_SESSION["c"], ...)=pg_fetch_row($res, 
0);

  pg_free_result($res);
  header("Location:my_third_script.php");
  exit();
}

Then the errors sometimes occur in my apache2/ssl_error_log (undefined index in $_SESSION variable). When I check the sess_12345789... file, some of the variables are missing : $_SESSION["a"] and ["b"] are there, but not $_SESSION["c"], even an empty one, it is just gone. That's all I know.

I would like to try to store my sessions variables in the main database, but it is quite difficult since the application is currently used by many people. I'll also have to upgrade a lot of scripts (a bit time consuming) to test this solution...


Regards,

C.


Fabrice VIGNALS a écrit :
Difficult to help you because there are many method of session :
- where do you store the sessions_variables : in local file, db or cookie ?
- how you transmit the session id, beetween pages(runtimes) : cookie, $GET link, database ?


Did you check the availability of user cookie if you use it ?
Because if in each page of your application you define a session variable it's sure it will be every time here. But the problem of session it's to transmit its ID between different pages, or session will be reset. If a user don't authorised cookie you must transmit the session id by db storage or $Get link.

Also I don't see, a php modification during the last upgrades to explain that's kind of session problem.




"karma" <[EMAIL PROTECTED]> a écrit dans le message de news:[EMAIL PROTECTED]

Hi !

I have a very weird issue since the last Apache upgrade (-> 2.2.8-r3, a month ago), but I'm not sure it is related (well, I'm pretty sure it's not).

Like many people, I've written an application that use PHP session variables, like $_SESSION["my_variable"].

Sometimes (it doesn't happen all the time), _some_ of these variables are not written in the session file and they are lost after a simple header("Location:...); (same domain). The session file is in the right directory (permissions are fine), but some of my variables are missing.

The facts :
- Apache 2.2.9 + PHP 5.2.6_rc4 running on a Gentoo (up-to-date)
- all my scripts begin with session_start(). I've tried to add session_write_close() before every header(Location:...) call, it doesn't help. - I didn't change anything in my program (it has been running just fine for 2 years), it just began to fail from time to time (I would say 10 times a day). There is no hidden unset() function : it would fail for everyone. - these variables are all set correctly, and they don't have reserved names. - only a few variables disappear, but they are always the same ones (could it depend on their position in the session file ?!?)
- the session files are very small (max 100ko)
- it seems that it doesn't depend on the browser, but IE6 and IE7 seem to be the most affected ones (it may be because my users mostly use these browsers). - I can't reproduce this issue from my local network (any OS/browser - it would be too easy :) - reverting to the previous stable Apache and/or PHP versions doesn't help.
- I didn't change any php.ini directive.

Any idea ?

Thanks !


PS: if you need more details, just ask. The only thing I can't do is pasting the code : the scripts are quite huge.



--- End Message ---
--- Begin Message ---
> Then the errors sometimes occur in my apache2/ssl_error_log (undefined
> index in $_SESSION variable). When I check the sess_12345789... file,
> some of the variables are missing : $_SESSION["a"] and ["b"] are there,
> but not $_SESSION["c"], even an empty one, it is just gone. That's all I
> know.

Sounds like for those situations, the user doesn't have one of the
options set (the database is returning a null value).

Check that by matching up whatever 'a' and 'b' are with what's in the
database.

-- 
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---

Reply via email to