We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10076
    • 1,024 Posts
    Hi,

    I would like to know if it would be possible to hide or show a delete or edit link on a page depending on the webuser beeing logged in. Below a bit of script that hides shows depending on a var. What do I need to have it react on a webuser beeing logged in:

    // if var $website is not empty.. 
    if ($website ==!'') { 
       // echo this... 
        echo "<a href=\"http://$website\" target=\"new\"> 
    	<img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/www.png\" alt=\"Go to '.$name.' \'s website\" title=\"Go to '.$name.' 's website\" style=\"border:0\"></a>"; 
    } 


    This is an external script. not using modx globals or what was it again.
    Would be nice,

    Fr.
      • 2912
      • 315 Posts
      I’ve always tended to use getUserLoginID() function.

      If it’s greater than 0 then you are logged as the return value is your userid smiley

      if ($modx->getUserLoginID() > 0 ) { 
        // User logged in so show icon
          echo "<a href=\"http://$website\" target=\"new\"> 
      	<img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/www.png\" alt=\"Go to '.$name.' \'s website\" title=\"Go to '.$name.' 's website\" style=\"border:0\"></a>"; 
      } 
      else {
        // user not logged in
      }
      
        BBloke
        • 10076
        • 1,024 Posts
        Hi,

        would that work for scripts that just use mysql connect, and are not a snippet?
        I mean those scripts are running outside of modx, no? So how can they pick up that value.
        Would I need to use what was it again "modx global", or something. Or am I completely missing something here.

        fr.
          • 4041
          • 788 Posts
          If you are using this in a script outside a modx page but in the same directory as the index.php file, in that case you might be able to use $_SESSION values:

          <?php
          
          if(isset($_SESSION['webShortname'])){
             echo "whatever needs echoing";
          }
          
          ?>


          This post might also help:
          http://modxcms.com/forums/index.php/topic,28064.msg170552.html#msg170552
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 10076
            • 1,024 Posts
            Hi

            that did not do it. Heres the error: Call to a member function on a non-object .
            ****************************
            edit that error leads me to a suggestion that I might want to use global $modx; right after the function call?
            Anyone?
            ****************************

            Your second alternative does not show an error nor an icon where it should.
            So I am thinking of using this script as a snippet. But do not know what to alter.
            Currently I have a call to a snippet on that page:

            Here is that little snippet:
            <?php
            $pubchar = $_GET['pubchar'];
            
            
            $myscript = "http://www.pianoquartet.nl/assets/snippets/nbscripts/publist.php?idpd=48&idpmod=40&idpdel=73&pubchar=".$pubchar;
            
            include ($myscript);
            ?>


            here is the publist script, which I think should be inside a modx snippet to make it all cleaner. I know i did not escape stuff (apparently my deletion and edit querys are vurnerable to injections?) But one thing at a time:

            <?php
            
            
            
            $idpubdetails = $_GET['idpd'];
            $idpubmod     = $_GET['idpmod'];
            $pubchar      = $_GET['pubchar'];
            $idpubdelete  = $_GET['idpdel'];
            $idpub  = $_GET['idpub'];
            $abbreviation = $_GET['$abbreviation'];
            
            
            if (strlen($pubchar) > 0)
            
            
            {
            
               $select_subset = true;
            
               $letter = strtoupper(substr($pubchar,0,1));
            
               if (is_numeric($letter))
            
                  $first_is_number = true;
            
               else $first_is_number = false;
            
            }
            
            else $select_subset = false;
            
            
            
            //include('connections.php');
            
            //$result = mysql_query($sql) or die('ERROR: '.$query.' '.mysql_error());
            
            $letterlinks = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            
            
            
            echo '<a name="top"></a>';
            
            echo '<a href="index.php?id=3&pubchar=0">0-9</a> ';
            
            for ($i = 0; $i < 26; $i++):
            
            echo '<a href="index.php?id=3&pubchar='.$letterlinks[$i].'">'.$letterlinks[$i].'</a> ';
            
            endfor;
            
            /*
            
            while
            
            ($list = mysql_fetch_object($result)):
            
            $letter = strtoupper(substr($list->name, 0, 1));
            
            if ($prev_row != '0-9' && is_numeric($letter)):
            
            echo '<br /><a name="number"></a><b><u>0-9</u></b> ';
            
            echo '<a href="#top"><i>goto top</i></a><br />';
            
            $prev_row = '0-9';
            
            endif;
            
            if ($letter != $prev_row && !is_numeric($letter)):
            
            echo '<br /><a name="'.$letter.'"></a><b><u>'.$letter.'</u></b> ';
            
            echo '<a href="#top"><i>goto top</i></a><br />';
            
            $prev_row = $letter;
            
            endif;
            
            echo $list->name.'<br />';
            
            endwhile;
            
            */
            
            
            
            
            //echo "<br>select_subset = " . $select_subset;
            
            
            
            if ($select_subset)
            
            {
            
            // 001: Database query
            
            
            
              if ($first_is_number)
            
                 $query = "select * from publisher WHERE name  >= '0%' and name  <= '9%'";
            
              else $query = "select * from publisher WHERE name  like '".$pubchar."%'";
            
            
            
              $query .= " order by name  ASC";
            
            
            
            //echo "<br>query = " . $query;
            
            
            
            // 002: Execute query
            
            
            
              $res = mysql_query($query, $sqlConn);
            
            
            
              if (!$res)
            
              {
            
                 die ('Could not run query: ' . mysql_error());
            
              }
            
             
            echo "<br><br>";
            
            
            echo "   <table border=\"0\" align=\"left\" width=\"580\"valign=\"left\" color=\"#fafafa\" cellpadding=\"10\" cellspacing=\"2\">";
            
            
            
            
            
            echo "    <thead>";
            echo "    <tr>";
            echo "      <th scope=\"row\" bgcolor=\"#cccccc\">Name</th>";
            echo "      <th scope=\"row\" bgcolor=\"#cccccc\">Remarks</th>";
            echo "      <th scope=\"row\" bgcolor=\"#cccccc\">Options</th>";
            echo "    </tr>";
            echo "    </thead>";
            
            
            echo "    <tbody>";
            
            
            $i = 0;
              while ($row = mysql_fetch_array($res, MYSQL_BOTH))
            
            
            
            
            
            {
              $idpub      = $row[0];
              $abbreviation = $row[1];
              $name         = $row[7];
              $address      = $row[6];
              $street       = $row[3];
              $postcode     = $row[4];
              $city         = $row[5];
              $country      = $row[2];
              $website      = $row[8];
              $email        = $row[9];
              $phone        = $row[11];
              $oddities     = $row[10];
            
            
            
            
            echo "    <tr>";
            {
            echo "
            ";
            if($i%2 == 0)
            {
            echo "<tr bgcolor='#F4F6FA'>";
            $i++;
            }
            else
            {
            echo "<tr bgcolor='#E9EDF5'>";
            $i++;
            }
            }
            
            echo "      <td >".$name."</td>";
            echo "      <td >".$oddities."</td>";
            echo "      <td ><a href=\"/index.php?id=".$idpubdetails."&idpub=".$idpub."&pubchar=".$pubchar."\">
            <img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/details.png\" alt=\"Vieuw $name 's details\" title=\"Vieuw $name 's details\" style=\"border:0\"></a>";
            
            // als $website niet leeg is.. 
            if ($website ==!'') { 
               // laten we dit stukje zien 
                echo "<a href=\"http://$website\" target=\"new\"> 
            	<img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/www.png\" alt=\"Go to '.$name.' \'s website\" title=\"Go to '.$name.' 's website\" style=\"border:0\"></a>"; 
            } 
            echo"
            
            <a href=\"/index.php?id=".$idpubmod."&idpub=".$idpub."&pubchar=".$pubchar."\">
            <img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/edit.png\" alt=\"Edit $name 's details\" title=\"Edit $name 's details\" style=\"border:0\"></a>";
            if ($email ==!'') { 
            	echo "<a href=\"mailto:$email\">
            	<img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/email.png\" alt=\"Email $name\" title=\"Email $name\" style=\"border:0\"></a>";
            }
            echo"
            <a href=\"/index.php?id=".$idpubdelete."&idpub=".$idpub."&pubchar=".$pubchar."&abbreviation=".$abbreviation."\">
            <img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/delete.png\" alt=\"Delete $name\" title=\"Delete $name\" style=\"border:0\"></a></td>";
            echo "    </tr>";
            echo "    </tr>";
            
            
            }
            
            if(isset($_SESSION['webShortname'])){
              // User logged in so show icon
             echo"<a href=\"/index.php?id=".$idpubdelete."&idpub=".$idpub."&pubchar=".$pubchar."&abbreviation=".$abbreviation."\">
            	<img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/delete.png\" alt=\"Delete $name\" title=\"Delete $name\" style=\"border:0\"></a></td>";
            } 
            else {
              // user not logged in
            }
            
              echo "        </tbody></table>";
            
            }
            
            //mysql_close($sqlConn);
            ?>