Placing Header Location as Sql Die action - php

I was curious to know if i could do this, but found no examples on line
$info_find = mysql_query("SELECT info FROM sets WHERE category = '$selected_cat'")
or die( header ("location: index.php"));
Firstly doing it as above doesn't work. Can it be done?
Are there any drawbacks?

die() just writes its parameter out, it doesn't execute it. So a "or die()" construct will not help you there.
You can consider something like
or die(createRedirect("index.php"));
with
function createRedirect($where) {
$s='<html><head>';
$s.='<meta http-equiv="refresh" content="1; url='.$where.'">';
$s.='</head><body>If you are not automatically redirected click here';
$s.='</body></html>';
return $s;
}
if you are willing to accept the downsides of client-sided redirection

You can rewrite your code as
$info_find = mysql_query("SELECT info FROM sets WHERE category = '$selected_cat'") ;
if ($info_find === FALSE) {
header ('location: index.php');
die();
}
But, before use the header function, be sure you haven't send any output to the browser.

Related

How to redirect or force page if mysql returns nothing in a query

working on a small project and having a small problem. i am querying mysql, i am able to get the array and everything works well but i am adding a additional condition if returns 0 or id does not match the query then redirect.
This is what i have so far.
$q = "SELECT TEST.*,
EMPLOYEE.EMP_ID
FROM TEST
LEFT JOIN EMPLOYEE ON TEST.EMP_ID = EMPLOYEE.EMP_ID
WHERE ITEM_ID=".$db->qstr($item_id);
if(!$item_id = $db->execute($q)){
force_page('system', 'Nothing found');
exit;
} else {
$view = $item_id->GetArray();
}
return $view;
}
so the above returns the array, query grabs $item_id from the url so it could be anything. i would like to check if that id exists if not then force the page as shown in the above. any suggestion on what i am doing wrong?
If you want to redirect you can use header function
header("Location: put_your_url_here");
e.g.
header('Location: /mypage.php');
There is no function such as force_page in PHP. You can use header to redirect user to another page. Modify your if condition as
if(!$item_id = $db->execute($q)){
header("Location: url_to_redict");
die();
}
header("Location: Your_url"); will help you to redirect based on your condition.but make sure that you have not echoed any thing before the header otherwise it won't redirect.
I don't think there is any function like force_page(); in php
You need to redirect like header("Location: Your_url");
And If you have function force_page(); please post code of that.

Using header() to redirect after using include()

I have a page structured like below and I am having issues getting the redirect to work. This page gets an ID from the URL and uses it in the query. If there isn't a match, just redirect to another page.
I am getting the "headers already sent error," due to the include. I need the include to be there regardless. Is there a different way I can do the redirect if the query result is empty?
include('somepage.php');
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:htp://example.com'); }
I've tried using exit(); and the various stop processing functions.
somepage.php:
<html>
<head>
(standard html)
include('sql-connect.php');
</head>
<body>
(code to format the header portion of the site)
You can put ob_start() at the beginning of the file, so that it looks like this:
<?php
ob_start();
include 'somepage.php';
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }
Also, you can echo html redirect:
<?php
if(mysql_num_rows($query)==0) { echo '<meta http-equiv="refresh" content="0; url=http://example.com/">'; die(); }
You need to add ob_start() at the beginning of the file and if this still does not work then you also need to add ob_flush(); to fully flush out the old header.
flush(); // Flush the buffer
ob_flush();
header("Location: http://example.com");
You may have a blank line after a closing ?> This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls. Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.
ob_start() may fix your problem.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
"headers already sent error" means that your script (somepage.php) already sent headers to the browser, so you can't change these headers and redirect user to the other URL.
The best solution is to move the "include" operator after your check:
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }
include('somepage.php');
Also, you can prevent your somepage.php from sending any data to the client.
The second variant is to use the functions ob_start(), ob_get_contents(), ob_end_clean():
ob_start();
include('somepage.php');
$id = $_GET['id'];
$query = mysql_query("My query is here");
if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }
$content = ob_get_contents():
ob_end_clean();
echo $content;

