0

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

3
  • Whatever you're triyng to do this will not work this way. Commented Sep 12, 2015 at 11:01
  • Hi, how can I go about it please? Thanks. Commented Sep 12, 2015 at 11:05
  • try using htmlspecialchars(). Commented Sep 12, 2015 at 11:12

2 Answers 2

3

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');
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. I can't use $_GET as the values are inside a multi dimensional array which are outputted using a foreach loop
And what? On each iteration set GET variables, or use global keyword.
I agree with this answer. You should be able to include your file this way, setting your GET values manually.
if allow_url_include is true you can include through HTTP by using an complet url infront.see doc page -> php.net/manual/en/function.include.php note your code may be less Secure
1

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.