Redirection header("Location:...")

Fermé
DwightShrut - 25 juin 2021 à 15:33
 DwightShrut - 2 juil. 2021 à 00:39
Salut à tous, alors voilà, j'ai fais une petite technique pour rediriger comme je voulais mes liens avec des balises <a> sans problème, néanmoins, j'ai une autre page ou ce n'est pas des balises <a> mais des header("Location:...") j'aimerai donc faire la même chose avec ça et j'ai essayé quelques trucs mais j'ai des problèmes avec tout ce que j'essaye. Merci pour votre aide.

Voici ce que j'ai fais :
<?php if ($_SESSION['nom']=="personne1"){ 
				?>
				<a href="taches.php?salarie=personne1">
				<?php } elseif ($_SESSION['nom']=="personne2"){ 
				?>
				<a href="taches.php?salarie=personne2">
					<?php } else { ?>
				<a href="taches.php?salarie=personne3">
				<?php } ?>


Et voici le code avec le quel j'aimerai faire pareil :
header("Location:taches.php");
		exit();

3 réponses

yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476
25 juin 2021 à 16:30
bonjour,
qu'as-tu essayé, quels problèmes as-tu rencontrés?
0
DwightShrut
25 juin 2021 à 16:34
Alors j'ai essayé ce bout de code ci-dessous mais peut importe le salarié connecté, c'est toujours le personne3 qui sortait à la fin et je pense que c'est parce que le dernier "exit();" est en dessous de personne3, donc j'avais essayé d'enlever le exit mais ça ne fonctionne pas :/

J'ai aussi essayé de faire un copier coller du premier code et de remplacer le header par une balise <a> mais ça n'a pas fonctionné non plus (logique).

<?php if ($_SESSION['nom']=="personne1"){ 
				?>
				header("Location:taches.php?salarie=personne1");
		exit();
				<?php } elseif ($_SESSION['nom']=="personne2"){ 
				?>
				header("Location:taches.php?salarie=personne2");
		exit();
					<?php } else { ?>
				header("Location:taches.php?salarie=personne3");
		exit();
				<?php } ?>
0
DwightShrut > DwightShrut
25 juin 2021 à 16:36
Du coup je pense que mes if, elseif, else sont bons mais que j'ai un problème par rapport aux exit(); ou au header("Location:...")
0
yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476 > DwightShrut
25 juin 2021 à 16:40
header() est une instruction php, il n'y a aucune raison d'avoir des balises de sortie et d'entrée de php.
<?php if ($_SESSION['nom']=="personne1"){ 
		header("Location:taches.php?salarie=personne1");
		exit();
} elseif ($_SESSION['nom']=="personne2"){ 
		header("Location:taches.php?salarie=personne2");
		exit();
 } else { 
		header("Location:taches.php?salarie=personne3");
		exit();
 } ?>
0
yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476
25 juin 2021 à 16:32
je suis étonné par ton code, pourquoi passer le nom via l'url alors qu'il est dans une variable de session?
peux-tu montrer le code de la page taches.php?
0
DwightShrut
25 juin 2021 à 17:02
Voilà tout mon code de tâches.php :
<body class="fond2">
	<?php 


// si l'utilisateur fait une recherche (taches.php?salarie=id), on change le filtre salarie


