Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Allan Carlos dos Santos G

CORS with php headers PHP

Recommended Posts

Olá pessoal, não estou conseguindo fazer uma requisição de um formulário html para um arquivo php que está em outro diretório no meu localhost, se puderem me ajudar, já estou há um tempo pesquisando e testando, mas nada.. Seguem os códigos, do html e do php:

 

html:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/jquery.mobile.icons.min.css">
<link rel="stylesheet" href="css/theme-classic.css">
<link rel="stylesheet" href="css/jquery.mobile.structure-1.4.5.min.css">
<link rel="shortcut icon" href="favicon.ico">
<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<center><img src="images/logo.png" style="width:15%"></center>
</div>
<div data-role="main" class="ui-content">
<!-- <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left">Show Popup Form</a> data-role="popup" -->
<div data-role="" class="ui-content" style="min-width:250px;">
<form method="post" action="../mobile_ar/index.php">
<div>
<h3>Login</h3>
<label for="email" class="ui-hidden-accessible">E-mail:</label>
<input required="" type="email" name="email" id="email" placeholder="E-mail">
<label for="senha" class="ui-hidden-accessible">Senha:</label>
<input required="" type="password" name="senha" id="senha" placeholder="Senha">
<!-- <label for="log">Keep me logged in</label>
<input type="checkbox" name="login" id="log" value="1" data-mini="true"> -->
<input type="submit" data-inline="true" value="Entrar">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
php:
<?php
session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
include_once 'Model/DAO/HomeDao.php';
$dao = new Model\DAO\HomeDao();
$row = $dao->loginDao($_POST["email"], $_POST["senha"]);
if (count($row) == 1) {
$dao->registroDeAcao($row[0]["id"], $row[0]["perfil"], "Logou no Sistema", "Sistema","Sistema");
$_SESSION["login"] = $row[0]["email"];
$_SESSION["perfil"] = $row[0]["perfil"];
$_SESSION["id"] = $row[0]["id"];
$_SESSION["nome"] = $row[0]["nome"];
$_SESSION["foto"] = $row[0]["foto"];
$_SESSION["tempo"] = time();
$dao->acessoDao(1, $row[0]["id"]);
header("location:../mobile/admin.html");
} else {
$msg = "Login/Senha inválido(s)";
header("location:../mobile/login.html?msg=" . $msg);
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Tente assim (com o session_start abaixo dos headers):

<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
    header('Access-Control-Expose-Headers: DAV, content-length, Allow');
    header('Access-Control-Allow-Methods: ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL');
}

session_start();

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.