Designer? Programmer? Enthusiast? Join Open Designs Now!

Open Designs Forum » Designer Talk

PHP Dynamic Popup

(27 posts)
  • Started 3 years ago by James
  • Latest reply from uberlemurguy

Tags:

  1. James
    Member

    Hi all,

    I am trying to set up a PHP dynamic pop, e.g.

    http://www.domain.com/?message=logged-in

    would produce a javascript pop up to say "You are now logged in" and

    http://www.domain.com/?message=logged-out

    would produce a javascript pop up to say "You are now logged out"

    If this was being done in ASP the code would look something like

    <% If Request("message") = "logged-in" Then %>
    {do login popup}
    <% End If %>

    What code would I use for PHP to achieve the same effect?

    Thanks everyone,

    James

    Posted 3 years ago #
  2. LobsterMan
    Key Master

    <?php if (isset($_GET['message'])) { ?>
    {do you popup here, using $_GET['message'] for the popup text,
     that way you don't need separate conditionals for each message}
    <?php } ?>
    Posted 3 years ago #
  3. James
    Member

    Thanks Joe,

    I have put this together:

    <?php if (isset($_GET['message'])) { ?>

    <script language="JavaScript" type="text/javascript">
    alert("<?php $_GET['message'] ?>")
    </script>

    <?php } ?>

    and have tried it with /?message=logged-out.

    The popup appeared, but there was no text. Where am I going wrong?

    Sorry - I'm not too competent with PHP...

    Thanks.

    Posted 3 years ago #
  4. LobsterMan
    Key Master

    <?php echo $_GET['message'] ?>
    This tell php to print out the variable to the HTML, where JS will pick it up

    Posted 3 years ago #
  5. James
    Member

    Thanks, I was missing "echo"!

    Posted 3 years ago #
  6. uberlemurguy
    Moderator

    That is very insecure. You should validate the text because someone could do http://www.domain.com/?message=&quot;); //any javascript OR PHP can go here insert bad serving trojan horses to all your vistors it is bad. use urlencode to avoid this http://us2.php.net/urlencode.

    <?php if (isset($_GET['message'])) { ?>
    
    <script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
    alert(&quot;<?php echo urlencode($_GET['message']) ?>&quot;)
    </script>
    
    <?php } ?>
    Posted 3 years ago #
  7. James
    Member

    Thanks Alec!

    I am wondering whether I would be better going back to plan 1 to avoid SQL injection and having tried Joe's method my links are looking something like:

    http://www.domain.com/?message=You+have+now+been+logged+out

    which I don't like, I admit

    http://www.domain.com/?message=logged-out

    isn't too pretty but the spiders should never find that link.

    Posted 3 years ago #
  8. uberlemurguy
    Moderator

    But if someone ever sees that link and they are well versed in XSS (cross site scripting, the example above) they will try it and may or may not report it. Going back to plan one:

    <?php
    if($_GET['message'] != &quot;&quot;) {
        switch ($_GET['message']) {
             case &quot;logged-out&quot;:
             $message = &quot;You have now been logged out&quot;;
             break;
    
             case &quot;logged-in&quot;:
             $message = &quot;You have now been logged in&quot;;
             break;
    
             default :
             $message = &quot;Whoa! That message doesn't exist!&quot;;
             break;
        }
    echo '<script type=&quot;text/javascript&quot;>
    alert(&quot;'.$message.'&quot;);
    </script>';
    }
    ?>
    Posted 3 years ago #
  9. James
    Member

    Thanks Alec you're a star.

    Posted 3 years ago #
  10. uberlemurguy
    Moderator

    Your welcome! Once you have written a nice CMS (LemurCMS) in php you can write anything!

    Posted 3 years ago #
  11. James
    Member

    Except....

    Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in \wp-content\themes\freshnews\includes\popup.php on line 3

    By the looks of it, PHP wants brackets somewhere...

    Edit: Fixed, I worked out where the brackets needed to be...

    Posted 3 years ago #
  12. uberlemurguy
    Moderator

    oops! I forgot the ('s around $_GET['message']

    revised code:

    <?php
    if($_GET['message'] != &quot;&quot;) {
        switch ($_GET['message']) {
             case &quot;logged-out&quot;:
             $message = &quot;You have now been logged out&quot;;
             break;
    
             case &quot;logged-in&quot;:
             $message = &quot;You have now been logged in&quot;;
             break;
    
             default :
             $message = &quot;Whoa! That message doesn't exist!&quot;;
             break;
        }
    echo '<script type=&quot;text/javascript&quot;>
    alert(&quot;'.$message.'&quot;);
    </script>';
    }
    ?>
    Posted 3 years ago #
  13. James
    Member

    I am having a strange isssue with this code. It works when on the homepage but not on any other page.

    I tested that the file is actually being loaded and it definately is but can't understand why it won't display the message on other pages.

    Does anyone have any idea what could cause this?

    Posted 3 years ago #
  14. uberlemurguy
    Moderator

    An example would be useful!

    Posted 3 years ago #
  15. James
    Member

    I've moved the site onto an Apache Server and the problem has gone!

    Thanks anyway.

    Posted 3 years ago #
  16. James
    Member

    How do I change this code so that if a user already has "?message={whatever message}" in the current URL it removes "?message={whatever message}"?

    <?php echo wp_logout_url(); ?>&redirect_to=http://<?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ?>?message=logged-out

    Basically the return URL is keeping the current "message" and adding "?message=logged-out" and is not showing the correct message e.g.

    http://www.domain.com/?message=logged-in?message=logged-out

    I have had a look on Google and found lots of pages showing ways to grab the current page URL but none on how to remove queries.

    Thanks!

    Posted 3 years ago #
  17. uberlemurguy
    Moderator

    $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]

    That is the current URL minus the query string.

    Posted 3 years ago #
  18. James
    Member

    Thanks for the quick reply but it's still not working properly.

    The url is http://techtrends.co.uk

    If you register then try to login you can see the problem.

    Posted 3 years ago #
  19. uberlemurguy
    Moderator

    $url = $_SERVER[&quot;SERVER_NAME&quot;].$_SERVER[&quot;REQUEST_URI&quot;];
    if(substr_count($url, '?') > 0) {
    	$url = strstr($visual[0], '?', true);
    }
    <?php echo wp_logout_url(); ?>&amp;redirect_to=http://<?php echo $url ?>?message=logged-out

    That should work. It finds the first occurrence of ? and returns everything in front. Should work for this application.

    Posted 3 years ago #
  20. James
    Member

    Ok thanks, it works on pages without a query string but on pages with a query string the URL $url produces looks like http://?message=logged-out

    Would this be easy to fix?

    Posted 3 years ago #
  21. uberlemurguy
    Moderator

    yes:

    $url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    if(substr_count($url, '?') > 0) {
    	$url = explode('?', $url, 1);
    	$url = $url[0];
    }
    <?php echo wp_logout_url(); ?>&amp;redirect_to=http://<?php echo $url ?>?message=logged-out
    Posted 3 years ago #
  22. James
    Member

    I'm sorry to be a pain...

    The original problem has come back with this code:

    http://techtrends.co.uk/?message=logged-out?message=logged-in

    I've put a link to Lemur CMS in the Useful Link section. If you want this updated (e.g. if you want it to your homepage) let me know.

    Posted 3 years ago #
  23. uberlemurguy
    Moderator

    hmm, I tested it myself and it did work.

    I'll try some more.

    Posted 3 years ago #
  24. James
    Member

    Thanks Alec

    Posted 3 years ago #
  25. uberlemurguy
    Moderator

    Sorry for the delay, try this:

    $url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    if(substr_count($url, '?') > 0) {
    $url = explode('?', $url, 2);
    $url = $url[0];
    }
    <?php echo wp_logout_url(); ?>&redirect_to=http://<?php echo $url ?>?message=logged-out

    Posted 3 years ago #
  26. James
    Member

    Thanks, its working properly now.

    Posted 3 years ago #
  27. uberlemurguy
    Moderator

    great!

    Posted 3 years ago #

RSS feed for this topic