$_SERVER[] Command and Page Linking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

$_SERVER[] Command and Page Linking

Post by lmg »

I am trying to make a login page which will redirect to another page once the login is successful. I am keeping things very basic for now with a hardcoded username and password. I really don't understand what the $_SERVER[] (lines 42 and 50) command is exactly (even after reviewing the php documentation and googling) and how I can get my page to link to another page.

Basically what I want to do is enter the correct username and password and link to a main menu-like page. I am not very familiar with php, but I have a feeling my problem is something to do with the $_SERVER command.

Here is my code:

Code: Select all

<!-- Original code from http://www.tutorialtastic.co.uk/tutorial/creating_a_secure_php_login_page with permission-->
 
<?php
$username = "user";
$password = "pass";
$randomword = "randomWord";
 
//Checking cookie. If it is verified, we grab the password provided by the user and use the md5 method (encryption) on the password
//and random word. This value will be compared to the password value in the database.
if (isset($_COOKIE['MyLoginPage'])) {
   if ($_COOKIE['MyLoginPage'] == md5($password.$randomword)) {
?>
      <!--CONTENT HERE-->
<html>
<head>
<title>Login</title>
</head>
<body>
</body>
</html>
      
<?php
      exit;
   } else {
      echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Bad cookie. Please clear your cookies and try 
      again.</b></p></center>";
      exit;
   }
}
 
if (isset($_GET['p']) && $_GET['p'] == "login") {
   if ($_POST['user'] != $username) {
      echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Failed to find username. Click the back button 
      to try again.</b></p></center>";
      exit;
   } else if ($_POST['pass'] != $password) {
      echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Failed to match password. Click the back button 
      to try again.</b></p></center>";
      exit;
   } else if ($_POST['user'] == $username && $_POST['pass'] == $password) {
      setcookie('MyLoginPage', md5($_POST['pass'].$randomword));
      header("Location: $_SERVER[PHP_SELF]");
   } else {
      echo "<center><h1><font face=\"Arial\" color=\"#993300\">Error!</h1><br><p><font size=5><b>Could not login at this time. Refresh the page 
      and try again.</b></p></center>";
   }
}
?>
 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
 
<h1><center><font face="Trebuchet MS">Login</font></center></h1>
<hr>
<br>
 
<center><font face="Arial"><b>Username:&nbsp;&nbsp;</b></font><input type="user" size="15" name="user"></center><br>
<center><font face="Arial"><b>Password:&nbsp;&nbsp;</b></font><input type="password" size="15" name="pass"></center>
<br>
<br>
<center><input type="submit" id="submit" value="Login"></center>
<br>
<hr>
<p align="right"><font face="Arial">Page Information &copy; 2009</font></p>
 
</form>
</div>
Right now, when I enter the correct username and password, I get redirected to a blank page. You can check out the page here.

Thanks for your patience!
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: $_SERVER[] Command and Page Linking

Post by mikemike »

$_SERVER is a superglobal array. It simple gives you access to some information about the user and the server environment, things like; users IP, users user-agent (browser, OS, etc), the server's IP, the server's software it's running (this is a string composed of some of the different modules running).

In your case $_SERVER['PHP_SELF'] is being used. This is simply the location of the current page. So your login form posts to itself, then if you're logged in the server tries to forward you to "$_SERVER['PHP_SELF']".

Now, you said you're getting a blank page. You haven't escaped the variable inside the header() function so you're actually trying to forward to a page called "$_SERVER['PHP_SELF']", rather than the value of it. If you want to redirect to the login page again then just escape the $_SERVER inside header():
header("Location: ".$_SERVER['PHP_SELF']);
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: $_SERVER[] Command and Page Linking

Post by lmg »

Thanks for the help with the $_SERVER stuff.

I made the change suggested (header("Location: ".$_SERVER['PHP_SELF']);) and I am still being redirected to a blank page.

I have also been playing around with making this page do what I want it to do (redirect to a main menu) and the format I found in the php help files:

Code: Select all

header("Location: http://www.example.com/"); /* Redirect browser */
 
still redirects me to a blank page.

Edited line 42 to read:

Code: Select all

header("Location: http://lmg.netau.net/myTestDir/session.php/");
 
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: $_SERVER[] Command and Page Linking

Post by mikemike »

When using header() you must make sure absolutely NO output is sent to the browser, this includes whitespace (a space before the openning <?php tag.

If you don't have error reporting on and you're outputting something before header() then you'll get a blank page.

What is the URL in your address bar? The same as the one with the form in? If so then header() isn't being called (assuming you're not using $_SESSION['PHP_SELF'] like you said you changed).
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: $_SERVER[] Command and Page Linking

Post by lmg »

I think I may have found the error - I had the <html> opening BEFORE the <?php opening. I changed it and now it goes to the correct page.

Thanks so much for your help mike! :D

Oh and one more question - does the <title> tag count as output to the browser?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: $_SERVER[] Command and Page Linking

Post by mikemike »

Yeah, absolutely everything does. You don't need to output anything if you're redirecting the user because they'll never get to see it. If you're thinking of making a redirect page for the user ("You are being redirected, if you are not redirected in 5 seconds, click here..." kind of thing) then Google 'meta refresh'. - although header() is the way to go.

Mike
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: $_SERVER[] Command and Page Linking

Post by JeffG »

Just an aside: do Linux systems have Arial and Trebuchet fonts? There is no fallback specified.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: $_SERVER[] Command and Page Linking

Post by mikemike »

Depends on the distro, most new one's will I'd imagine
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: $_SERVER[] Command and Page Linking

Post by lmg »

I am unfortunately working in Windows -_-
Post Reply