// [...] execution de la requête sql pour récupérer les taches
	session_start();
	include('pdo.php');
	include('header.php');
	if ($_SESSION['niveau']=="technicien"){ 
		if($droit['techAcces']=="non"){
			header('Location:index.php?acces=interdit');
		}
	}
	if ($_SESSION['niveau']=="basique"){ 
		if($droit['basiqueAcces']=="non"){
			header('Location:index.php?acces=interdit');
		}
	}
	
	if(isset($_GET['codeClient'])){
		$stmt = $pdo->prepare('SELECT * from clients WHERE codeClient = :codeClient');
		$stmt->bindValue(':codeClient', $_GET['codeClient'], PDO::PARAM_STR);
		$stmt->execute();
		$client=$stmt->fetch();

	?>
	<div class="container" style="text-align:left; position:relative; bottom:10px">
		<a href="modifier.php?id=<?php echo $client['id']; ?>" class="retour"><img class="imagehome" src="icones/retour.png"></a>
	</div>
	<?php } ?>
	<br>
	<div style="text-align:center">
			<h1 class="h1liste">Tâches</h1>
			
	</div>

	<?php if(isset($_GET['acces']) AND $_GET['acces']=="interdit"){ ?>
		<div style="text-align:center; color:red"> Vous n'avez pas les droits d'accès à cette page ! </div><br>
	<?php } ?>
	<br>
		<?php
		if (isset($_GET['salarie'])) {
	$filtreSalarie = $_GET['salarie'];
}
			if(isset($_GET['codeClient']) and isset($_GET["moisSelec"]) and isset($_GET["annee"])){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient where taches.codeClient = :codeClient AND YEAR(taches.date) = '.$_GET["annee"].' AND MONTH(taches.date) = '.$_GET["moisSelec"].' ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->bindValue(':codeClient', $_GET['codeClient'], PDO::PARAM_STR);
				$stmt->execute();
				$resu=$stmt->fetchALL();
			}

				if(isset($_GET['codeClient']) and isset($_GET["moisSelec"]) and isset($_GET["annee"]) and $_GET["annee"] == "ALL"){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient where taches.codeClient = :codeClient AND MONTH(taches.date) = '.$_GET["moisSelec"].' ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->bindValue(':codeClient', $_GET['codeClient'], PDO::PARAM_STR);
				$stmt->execute();
				$resu=$stmt->fetchALL();
			}

				if(isset($_GET['codeClient']) and isset($_GET["annee"]) and isset($_GET["moisSelec"]) and $_GET["moisSelec"] == "annee"){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient where taches.codeClient = :codeClient AND YEAR(taches.date) = '.$_GET["annee"].' ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->bindValue(':codeClient', $_GET['codeClient'], PDO::PARAM_STR);
				$stmt->execute();
				$resu=$stmt->fetchALL();
			}

				if(isset($_GET['codeClient']) and isset($_GET["annee"]) and $_GET["annee"]=="ALL" and isset($_GET["moisSelec"]) and $_GET["moisSelec"] == "annee"){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient where taches.codeClient = :codeClient ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->bindValue(':codeClient', $_GET['codeClient'], PDO::PARAM_STR);
				$stmt->execute();
				$resu=$stmt->fetchALL();
			}

			if(!isset($_GET['codeClient']) and !isset($_GET["moisSelec"]) and !isset($_GET["annee"])){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->execute();
				$resu=$stmt->fetchALL();
			}

				if(isset($_GET['salarie']) and $_GET['salarie']=="TOUS"){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->execute();
				$resu=$stmt->fetchALL();
			}elseif (isset($_GET['salarie']) and $_GET['salarie']!="TOUS"){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient where taches.salarie=:nom ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->bindValue(':nom', $_GET['salarie'], PDO::PARAM_STR);
				$stmt->execute();
				$resu=$stmt->fetchALL();
			}
			
			/*if(isset($_SESSION['niveau']) AND $_SESSION['niveau']!="admin"){
				$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient where taches.salarie=:nom ORDER BY taches.statut DESC, taches.date DESC');
				$stmt->bindValue(':nom', $_SESSION['nom'], PDO::PARAM_STR);
				$stmt->execute();
				$resu=$stmt->fetchALL();}*/
				
				if(isset($_GET['date']) AND ($_GET['date']="depasse")){
					$dateactuelle=date("Y-m-d");
					$stmt = $pdo->prepare('SELECT taches.codeClient, taches.salarie, taches.id, taches.date, taches.minReal, taches.heureReal, taches.statut, clients.id as idd from taches JOIN clients ON taches.codeClient = clients.codeClient where taches.salarie=:nom AND taches.statut = :statut and taches.date < :date ORDER BY taches.date DESC, taches.salarie ASC');
					$stmt->bindValue(':nom', $_SESSION['nom'], PDO::PARAM_STR);
					$stmt->bindValue(':statut', 1, PDO::PARAM_INT);
					$stmt->bindValue(':date', $dateactuelle, PDO::PARAM_STR);
					$stmt->execute();
					$resu=$stmt->fetchALL();
				}
		?>
		<table>
			<tr>
				<td></td>
				<td><div style="text-align: center"><a href="ajoutTache.php">
				<img align="center" src="icones/add.png" width="35">
			</a></td>
				<td></td>
		</tr>
		<tr>
			<br><br>
							<td></td>
							<td>


<form action="taches.php" method="GET">
	<select name="salarie" id="id" style="width:200px">
		<option value="" <?= empty($filtreSalarie) ? 'selected' : ''; ?>>Tous les salariés</option>
		<option value="personne3" <?php if ($filtreSalarie == 'personne3') echo 'selected'; ?>>personne3</option>
		<option value="personne2" <?php if ($filtreSalarie == 'personne2') echo 'selected'; ?>>personne2</option>
		<option value="personne1" <?php if ($filtreSalarie == 'personne1') echo 'selected'; ?>>personne1</option>
	</select>
	<button class="btn" style="width:60px" type="submit"><img src="icones/accept.png" width="40" height="40"></button>
</form></td>
            <td></td>
        </tr>
	</table>
		<br>
		<div class="container">
		
