-1

I have this code:

<?php $myarray->appends(['action' => $_GET['desc']])->render() ?>

When there is action argument in the url, then my code works as well. Otherwise it throws this error message:

Undefined index: action (View: /var/www/html/myweb/resources/views/mypage.blade.php)


So I need to do this ->appends(['action' => $_GET['desc']]) dynamically. Something like this:

<?php
  if ( isset($_GET['desc']) ) {
      $append_param = "->appends(['action' => $_GET['desc']])";
  } else {
       $append_param = "";
  }

  $myarray.$append_param->render();
?>

But I'm pretty much sure my code won't work .. I wore code about just for showing you what was my point.

Anyway, can anybody tell me how can I do that in a right way?


All I'm trying to do is: appending action argument to the pagination-links if it already exists. Otherwise I don't want to append it.

stack
  • 10,280
  • 19
  • 65
  • 117
  • I'm not real big into Laravel (not fond of the syntax), but from what I know about coding **_you probably shouldn't do that_** – Jhecht Dec 05 '16 at 06:33
  • Possible duplicate: [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index). I also hope you read and take in everyone's warnings about what you are trying to do. You're basically giving attackers a loaded gun. – M. Eriksson Dec 05 '16 at 06:41

3 Answers3

0

You should never take input from a user-controllable source and execute it. Doing this is a major security risk, and it will probably open more security holes in your server than you'd be able to patch in a lifetime.

If you really, really must do this, you can use eval(). But please, make sure you understand the security implications of this.

aaaaaa123456789
  • 5,541
  • 1
  • 20
  • 33
0

i think you are just horribly overthinking the whole situation. you don't have to put your code in a string to execute it conditionally, you can just write it as code itself.

having no further information about how your class works internally, the most straightforward and basic way would be:

if ( isset($_GET['desc']) ) {
    $myarray->appends(['action' => $_GET['desc']])->render()
} else {
    $myarray->render();
}
Franz Gleichmann
  • 3,420
  • 4
  • 20
  • 30
-1

You cannot do that.

First, if you want to check for a valid key in an array, use "array_key_exists(key, array)" which is hugely recommanded.

if ( array_key_exists('dec',$_GET) ) {

Then, if I understand, you want to "append" data to an Array ? That means adding a value to a key in this array. This can be done easily with PHP : $array [ $key ] = $value;

Be very careful on your variable names : $myarray.$append_param->render(); This makes no sense, if it's an array, you only should use [] on it (or array functions), but if it's an object, you only can use -> on it.

Yoann
  • 76
  • 3