xltmt
xltmt

Reputation: 61

PHP Insert '&' / and text symbol in a string

I am trying to insert the & symbol in my PHP code but instead its being converted into ASCII &.

Below is my code:

$symbolAnd= '&';
echo $symbolAnd;
foreach ($clientLists as $clientList =>$clientValue){
    //Get values
    echo  $clientList .' - '.$clientValue['client-pass'] . '<br/>';

    include('index.php?client='.$clientList.$symbolAnd.'pass='.$clientValue['client-pass']);
}//foreach ($clientLists as $clientList){

I am getting this error in my error_log;

PHP Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'index.php?client=john-jones&amp;pass=123456' for inclusion

Upvotes: 0

Views: 1397

Answers (2)

morganbaz
morganbaz

Reputation: 3117

Your code is correct. It displays an &amp; because the output of error.log is what should be outputted to the user in HTML if error were displayed. The real problem here is that you are trying to include a file and passing GET parameters, but that however is not possible. When you include (although the same happens for all its variants like require and include_once) a file, you have to see it as a copy and paste of that file into your current PHP file. You are trying to get from your filesystem the file with the name being index.php?client=WhateverClientListIs&pass=WhateverClientPassIs, which does not exist!

To fix this, you can do the following:

 $_GET['client'] = $clientList;
 $_GET['pass'] = $clientValue['client-pass'];
 include('index.php');

Upvotes: 3

u_mulder
u_mulder

Reputation: 54841

Whatever symbols you put into your filename (& or &amp;) this approach will not work as you expect unless you really have files which names are

index.php?client=client1&password=1234
index.php?client=client2&password=123123
index.php?client=client3&password=qwerty

I mean real files. Cause include includes file by name. And if you want to include index.php you should include('index.php')

Upvotes: 1

Related Questions