3 Replies - 816 Views - Last Post: 29 August 2011 - 05:42 AM Rate Topic: -----

#1 itaprz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-April 10

PHP login script problem

Posted 29 August 2011 - 03:18 AM

Hey there, i'm now working on a php login script . So far everything works as well but there's an annoying issue I have to deal with -
displaying connection error.

Using "echo $error" only displays the error output in a blank page, so I tried sending the $error variable to another page which looks exactly like the login page using header,

$error="Please check your connection details again";
header("location:log.php?error=".$error);


It works just fine but it doesn't look good,moreover,the new url looks bad and I'm sure there's another way to do it.



Any suggestions?
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: PHP login script problem

#2 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: PHP login script problem

Posted 29 August 2011 - 03:50 AM

What do you mean 'doesn't look good'? You could try a die(); or trigger_error(); which will call a error that you define.

Trigger Error @ Php.net

This post has been edited by aaron1178: 29 August 2011 - 04:11 AM

Was This Post Helpful? 1
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: PHP login script problem

Posted 29 August 2011 - 05:33 AM

This isn't going to work if you start putting special characters in the error so you shouldn't do this anyway.

The best way would probably be to predefine your error messages, for example:
class ErrorMessages
{
    const CHECK_CONNECTIONS = 1;

    private static $msgs = array(
        self::CHECK_CONNECTIONS => 'Please check your connection details again'
    );

    public static getMessage($id)
    {
        if (isset(self::$msgs[$id])) {
            return self::$msgs[$id];
        }
        return '';
    }
}


Then change your code above to:
header("Location: log.php?ec=" . ErrorMessages::CHECK_CONNECTIONS);
die();


Within log.php, to get the variable $error back, simply do something along the lines of:
$error = '';
if (isset($_GET['ec'])) {
    $error = ErrorMessages::getMessage($_GET['ec']);
}
// $error will now be 'Please check your connection details again'


EDIT: Note that this method is not only simpler and more understandable, but it alleviates security issues such as XSS, by not allowing the user to pass over arbitrary strings.

This post has been edited by RudiVisser: 29 August 2011 - 05:36 AM

Was This Post Helpful? 1
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: PHP login script problem

Posted 29 August 2011 - 05:42 AM

If you had a templating system then it would have been a lot easier and still keep your HTML separated from your PHP.

Your HTML page
<!DOCTYPE html>
<html>
<head>
	<title>Example</title>
    <style type="text/css">
		.error {
			color:red;
			font-weight:bold;
			text-decoration:underline
		}
	</style>
</head>
<body>
<h1>Content Here</h1>
<span class="error">{{error}}</span>
</body>
</html>



PHP file
<?php
$html = file_get_contents('page.html');
/*
* Did a lot of processing here and now we have an error
*/
$error = 'There has been an error but the administrator has been notifed';

echo str_replace('{{error}}', $error, $html);
?>



To retrieve the errors from your url ($_GET) you'll still need PHP in your HTML so might as well store it in your session and keep the URL tidy.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1