ERR_TOO_MANY_REDIRECTS ERROR LOCALHOST - php

In most cases, a plain URL will be recognized as such and automatically linked:
The error only appears on localhost, please help!
The interesting thing is that on the hostinger server it works perfectly...
require_once('header.php');
// Preventing the direct access of this page.
if(!isset($_REQUEST['slug']))
{
header('location: index.php');
exit;
}
else
{
// Check the page slug is valid or not.
$statement = $pdo->prepare("SELECT * FROM tbl_page WHERE page_slug=? AND status=?");
$statement->execute(array($_REQUEST['slug'],'Ativo'));
$total = $statement->rowCount();
if( $total == 0 )
{
header('location: index.php');
exit;
}
}
$statement = $pdo->prepare("SELECT * FROM tbl_page WHERE page_slug=?");
$statement->execute(array($_REQUEST['slug']));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row)
{
$page_name = $row['page_name'];
$page_slug = $row['page_slug'];
$page_content = $row['page_content'];
$page_layout = $row['page_layout'];
$banner = $row['banner'];
$status = $row['status'];
}
// If a page is not Ativo, redirect the user while direct URL press
if($status == 'Inativo')
{
header('location: index.php');
exit;
}

Related

PHP redirecting before finish MySQL insert

I have this code on "insert.php":
if ($stmt = $GLOBALS['mysqli']->prepare("INSERT INTO table1(iduser, title, msg, anonim, ip, iduniversity) VALUES (?,?,?,?,?,?)")) {
$stmt->bind_param("issisi", $_SESSION['iduser'], $_POST['title'], $_POST['msg'], $anonim, getIP(), $_SESSION['iduniversity']);
if ($stmt->execute()) {
$idmsg = $GLOBALS['mysqli']->insert_id;
$i = 0;
$stmt2 = $GLOBALS['mysqli']->prepare("INSERT INTO tag(idmsg, tag) VALUES(?,?)");
foreach ($tags as $tag) {
if ($tag != '') {
$stmt2->bind_param("is", $idmsg, $tag);
if ($stmt2->execute()) {
$i++;
}
}
}
$stmt2->close();
$stmt->close();
mysqli_close($GLOBALS['mysqli']);
sendFile($idmsg);
header("Location: /public/watch_msg.php?idmsg=" . $idmsg);
exit();
} else {
exit("Ops! Ocorreu algum erro. COD1370");
}
} else {
exit("Ops! Ocorreu algum erro. COD1371");
}
So, everything is working fine, except that sometimes when it redirects to "watch_msg.php" the message seems not to be on the database yet. When this happens, as soon as I refresh the page, everything is there!
First thing I thought is that there could be a race-condition somewhere, but I read in another question that PHP is sequential, and as I close both statements and connection before the redirect (so that used tables should be unlocked), why i'm getting this result somethimes? What i'm doing wrong?
Also, no functions outputs anything, but "sendFile" saves an image if the user sends one, so headers should be fine (it also gives me the error when I comment the function).
Code on watch_msg:
$msg = NULL;
$tags = NULL;
$coments = NULL;
$data_high = date("Y-m-d H:i:s");
$iduser;
if ($loggedin) { //If logged in
$idmsg = filter_input(INPUT_GET, 'idmsg', FILTER_SANITIZE_STRING);
$iduser = $_SESSION['iduser'];
$query = "SELECT * FROM table1 WHERE iduser = ? AND idmsg = ? AND datemsg < ?";
$stmt = $GLOBALS['mysqli']->prepare($query);
$stmt->bind_param("iis", $iduser, $idmsg, $data_high);
if ($stmt->execute()) {
$msg = mysqli_fetch_assoc($stmt->get_result());
if ($msg === NULL) {
exit('This message doesn\'t exists');
}
...
} else {
echo "Error.";
}
}

Why it is not redirecting

<?php
if(isset($_POST['confirm'])){
$name = $db->validation($_POST['name']);
$id = "";
for($i= strlen($name) ; $i>=1 ; $i--){
if($name[$i-1] == 'a'){
break;
}else{
$id = $id.$name[$i-1];
}
}
$userid = strrev($id);
$query = "SELECT * FROM bluekolar_user WHERE token = '$name' AND user_id='$userid'";
$result = $db->select($query);
if($result){
if(mysqli_num_rows($result) ==1 ){
$qu = "UPDATE bluekolar_user SET active = '1' WHERE user_id='$userid'";
$res = $db->update($qu);
if($res){
$url = "profile/index.php?id=".$userid ;
header('location:'.$url) ;
exit();
}else{
$error = "Provide correct confirmation code";
}
}else{
$error = "Provide correct confirmation code";
}
}
}
?>
The code is performing good but it is unable to redirect. I don't know why. I need help.
Please help me so that I can redirect my code command to another page.
Thank you.
You need to ensure the path you are trying to redirect to is correct. The code in your question will try to redirect to a "profile" subdirectory of the location where this script is running. If you want to go to "http://example.com/profile/index.php?id=" you need to change your $url to be "/profile/index.php"

Can I have 2 header locations in PHP?

