简体   繁体   中英

Why is my PHP not working?

I've have been trying to get into PHP and JavaScript lately, so I was just experimenting around with each of their functions, by making a little page that did something script specific like display time, browser, ip addresses, etc. But I seem to have gotten a little stumped here:

<html>
    <head>
        <title>Scripting Demo</title>
        <style type="text/css">
            html, body, a {
                margin: 0;
                padding: 0;
                text-decoration: none;
            }
            .globalContainer {
                width: 100%;
                height: 100%;
                text-align: center;
                font-family: Arial;
                background-image: linear-gradient(top, rgba(255,248,133,255) 0%, rgba(255,244,73,255) 50%, rgba(255,241,13,255) 100%);
                background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.00000105000042, rgba(255,248,133,255)), color-stop(0.5000002, rgba(255,244,73,255)), color-stop(1, rgba(255,241,13,255)));
                background-image: -moz-linear-gradient(top, rgba(255,248,133,255) 0%, rgba(255,244,73,255) 50%, rgba(255,241,13,255) 100%);
                background-image: -o-linear-gradient(top, rgba(255,248,133,255) 0%, rgba(255,244,73,255) 50%, rgba(255,241,13,255) 100%);
                filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fff885', endColorstr='#fff449'endColorstr='#fff10d');
            }
            .table {
                margin-left: auto;
                margin-right: auto;
            }
        </style>
    </head>

    <body>
        <div class="globalContainer">
            <table class="table" border="0">
                <th>Current Time</th>
                <tr>
                    <td align="center" id="currentTime">
                    </td>
                    <script type="text/javascript">
                        <!--
                        var cTime = document.getElementById("currentTime");

                        cTime.innerHTML="<b>Date:</b> "+Date();
                        cTime.style.backgroundColor = "rgb(188,212,230)";
                        cTime.style.paddingLeft = "5px";
                        cTime.style.paddingRight = "5px";
                        cTime.style.border = "solid";
                        cTime.style.borderRadius = "2em";
                        cTime.style.MozBorderRadius = "2em";
                        cTime.style.WebkitBorderRadius = "2em";
                        cTime.style.borderColor = "rgb(188,212,230)";
                        cTime.style.fontFamily = "Arial";
                        cTime.style.fontSize = "15px";
                        //-->
                    </script>
                </tr>
                <th>Current Browser</th>
                <tr>
                    <td align="center" id="currentBrowser">
                    </td>
                    <script type="text/javascript">
                        var cBrow = document.getElementById("currentBrowser");

                        cBrow.innerHTML="<b>Browser:</b> "+navigator.appName;
                        cBrow.style.backgroundColor = "rgb(188,212,230)";
                        cBrow.style.paddingLeft = "5px";
                        cBrow.style.paddingRight = "5px";
                        cBrow.style.border = "solid";
                        cBrow.style.borderRadius = "2em";
                        cBrow.style.MozBorderRadius = "2em";
                        cBrow.style.WebkitBorderRadius = "2em";
                        cBrow.style.borderColor = "rgb(188,212,230)";
                        cBrow.style.fontFamily = "Arial";
                        cBrow.style.fontSize = "15px";
                    </script>
                </tr>
                <th>IP Address</th>
                <tr>
                    <td align="center" id="ipAddress">
                        <b>IP:</b>&nbsp;<?php $ip=@$REMOTE_ADDR; echo $ip; ?>
                    </td>
                    <script type="text/javascript">
                        var ip = document.getElementById("ipAddress");

                        ip.style.backgroundColor = "rgb(188,212,230)";
                        ip.style.paddingLeft = "5px";
                        ip.style.paddingRight = "5px";
                        ip.style.border = "solid";
                        ip.style.borderRadius = "2em";
                        ip.style.MozBorderRadius = "2em";
                        ip.style.WebkitBorderRadius = "2em";
                        ip.style.borderColor = "rgb(188,212,230)";
                        ip.style.fontFamily = "Arial";
                        ip.style.fontSize = "15px";
                    </script>
                </tr>
            </table>
        </div>
    </body>
</html>

I realize it is really inefficient to use JavaScript just to implement CSS and all that jazz, but I'm doing all that just for the sake of practice.

Pay attention to line number seventy-six:

<b>IP:</b>&nbsp;<?php $ip=@$REMOTE_ADDR; echo $ip; ?>

This is where I was trying to use PHP and fetch the user's IP address, didn't really work out. Would anyone explain to me what I am doing wrong? I would love to learn PHP and JavaScript, but I seem to be really stumped here. :O

First, realize that @ silences errors and warnings, and those would help you solve the problem.

I think you are looking for $_SERVER['REMOTE_ADDR'] and $REMOTE_ADDR was a mistake or misinterpretation of the manual.

http://php.net/manual/en/reserved.variables.server.php

$_SERVER

Indices

You may or may not find any of the following elements in $_SERVER. Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line.

'REMOTE_ADDR'
The IP address from which the user is viewing the current page.

<b>IP:</b>&nbsp;<?php $ip=$_SERVER['REMOTE_ADDR']; echo $ip; ?>

这对我有用!

The $_SERVER superglobal array contains various pieces of information about the remote connection, including its IP address. Use phpinfo() to see the various bits available to you.

And don't use @ ; break that habit right now while there's still time.

使用它代替您拥有的东西:

$_SERVER['REMOTE_ADDR']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM