Localhost - Error - ERR_TOO_MANY_REDIRECTS

Hi, I am currently working on building a database driven website following the ‘PHP & Mysql Novice to Ninja’ book. I am a beginner in programming, but so far have managed to work through my problems, most often some tiny little thing I have missed!! Last night while I was working I suddenly came up against this localhost problem “ERR_TOO_MANY_REDIRECTS”.

Everything I have read so far suggests I need to reset the port to a different port, but I don’t understand why I should have to do this. Nothing has been added to make port 80 unavailable. I am not working with this online. What happened to ‘break’ this? Can i not just reset it somehow? It was suggested to clear cookies, which I have done with no noticeable effect except that I now have to log in to everything anew. Very frustrating. I didn’t even know localhost could ‘break’!! I use xampp, and that all appears to be working fine. I just can’t call anything to the screen now. Any help will be appreciated, thanks.

I would suggest that you use what the book is using and not something you found easy to use from a different source. If the book is using Vagrant, you should be using Vagrant. If the book is using, you should be using XAMP. If the book isn’t using XAMP and you’re using XAMP, you’re going to have a harder time replicating everything the book is showing.

Back on topic, can you show the code you are working on?

1 Like

I can show the code if you think it will help. My main problem is that I can not get localhost to run my code

Hi @shirt_hot_teez and welcome to the forum.

I have found that TOO_MANY_REDIRECTS is usually caused by .htaccess “pretty urls” redirection.

Try this .htaccess:

RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L, R=302]

Try adding the following to the index.php

<?php 
echo '<pre>'; // formats output and adds linefeeds
  print_r($_SERVER);
echo '</pre>';
die;

You don’t say what OS you’re using and we don’t know what apps you have installed. Skype sometimes uses port 80.

@spaceshiptrooper, thanks the book does suggest xampp for windows.

I do now believe my code is at fault, localhost is now showing my
code and what has happened is this… the sample website the book
builds is putting jokes into the database. I am putting a delete
button on each joke. It does this but it is adding a button on
every line indefinitely! So I add the code for checking and
I thank you for your time.

@John_Betong I will be glad to try this thank you, i will put the
code up first to see if that changes anything.

@Gandalf thanks, skype is installed on my laptop but i don’t use
it. I can’t see that it would suddenly change anything, and I now
believe my code to be at fault.

index.php

<?php
if (get_magic_quotes_gpc()) 
	{
	$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
	while (list($key, $val) = each($process)) 
		{	
		foreach ($val as $k => $v) 
			{
			unset($process[$key][$k]);
			if (is_array($v)) 
				{
					$process[$key][stripslashes($k)] = $v;
					$process[] = &$process[$key][stripslashes($k)];
				}
				else 
				{
					$process[$key][$stripslashes($k)] = stripslashes($v);					
				}
			}
		}
		unset($process);
	}#--------------------------------------magic quotes---------------------------------------------------
if (isset($_GET['addjoke'])) 
	{
		include 'form.html.php';
		exit();
	}#--------------------------------------form-----------------------------------------------------------
	try	{
		$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'mypassword');
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$pdo->exec('SET NAMES "utf8"');
	} 
	catch (PDOException $e) 
	{
		$error = 'Unable to connect to the database server.';
		include 'error.html.php';
		exit();
	}

	#--$output = 'Database connection established.';
	#--include 'output.html.php';                     
#--------------------------------------------connection----------------------------------------------------
if (isset($_POST['joketext'])) 
	{
	try {
		$sql = 'INSERT INTO joke SET
			joketext = :joketext,
			jokedate = CURDATE()';
		$s = $pdo->prepare($sql);
		$s->bindValue(':joketext', $_POST['joketext']);
		$s->execute();
	} 
	catch (PDOException $e) 
	{
		$error = 'Error adding submitted joke: ' . $e->getMessage();
		include 'error.html.php';
		exit();
	}

	header('Location: .');
	exit();
	}#---------------------------------------add joke to database------------------------------------------