<table id= "table-id">
			<tr>
				<th>Code client</th>
				<th>Salarié</th>
				<th>Date</th>
				<th>Temps de réalisation</th>
			</tr>
			<?php
			foreach ($resu as $res){

				$heure = ($res['heureReal'] * 60) ;
				$minutes = ($res['minReal']);
				$totalHeures += $heure + $minutes;
			?>
			
				<tr class="trlist">
					<td <?php if($res['statut']==1){ ?>style="width:200px; background-color:RGBA(255,0,0,0.4); text-align:left; padding-left:10%"<?php }else { ?> style="width:200px; text-align:left; padding-left:10%"<?php } ?>><a href="modifier.php?id=<?php echo $res['idd']?>" onclick="window.open(this.href); return false;"><?php echo $res['codeClient']?></a></td>
					<td <?php if($res['statut']==1){ ?>style="width:200px; background-color:RGBA(255,0,0,0.4); text-align:left; padding-left:12%"<?php }else { ?> style="width:200px; text-align:left; padding-left:12%"<?php } ?>><?php echo $res['salarie']?></td>
					<td <?php if($res['statut']==1){ ?>style="width:200px; background-color:RGBA(255,0,0,0.4); text-align:left; padding-left:10%"<?php }else { ?> style="width:200px; text-align:left; padding-left:10%"<?php } ?>><a href="modifierTache.php?id=<?php echo $res['id']?>"><?php echo strftime('%d-%m-%Y',strtotime($res['date']))?></a></td>
					<td <?php if($res['statut']==1){ ?>style="width:200px; background-color:RGBA(255,0,0,0.4); text-align:left; padding-left:12%"<?php }else { ?> style="width:200px; text-align:left; padding-left:12%"<?php } ?>><?php echo gmstrftime( "%H h %M min", (($res['heureReal'] * 60) + $res['minReal']) * 60 )?></td>
				</tr>				
									
			<?php
			}
			?>		
		</table>
		<br>
		<?php if(isset($_GET['codeClient'])){
		?>
		<h4>Total d'heures : <?php echo gmstrftime( "%H h %M min", $totalHeures * 60 )?></h4>
		<br>
		<?php } if(isset($_GET['codeClient'])){
		?>
			<br>
			<div class="container" style="text-align: right;"><a href="HTML2PDF/pdfTache.php?codeClient=<?php echo $_GET['codeClient']?>&moisSelec=<?php echo $_GET['moisSelec']?>&annee=<?php echo $_GET['annee']?>" target="_blank"><button type="button" style="width:60px" class="btn"><img src="icones/imprimante.png" style="width:30px"></button></a></div>
		<?php 
		}
		?>
	<br><br>			
	<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    


Et ma page où se trouve le header("Location:...") est la page modifierTachesReq.php qui permet de faire tous les UPDATE dans la base de donnée après la modification d'une tâche (modifierTache.php)
0
yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476 > DwightShrut
25 juin 2021 à 17:14
Il est conseillé d'éviter les GET et POST pour tout ce qui est sensible. L'utilisateur peut très facilement manipuler ces valeurs.

Préférable, donc, d'utiliser, par exemple,
$_SESSION['nom']
au lieu de
$_GET['salarie']
.
0
DwightShrut > yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024
25 juin 2021 à 17:23
Ok ok merci je prend note je vais modifier tout ça merci !

Et sinon du coup pour mon autre problème as-tu une solution aussi ?
0
yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476 > DwightShrut
25 juin 2021 à 17:24
quel autre problème?
0
DwightShrut > yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024
25 juin 2021 à 17:26
Mon problème principal pour lequel j'ai posé ma question sur ce forum, les redirection avec le header("Location:taches.php")
0
jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 4 650
2 juil. 2021 à 00:33
Bonjour,


ce code :
<?php if ($_SESSION['nom']=="personne1"){ 
		header("Location:taches.php?salarie=personne1");
		exit();
} elseif ($_SESSION['nom']=="personne2"){ 
		header("Location:taches.php?salarie=personne2");
		exit();
 } else { 
		header("Location:taches.php?salarie=personne3");
		exit();
 } ?>

Revient à écrire
<?php 
$nom = !empty($_SESSION['nom']) ? $_SESSION['nom'] : "personne3";
header("Location:taches.php?salarie=".$nom);
exit;

 ?>


Mais.. Sachant que tu as le "nom" de la personne en variable de session.... à quoi ça sert de le remettre en variable GET dans l'url sur laquelle tu redirige ?
Le code devrait simplement être :
<?php
header("Location:taches.php");
exit;
?>

et dans ton fichier taches.php, remplacer ton GET pour récupérer le salarié par :
$salarie =  !empty($_SESSION['nom']) ? $_SESSION['nom'] : "personne3";


Quoi qu'il en soit, si tes soucis persistent.. pense à :
- Vérifier que tu as bien mis un session_start sur chacune de tes pages
- Faire un var_dump($_SESSION); pour vérifier ce qu'elle contient


0
Bonjour,

Merci beaucoup pour ces aides précieuses ! Je vais essayer ça demain ! :)
0