So, I know my code was working and I have tried un-doing all steps to see where the bug is, but I am still keep getting an error
my php id=0.
Can you guys show me how I can fix my code up?
The error is as follows:
undefined variable list_id. It works on my localmachine but not when
uploaded to server.
Thanks.
The following is my code:
if(!empty($_GET['id'])){
$list_id = intval(($_GET['id']));
try {
$sql = 'SELECT * FROM items where id =' . $list_id;
$query = $pdo->prepare($sql);
$query->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
if ($list == FALSE) {
header("location: index.php");
}
}
if ($list_id == 0) {
header("location: index.php");
}
Seems there are a few issues here. I have added the comments inline.
if(!empty($_GET['id'])){
$list_id = intval($_GET['id']); //was double parenthesis here
try {
$sql = 'SELECT * FROM items where id =' . $list_id;
$query = $pdo->prepare($sql);
$query->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
$count = count($list); //count result and use for comparison instead of false
if ($count === 0) {
header("location: index.php");
exit;
}
} else {
header("location: index.php"); //if no $_GET, redirect
exit;
}
All you need to do is instantiate the variable $list_id :
$list_id = 0; // <-- HERE
if(!empty($_GET['id'])){
$list_id = intval(($_GET['id']));
...
...
It looks as though you want to redirect to the index if there is no id parameter. Since you already check for its presence, redirect in the else clause. Remove the last block and add:
else
{
header("location: index.php");
exit;
}
You may want to add exit; after the header() call in the if block as well, so that code that uses the database further down isn't executed.

Header is not working

My header is not working.
<?php
$name = mysql_prep($_POST['name']);
$pastor = mysql_prep($_POST['pastor']);
$head = mysql_prep($_POST['head']);
$schedule = mysql_prep($_POST['schedule']);
$venue = mysql_prep($_POST['venue']);
$id = mysql_prep($_GET['ministryid']);
$errors = array();
$required_field = array('name', 'pastor', 'address', 'schedule', 'venue');
foreach ($required_field as $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[] = $fieldname;
echo "Sorry, you missed to complete {$fieldname} <br />";
}
else {
$query = "UPDATE ministry SET
name = '{$name}',
pastor = '{$pastor}',
head = '{$head}',
schedule = '{$schedule}',
venue = '{$venue}'
WHERE id = {$id}";
mysql_query($query);
if(mysql_affected_rows() == 1) {
header('location: editministry.php?');
exit;
} else {
echo "Updating Failed on {$s_ministry['name']} <b />".mysql_error();
exit;
}
}
}
require_once("include/footer.php");
Every time I have successful update, the link change its address.
For example, when I'm updating id = 3, the address will change to editministry.php?ministryid=3.
You dont send anything along your URL, thats why your header won't work. Check for yourself.
if(mysql_affected_rows() == 1) {
header('location: editministry.php?');
exit;
Your link will become effectivly baseUrl/editministry.php?.It searches than for a variable, which is not defined. I am not sure how you actually can pass on a variable that you didnt define in a link, yet it sends you there. Don't know. But if you just tell it to the hard link without the questionmark, it should go to that page. For me it works at least within my code. For you it would be:
if(mysql_affected_rows() == 1) {
header('location: editministry.php' );
exit;
In my code it looks like this:
header( "Location: http://" . strip_tags( $_SERVER ['HTTP_HOST'] ) . "/newHolo/index.php" );

PHP If Statements Not Firing

I'm currently building a system for a football league. And are currently working on the script file for adding results. Most of the script works and the result is always successfully added to the database. However the authentication part seems to fail. The if statement on line 12 does not seem to fire and I can't understand why.
My code can be found in the pastebin link here: http://pastebin.com/ty4pdGgn
<?PHP
include 'functions.php';
dbConnect();
//$userEmail = mysql_real_escape_string($_POST["userEmailText"]);
$userCode = mysql_real_escape_string($_POST["userPasscodeText"]);
$authenticated = false;
$userEmail = "info#example.com";
if ($userEmail == "info#example.com") {
header('Location: ../results.php?error=authentication');
}
$allUsers = mysql_query("SELECT * FROM accounts WHERE email = '$userEmail'");
while ($thisUser = mysql_fetch_assoc($allUsers)){
if ($userCode != $thisUser['passCode']) {
header('Location: ../results.php?error=authentication2');
}
echo $thisUser['passCode'];
$authenticated = true;
$userID = $thisUser['userID'];
}
if (!$authenticated) {
header('Location: ../results.php?error=authentication3');
}
$dateSubmitted = $_POST['submissionDate'];
$homeTeam = $_POST['homeTeam'];
$awayTeam = $_POST['awayTeam'];
$homeGoals = $_POST['homeGoals'];
$awayGoals = $_POST['awayGoals'];
if ($homeTeam == $awayTeam) {
header("Location: ../results.php?error=team");
}
if (getTeamLeague($homeTeam) != getTeamLeague($awayTeam)) {
header("Location: ../results.php?error=league");
} else {
$leagueID = getTeamLeague($homeTeam);
}
if ($homeGoals > $awayGoals) {
$winnerID = $homeTeam;
} else if ($homeGoals < $awayGoals) {
$winnerID = $awayTeam;
} else if ($homeGoals == $awayGoals) {
$winnerID = -1;
}
$cQuery = mysql_query("INSERT INTO results VALUES ('', $userID, '$dateSubmitted', $leagueID, $homeTeam, $homeGoals, $awayTeam, $awayGoals, $winnerID, 0)");
if ($cQuery){
header('Location: ../results.php');
} else {
echo mysql_error();
}
?>
Any help with this matter will be much appreciated. The functions.php contains no errors as this is all to do with database entry and not the authentication.
Put a die(); after the header("Location:...");
As your comparison code (the "if" part on line 12) that you pasted has to work, i have two advice:
Put a die(); or exit(); after the header() part.
Try looking here, as I am not sure if header() will work, while the location path you set is relative. Basic advice is to always use base paths for redirects, like "http://your.site.com/script.php").

Categories