wrong webpage rendered on user error - php

I am currently writing a page of a website, and I have a function that renders a new webpage to tell the user that they made an error if the date they have selected is in the past. The function looks like this:
if($interval < 0){
handle_error($dbval_date_error2);
}
If the user did put in a valid date, then the date is stored in a database and a new webpage is displayed which shows the new data entry.
The problem I have is that the handle_error function appears not to work usually. It functions exactly as it should if I also include some kind of print statement:
if($interval < 0){
handle_error($dbval_date_error2);
echo "here";
}
But if I just have the error function on its own, the function call just gets completely ignored and the entry gets stored in the database.
The error function looks like this:
function handle_error($error)
{
$_SESSION['error'] = $error;
header('location:../register/register.php');
}

The problem lies in how you're constructing your header; there needs to be a space between the colon and the path, Location needs to be uppercase, and you need to call exit(); after you set the header.
function handle_error($error)
{
$_SESSION['error'] = $error;
header('Location: ../register/register.php');
exit();
}

Just add an exit() after your header():
function handle_error($error)
{
$_SESSION['error'] = $error;
header('location:../register/register.php');
exit();
}
And it should work

Related

Echo (or display errors) after page reload

My latest idea which didn't seem to work was to store the array in a session,
include_once "scripts.php"
.........
//some code later
$errorlog .= "a random message<br/>";
$_SESSION['errorlog']=$errorlog;
reloadPage();
And then if 'errorlog' wasn't empty then display it,
[code]
<div class="randomclass">
<?php
displayErrors('errorlog');
?>
</div>
//here are the functions
function reloadPage(){
Header('Location: '.$_SERVER['PHP_SELF']);
}
function displayErrors($valuename = "errorlog"){
if(!empty($_SESSION['valuename'])){
echo $_SESSION['$valuename'];
unset($_SESSION['$valuename']);
return true;
}else{
return false;
}
}
[/code]
scripts.php
<?php
if(!isset($_SESSION)) session_start();
........
I have included scripts.php which starts with if(!isset($_SESSION)) session_start();.
I'm new to php, still making my first webpage (or actually, preparing scripts for it). I can't seem to successfully find bugs in my scripts because I don't know how to show the errors after a page reload is needed.
What I want, is a way to store strings like in this $errorlog and display it just like an echo(in div or whatever) after the page was reloaded
I don't get any errors with headers, the page reloads correctly but the problem is that no text is displayed after the page reloads, so I don't see why I shouldn't be using them unless you know another way to reload the page after script is done
surely this way is not the best one, but I think that the problem is very easy..
function displayErrors($valuename = "errorlog"){
if(!empty($_SESSION['valuename'])){ // here you must put a variable $valuename instead a simple string 'valuename'
echo $_SESSION['$valuename'];
unset($_SESSION['$valuename']);
return true;
}else{
return false;
}
}
You must change the session key at this row whit: $_SESSION[$valuename]
if(!empty($_SESSION['valuename'])){
The correct function is the follow:
function displayErrors($valuename = "errorlog"){
if(!empty($_SESSION[$valuename])){
echo $_SESSION[$valuename];
unset($_SESSION[$valuename]);
return true;
}else{
return false;
}
}
Bye!
Marco

My php cookie session code displays a blank white page no matter what?

So on one page my users check a box and type agree in an input field to proceed to the next page, I am trying to use session cookies to stop people bypassing this by typing the URL however when you proceed to the next page it just displays blank? i have tried tests such as using Echo to display text at the beginning of the script and have enabled error reporting but the page still just displays white? any ideas why?
Check Box and Input Page php:
<?php
if(isset($_POST["terms"])&&isset($_POST["agree"])) {
$agree = $_POST["agree"];
$validated = false;
if($agree=="agree") $validated = true;
if($validated) {
setcookie("agree",($agree));
header("Location: nextpgae");
} else {
header("Location: homepage");
}
}
?>
Page it leads to that is displaying blank's php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$validated = false;
if(isset($_COOKIE["agree"])){
$agree = $_COOKIE["agree"];
if(&$agree==("agree")) $validated = true;
}
if($validated) {
} else {
header("Location: homepage");
?>
A blank page is usually symptomatic of a server error. Frequently, it indicates a syntax error in your PHP code. Your server error log will tell you exactly what is going on, but in this case you have missed a closing } after this line
header("Location: homepage");
You should implement tokens for this kind of procedure. This may help you: http://forum.codecall.net/topic/58268-form-tokens-with-php/

Login Verification , $_GET['id'] in php not working

THIS is the PHP code m using for checking the values
if($uname==$row['username']) {
if($uname=='' || $pass=='') {
header("Location:login.html?id=Some fields are empty");
} else if($uname==$row['username'] && $pass==$row['password']) {
header("Location:1.html?id=$uname");
} else {
// **HERE I AM REDIRECTING TO THE LOGIN PAGE AND SENDING ERROR MESSAGE AS ID**
header("Location:login.html?id=Incorrect Password");
}
}
In the HTML part I included this to show the error message
<?php
if(isset($_GET['id']))
{
echo $_GET['id'];
}
?>
However NOTHING is getting printed
As mentioned in the comments, make sure that the server is set to parse .html file as PHP. See Server not parsing .html as PHP
The other option is to change your login.html to be login.php and go from there.
you can use php filters to check correctly if the values are set or not like the following :
filter_has_var(INPUT_GET , 'id');
this function returns true or false

Another command to replace exit in php?

is there any any other command we can use as an alternative to exit(); in php.
Because it breaks my html code at the end of the page if the condition is not met and when script has to exit.
Or if anyone has any other idea to resolve this issue???
Thanks
Update:
html code...
<?php
if username is not in correct format
echo "Please check your username";
exit();
if Username and Password didn't match
echo "Wrong Username or Password.";
exit();
if some other condition not met
echo "Condition not met";
exit();
?>
html code continues...
Now the problem is if any of the condition is not met and the script has to exit, the html code below it, which is a whole webpage, does not display...
And please...I am not a computer geek, had a problem so asked it, but why people vote down the question??? don't understand....
You should probably wrap your code into an if statement:
<?php
if($code == 'ok'){
echo 'ok';
} else {
echo 'not ok';
}
?>
your script doesn't have to exit(), you can add statements where you want and how you want.
As the name suggests, the PHP exit() statement will cause your PHP script to exit, right there and then, and not do anything else. If you want it to carry on processing the rest of the code, just don't use exit().
Looking at your code, what you seem to be aiming for is displaying errors to the user, and then (I would guess) re-showing the form they filled in incorrectly.
Rather than just echoing the errors as soon as you discover them, why not store them into a variable, which can then be displayed at an appropriate point in the HTML? Even the most basic of scripts can benefit from a bit of basic code structure.
As an example (and I stress this is not the One True Pattern for this kind of thing), you could arrange your file something like this:
if ( /* form has been submitted */ )
{
$errors = validate_form();
if ( count($errors) > 0 )
{
display_form($errors);
}
else
{
display_success_message();
}
}
else
{
display_form();
}
function validate_form()
{
$errors = array();
// Series of if conditions, each adding a message to $errors if appropriate
return $errors;
}
function display_form($errors=array())
{
// HTML <ul> list displaying the contents of $errors, if any
// HTML for form
}
function display_success_message()
{
// HTML thanking user for a successful form submission
}

Header() function not working properly

Whenever verifyUser is returned true then the if statement should execute but for some reason instead of going to the header location, the page just refreshes and that's all that happens. I've checked to be sure that the input information is correct and it is and when the information is incorrect the else statement executes perfectly fine. If anyone has any ideas as to why this is happening, please let me know. Thank you.
Here is the segment where the header() statement is made:
function validateUser($name, $pass)
{
$check = verifyUser($name, md5($pass));
if($check)
{
$_SESSION['status'] = 'authorized';
header('location: index.php');
} else{
echo'Please enter a correct username and password <br />';
echo "<a href='http://localhost/cms/admin/login.php'>Try Again?</a>";
exit;
}
}
Here is the verifyUser function just in case anyone needs it.
function verifyUser($name, $pass)
{
// Escape strings
$username = mysql_real_escape_string($name);
$password = mysql_real_escape_string($pass);
$result = mysql_query("select * from users where username='$username' and password='$password' limit 1");
if (mysql_num_rows($result)>0)
{
return true;
} else{
return false;
}
}
The most common reason that header() doesn't work is because something else has been output first. header() can only be called before anything else has sent output to the browser.
If this is the case, PHP will throw an error when header() is called. If you're not displaying errors on the page, you can check your error logs to see if this is happening.
You should also call die() or exit() immediately after (or a soon as possible after) the header() call, to prevent anything else from happening after the redirect header. It's unlikely but possible that something later in the program could also cause the redirect to fail even where the initial header() call succeeded.
Try using "url" instead of "location":
header("refresh:1;url=index.php");
You can put this line in your project at very first line (actually before sending any output), otherwise header function never will work after sending any output (e.g. html code or echo print etc);
ob_start();
Or, check that are headers already sent before sending new headers;
if(!headers_send()){
header("header params...");
} else {
echo "<script>window.location.href="index.php"</script>";
}
check to make sure that $_SESSION['status'] = 'authorized'; is defined
In mysql query use quotes on variable names as follows:
mysql_query("select * from users where username="'".$username."'" and password="'".$password."'" limit 1");

Categories