if (isset($_GET['deletejoke'])) 
	{
	try {
		$sql = 'DELETE FROM joke WHERE id = :id';
		$s = $pdo->prepare($sql);
		$s->bindValue(':id', $_POST['id']);
		$s->execute();
	} 
	catch (PDOException $e) 
	{
		$error = 'Error deleting joke: ' . $e->getMessage();
		include 'error.html.php';
		exit();
	}

	header('Location: .');
	exit();
	}#--------------------------------------delete joke----------------------------------------------------
#--------------------------------------display jokes--------------------------------------------------
while ($row = $result->fetch())
	{
	$jokes[]  array('id' => $row['id'], 'text' => $row['joketext']);
	}#-----------------------------------store jokes---------------------------------------------------

include 'jokes.html.php';

jokes.html.php

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>List of Jokes</title>
	</head>
	<body>
		<p><a href="?addjoke">Add your own joke</a></p><br>
		<p>Here are all the jokes in the database:</p>
		
		<?php foreach ($jokes as $joke): ?>
			<form action="?deletejoke" method="post">
				<blockquote>
					<p><?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8'); ?>
					   <input type="hidden" name="id" value="<php echo $joke['id']; ?>">
					   <input type="submit" value="Delete">		
					</p>
				</blockquote>
			</form>
		<?php endforeach; ?>
	</body>
</html>

form.html.php

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Add Joke</title>
		<style type="text/css">
			textarea {
				display: block;
				width: 100%;
			}
		</style>
	</head>
	<body>
		<form action="?" method="post">
			<div>
			<label for="joketext">Type your joke here</label>
			<textarea id="joketext" name="joketext" rows="3" cols="40"></textarea>
			</div>
		</form>
		<div><input type="submit" value="Add Joke"></div>
	</body>
</html>

error.html.php

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Script Error</title>
	</head>
	<body>
		<p>
			<?php echo $error; ?>
		</p>
	</body>
</html>

Thank you all

Oh, I run windows 2010 and use chrome

sorry i see my code is junked together, how do I put it up properly?

@shirt_hot_teez: when you post code on the forums, you need to format it so it will display correctly. (I’ve fixed the post for you.)

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

Which version of _ PHP & Mysql Novice to Ninja_ are you using? This code is seriously out of date and will not work with the current version of PHP. (scrub that, I must be seeing things).

1 Like

Thank you. I will try get it right next time!

1 Like

The Novice to Ninja is fifth edition. i have been using sublime, sometimes i use expressionweb4

So the code I’m using is straight out of the book

Your header() redirects are targeting . which I feel like is the problem. It does correlate with the infinite redirect error message. Try redirecting to a different page that doesn’t exist and see if you can trigger the default error 404 page. If you can, then those header() redirects are at fault and you should change them to the appropriate target location.

I don’t see where this

while ($row = $result->fetch())
	{
	$jokes[]  array('id' => $row['id'], 'text' => $row['joketext']);
	}

comes from. Apart from missing an = sign, it’s also missing a whole chunk of code to connect to and read from the db as far as I can see.

@spaceshiptrooper thanks, i got no significant change with that

@Gandalf
thanks about the = sign, and i have been reading this text off and on for hours!!
I am trying to follow the book, i’m at chapter 4, there is no more to this piece of code at this point and looking just ahead the only change to it is when it brings in another table, and it changes to foreach instead of while.

I did have this part of the coding working but when it stepped up a level it all broke and I didn’t have a backup copy (I Know, stupidity++). I could add and delete jokes which I have not yet achieved this time round

If you’re not already doing so, save each version of the programs (especially the ones that work) so if you find the next version doesn’t work you can go back to the previous version and try again

1 Like

And what does that mean? What result did you see? What did changing the header() locations do? Details, details, details. “It doesn’t work” doesn’t explain anything.

Sorry. Nothing changed on the page, no error.

I am working through from the start. I put the whole thing into a saved file and started from beginning. Will give update later, i better get some sleep soon before the sun rises, its 4.30am here. The night has gone really fast

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.