PHP Header relocating and $_GET

I have a website that allows users to upload a picture, but I don't want any nudity in the photos. I found a scan written in php that I have succesfully implemented. The file and record are deleted if nudity is found to be in the file. I am just having trouble alerting the user as to why there pic wasn't kept. It just reloads the page. What could be the problem?
This code is the beginning of non commented code in my new2.php file:
if (isset($_GET['error'])) {
echo "Nudity found. Please try again with a more appropriate picture.";
sleep(5);
header("Location: new2.php");
}
This code is the code that scans the pic for nudity:
if($quant->isPorn()) {
$q = "delete from $table where id='$id'";
$result = mysql_query($q);
unlink("pics/".$picfile);
header('Location: new2.php?error=1');
} else {
header("Location: index.php?id=$id");
}
Any help would be greatly appreciated!
Your echo output won't be seen by the user. Output hasn't been sent to the browser yet, that happens at the end of your script. You redirect before that happens.
If you want to send a message, wait 5 seconds, and then redirect, do it client side.
<?php if (isset($_GET['error'])) { ?>
<p>Nudity found. Please try again with a more appropriate picture.</p>
<script>
setTimeout("self.location='new2.php'",5000);
</script>
<?php } ?>
Javascript is not needed. Just output the page containing your error, but change
sleep(5);
header("Location: new2.php");
to
header("Refresh: 5; url=new2.php");
This has the same effect as a <meta http-equiv="refresh">.
Don't do it from header. Use this instead:
<script> location="whatever.php?a=1"; </script>

Creating dynamic header links in PHP

In my code i have an if statement, which is return is true, it would header the user to a specific page. But i want to send some data to the next page. So i tried using dynamic links. but it doesn't seem to work. Here's my code;
<?php
$row = mysqli_fetch_object($query);
if($row->Usertype = "General_User")
{
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid= echo $row->Company_ID");
}
else
{
header('Location: http://www.mywebsite.com');
}
?>
but when i'm redirected to the page, i get this;
http://www.mywebsite.com/GeneralUserHome.php?cid=%20echo%20'';
any suggestions?
Why do you have echo in there?
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid={$row->Company_ID}");
You have a typo here:
if($row->Usertype = "General_User")
That's an assignment, and will always be true. You want double-equals for comparison:
if($row->Usertype == "General_User")
As a note, I reverse the two in order to avoid these typos. This will error out and tell you exactly what was wrong, if you typo'd a single equals sign:
if("General_User" = $row->Usertype)

Setting session var

Here is my PHP code:
<?php
session_start();
if ( !isset($_SESSION['index_visited']) )
{
$_SESSION['page2_visited'] = 1;
$_SESSION['wrong_number'] = 1;
header('Location: index.php');
}
?>
I think that $_SESSION['page2_visited'] = 1 and $_SESSION['wrong_number'] = 1 never get set. Program redirect me on index.php and thats it.
What I have to do to set it?
It's probably the session doesn't have time to properly save itself before the redirect happens.
Use session_write_close(); to force the saving of the session right before the header redirect.
So it would be:
<?php
session_start();
if ( !isset($_SESSION['index_visited']) ) {
$_SESSION['page2_visited'] = 1;
$_SESSION['wrong_number'] = 1;
session_write_close();
header('Location: index.php');
}
?>
session_commit() works too. It's just as alias of session_write_close()
i bet $_SESSION['index_visited'] is set. perhaps you want
if(isset($_SESSION['index_visited']) and $_SESSION['index_visited'] == 1) {
remember, if $_SESSION['index_visited'] has been set to 0, that counts as set.
The header('Location: index.php'); overwrites the session setting headers.
The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type.
http://php.net/manual/en/function.header.php
So please try: header('Location: index.php', false);
My mistake. This is not place for $_SESSION['wrong_number'], now it is working. Thank you for answers anyway.

Categories