Jul 27, 2022
0
Level 1
The page isn’t redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
In my php project when i tray to get the index or other pages in the browser it says like "The page isn’t redirecting properly,This problem can sometimes be caused by disabling or refusing to accept cookies." here is my code index.php
<?php
include_once 'session.php';
include_once 'connection.php';
include_once 'utilities.php';
include_once 'conect.php';
include_once 'User.php';
if(isset($_POST['loginBtn'])){
//array to hold errors
$form_errors = array();
//validate
$required_fields = array('username', 'password');
$form_errors = array_merge($form_errors, check_empty_fields($required_fields));
if(empty($form_errors)){
//collect form data
$user = $_POST['username'];
$password = $_POST['password'];
//check if user exist in the database
$sqlQuery = "SELECT * FROM users WHERE username = :username";
$statement = $db->prepare($sqlQuery);
$statement->execute(array(':username' => $user));
while($row = $statement->fetch()){
$id = $row['id'];
$hashed_password = $row['password'];
$username = $row['username'];
if(password_verify($password, $hashed_password)){
$_SESSION['id'] = $id;
$_SESSION['username'] = $username;
header("location: home.php");
}else{
$result = "<p style='padding: 20px; color: red; border: 1px solid gray;'> Invalid username or password</p>";
}
}
}else{
if(count($form_errors) == 1){
$result = "<p style='color: red;'>There was one error in the form </p>";
}else{
$result = "<p style='color: red;'>There were " .count($form_errors). " error in the form </p>";
}
}
}
?>
<html>
<head>
<title>Live Video Chat Using PHP</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@100;300;400;500;700&display=swap" rel="stylesheet">
</head>
<body>
<!--WRAPPER-->
<div class=" wrapper h-screen items-center justify-center flex">
<div class="inner rounded flex bg-white w-4/5 border" style="height:70%; margin-bottom:10%;">
<!--LEFT_SIDE-->
<div class="w-2/5 border-r">
<div class="select-none flex h-full items-center justify-center">
<img class="select-none w-4/5" src="assets/images/login-left-bg.png">
</div>
</div><!--LEFT_SIDE_END-->
<!--RIGHT_SIDE-->
<div class="flex-2 flex rounded-xl w-full h-full ">
<!--PROFILE_SECTION-->
<div class=" flex flex-1 justify-center items-center">
<div class="flex flex-col flex-1 h-full overflow-hidden overflow-y-auto items-center justify-start">
<div class="mt-10 w-60 h-60 right-img rounded-full overflow-hidden">
<img class="h-auto w-full" src="assets/images/user.png">
</div>
<div class="right-heading w-full flex flex-col items-center">
<div>
<h2 class="text-center" style="padding-top:0px;">Wellcome!</h2>
<p>Sign-in into your account.</p>
</div>
<form method="post" class="w-full" action="index.php">
<div class="w-full flex flex-col items-center">
<div class="flex w-2/4 flex-col my-2 items-center">
<input class="w-4/5 my-2 border border-gray-200 rounded px-4 py-2" type="text" name="username" placeholder="username">
<input class="w-4/5 my-2 border border-gray-200 rounded px-4 py-2" type="password" name="password" placeholder="Password">
<div class="select-none error text-red-500 text-xs p-2 px-2 w-auto self-start ml-20">
<!-- ERROR -->
<?php if(isset($result)) echo $result; ?>
<?php if(!empty($form_errors)) echo show_errors($form_errors); ?>
</div>
</div>
<div>
<button class="active:-top-2 relative transition border border-gray-400 shadow-md my-4 bg-green-400 hover:bg-green-500 p-2 px-20 rounded-full text-white text-xl" name="loginBtn">Login</button>
</div>
<div>
</div>
</form>
<a class="active:-top-2 relative transition border border-gray-400 shadow-md my-4 bg-green-400 hover:bg-green-500 p-2 px-20 rounded-full text-white text-xl" href="register.php" name="signupBtn">Register</a>
</div>
</div>
</div>
</div>
<!--PROFILE_SECTION_END-->
</div>
<!--RIGHT_SIDE_END-->
</div><!--INNER_ENDS-->
</div><!--WRAPPER ENDS-->
</body>
</html>
<script type="text/javascript">
var conn = new WebSocket('ws://localhost:8087');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
//sendMessage(e.data, 'right');
};
</script>
session.php
<?php
session_start();
require 'connection.php';
require 'User.php';
$userObj = new \MyApp\User;
define('BASE_URL','http://localhost:8081/insa1/one/');
home.php
<?php
include_once 'session.php';
include_once 'connection.php';
include_once 'utilities.php';
include_once 'user.php';
if(!$userObj->isLoggedIn()){
$userObj->redirect('index.php');
}
$user = $userObj->userData();
?>
<!DOCTYPE html>
<html>
<head>
<title>Live Video Chat PHP</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Tailwind -->
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<!-- Font-awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<!-- Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@100;300;400;500;700&display=swap" rel="stylesheet"> <!-- Jquery -->
<!-- Jquery -->
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script>
</head>
<body>
<!-- AlertPopup -->
<!-- CallPopup -->
<!--WRAPPER-->
<div class="wrapper h-screen items-center justify-center flex">
<div class="inner flex bg-white w-4/5 border" style="height:70%; margin-bottom:10%;">
<!--LEFT_SIDE-->
<div class="w-2/5 border-r">
<div class="h-full overflow-hidden">
<div class="flex justify-between items-center border-b">
<div class="mx-2 flex items-center justify-center">
<div class="flex-shrink-0 mx-2 my-4 rounded-full overflow-hidden h-12 w-12 cursor-pointer">
<img class="w-full h-auto rounded-full select-none" src="<?php echo BASE_URL. $user->profileimage;?>">
</div>
<div>
<span class="font-medium select-none"><?php echo $user->name;?></span>
</div>
</div>
<div class="flex items-center mx-4">
<span class="select-none transition mx-3 text-green-400 cursor-pointer"><i class="fas fa-circle"></i></span>
<span class="select-none transition hover:text-gray-500 mx-3 text-gray-600 cursor-pointer"><i class="fas fa-comment-alt"></i></span>
<span class="menu-logout relative select-none transition hover:text-gray-500 mx-3 text-gray-600 cursor-pointer"><i class="fas fa-ellipsis-v"></i>
<div class="logout flex items-center justify-center rounded absolute right-0 top-2 bg-white border hover:bg-gray-200" style="width: 100px;height: 60px;">
<a href="logout.php" class="p-2 px-2 text-lg text-gray-600">
Logout
</a>
</div>
</span>
</div>
</div>
<div class="px-6 py-5 select-none">
<input class="p-2 w-full rounded border" type="text" name="usersearch" placeholder="Search users">
</div>
<div class="select-none overflow-hidden overflow-y-auto h-2/4">
<h2 class="font-bold text-lg my-4 px-6 select-none">Users</h2>
<ul class="select-none">
<!-- USER-LIST -->
<?php $userObj->getUsers(); ?>
</ul>
</div>
</div>
</div>
<!--LEFT_SIDE_END-->
<!--RIGHT_SIDE-->
<div class="flex-2 flex rounded-xl w-full h-full ">
<!--HOME_PAGE_DIV-->
<div class=" flex flex-1 justify-center items-center">
<div class="select-none flex flex-col flex-1 h-full overflow-hidden overflow-y-auto items-center justify-center">
<div class="w-60 h-60 right-img select-none">
<img class="h-auto w-full select-none" src="assets/images/cam.png">
</div>
<div class="right-heading">
<h2 class="text-center select-none">Keep your webcam connected</h2>
<p class="select-none">This is app allow users to video chat with other users.</p>
</div>
</div>
</div><!--HOME_PAGE_DIV_ENDS-->
</div>
<!--RIGHT_SIDE_END-->
</div><!--INNER_ENDS-->
</div><!--WRAPPER ENDS-->
<!-- JavaScript Includes -->
<script type="text/javascript" src="<?php echo BASE_URL; ?>assets/js/main.js"></script>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
</body>
</html>
user.php
<?php
namespace MyApp;
use PDO;
include_once 'login.php';
include_once 'session.php';
include_once 'connection.php';
class User{
public $db,$id,$sessionID;
public function __construct(){
$db = new \MyApp\DB;
$this->db = $db->connect();
$this->id = $this->ID();
$this->sessionID = $this->getSessionID();
}
public function ID(){
if($this->isLoggedIn()){
return $_SESSION['id'];
}
}
public function getSessionID(){
$ss = session_id();
//var_dump($ss);
return $ss;
}
public function emailExist($email){
$stmt = $this->db->prepare("SELECT *FROM `users` WHERE `email`=:email ");
$stmt->bindParam(":email",$email,PDO::PARAM_STR);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_OBJ);
if (!empty($user)) {
return $user;
}else{
return false;
}
}
public function userData($id=''){
$id = ((!empty($id)) ? '$id' : $this->id);
$stmt = $this->db->prepare("SELECT *FROM `users` WHERE `id`=:id ");
$stmt->bindParam(":id",$id,PDO::PARAM_STR);
//var_dump($id);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
}
public function isLoggedIn(){
return ((isset($_SESSION['id'])) ? true : false);
}
public function redirect($location){
header("Location: ".BASE_URL.$location);
}
public function getUsers(){
$stmt = $this->db->prepare("SELECT * FROM `users` WHERE `id` != :id");
$stmt->bindParam(":id",$this->id,PDO::PARAM_INT);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach($users as $user){
echo '<li class="select-none transition hover:bg-green-50 p-4 cursor-pointer select-none">
<a href="'.BASE_URL.$user->username. '">
<div class="user-box flex items-center flex-wrap">
<div class="flex-shrink-0 user-img w-14 h-14 rounded-full border overflow-hidden">
<img class="w-full h-full" src= "'.BASE_URL.$user->profileimage.'">
</div>
<div class="user-name ml-2">
<div><span class="flex font-medium">'.$user->username.'</span></div>
<div></div>
</div>
</div>
</a>
</li>';
}
}
public function getUserByUsername($username){
$stmt = $this->db->prepare("SELECT * FROM `users` WHERE `username`=:username");
$stmt->bindParam(":username",$username,PDO::PARAM_STR);
//var_dump($username);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
}
public function updateSession(){
$stmt = $this->db->prepare("UPDATE `users` SET `sessionID` = :sessionID WHERE `id` =:id");
$stmt->bindParam(":sessionID",$this->sessionID,PDO::PARAM_STR);
$stmt->bindParam(":id",$this->id,PDO::PARAM_INT);
//var_dump($this->id);
$stmt->execute();
}
public function getUserBySession($sessionID){
$stmt = $this->db->prepare("SELECT * FROM `users` WHERE `sessionID`=:sessionID");
$stmt->bindParam(":sessionID",$sessionID,PDO::PARAM_STR);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
}
}
chat.php
<?php
namespace MyApp;
require 'core/chat.php';
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
class Chat implements MessageComponentInterface
{ protected $clients;
public $userObj, $data;
public function __construct() {
$this->clients = new \SplObjectStorage;
$this->userObj = new \MyApp\User;
}
public function onOpen(ConnectionInterface $conn)
{
// Store the new connection to send messages to later
$this->clients->attach($conn);
var_dump($this->userObj->userData('11'));
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg)
{
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n",
$from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
//The sender is not the receiver, send to other clients
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn)
{
// The connection is closed, remove from connection list
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
server.php
<?php
require __DIR__ . '/vendor/autoload.php';
require 'core/Chat.php';
use Ratchet\Server\IoServer;
use MyApp\Chat;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8087
);$server->run();
and when i tray to run server.php in the terminal i got this error "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes)" any one who can help me please?
Please or to participate in this conversation.