Session Variable disappears in Admin Control [duplicate] - php

This question already has answers here:
How to use store and use session variables across pages?
(8 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
$_SESSION['Active'] = 1; I have defined this in my login.php file.
if($_SESSION['Active'] ==1){ I am checking if this exists in my AdminControl File
echo "Hello"; when I login this works.
}
if (isset($_POST['submit'])) { this is the code that I used for going back to the AdminControl Page
header("Location: AdminControl.php");
exit();
}
When I go back to the Admin Control the Hello disappears.
In this picture you can see that there is not Hello

on your admin control page you could add the following.
<?php if(isset($_SESSION['Active']) && ($_SESSION['Active'] == '1')) { echo "Hello"; } else { echo "Something else";} ?>
Hope this helps you.

Related

Echo PHP variable to HTML after a form submit [duplicate]

This question already has answers here:
PHP Pass variable to next page
(9 answers)
Closed 3 years ago.
After a form submit, I am redirecting to the mentioned header location using below codes:
PHP Part:
$TestMessage = "This is a sample message. This is not a constant value. Original data will be collecting from DB";
if(mysqli_affected_rows($conn) > 0) {
header('Location:mydaomain/expenses?success=1');
}else{
header('Location:mydomain/expenses?failed=1');
}
HTML Part:
<div class="SubResult" id="SubResult" >
<?php
if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
echo $TestMessage;
echo "<h1 style='color:green;padding:3%;'>Success !!</h1>";
}
if ( isset($_GET['failed']) && $_GET['failed'] == 1 )
{
echo $TestMessage;
echo "<h1 style='color:red;padding:3%;'>SQL Error !!</h1>";
}
?>
</div>
Issue with the above code is,HTML wont display $TestMessage PHP variable content.
Why I am using this header option in PHP:
Earlier, form was getting submitted whenever I refresh the page after an original submit. I found this solution to avoid that issue.
When I remove this header part from php file, I am able to echo variable to PHP (But at that I am facing issue with the form submission on page refresh issue)
Is there any way I can echo this variable to HTML ?
When you use header, you are doing a redirect, so your variable $TestMessage is not available after that, if you want to use it, make sure you pass using query string, for example:
header('Location:mydaomain/expenses?success=1&testMessage='.$TestMessage);
and in the next page use $_GET['testMessage'] to get his value.

How to display alert box with timeout and then automatically redirect to another page using php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Hi i have to set a message box with time out when a specific data is already available in the database table.and then redirect to another page.My issue is that when page is getting redirected without showing the alert box.
Control statements php:
$count=0;
$sql="SELECT main_sec_name from main_sec_temp";
if($r=mysqli_query($con,$sql)) {
while($result= $r->fetch_row()) {
$count=$count+1;
}
}
if($count==0) {
if(mysqli_query($con,"INSERT INTO main_sec_temp(main_section_order,main_sec_name,main_number_of_ques,attempts) VALUES('','$sec_name','$no_question','$no_attempts')")) {
echo "Successfully Inserted";
header("location:index.php");
}else{
echo "Insertion Failed";
}
} else { //When Data is already exist
echo "<script>alert('already exist')</script>";
header("location:index.php");
}
You are trying to mix server side (PHP) and client side (Javascript) code in your example. Using header('Location:...') will get executed by PHP before the user will get a chance to see the contents of the page (also headers need to be used before any content is displayed on the page).
Additionaly, using javascript alert() requires user interaction by pressing the OK button, so you can't use timeout to redirect user.
Better approach would be something like this:
/// display html message and the javascript part of your code
echo "<h3 class='error'>Already exist</h3>";
echo "<script>";
echo "setTimeout(function(){ ";
echo " document.location='index.php';";
echo "}, 3000);"; // redirect after 3 seconds
echo "</script>";

Need to hide a link and show it only if an admin is logged in [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
I am trying to figure out this issue I am having.
I am trying to create a link but ONLY if there's an admin logged in, otherwise the link should be hidden.
if ($administrator = true) {
echo '<a href="medewerkertoevoegen.php?id='.'">Medewerkers
toevoegen</a>';
}
else
{
echo '<span></span>';
}
I am not sure if this is how it should work, currently my database looks like this. http://i.imgur.com/CdgLXkj.png
Please let me know if you can help me because I am very new at this, thanks!
Because '=' is assignation (not comparing). You must use '==' or '==='.
if ($administrator === true) {
echo '<a href="medewerkertoevoegen.php?id='.'">Medewerkers
toevoegen</a>';
} else {
echo '<span></span>';
}

trying to redirect a user to a different link after an input was insert [duplicate]

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 6 years ago.
I am learning PHP and HTML (sorry for being noob) trying to redirect a customers to a different link after an input was insert. which means after he press verify he will be redirect to my website www.example.com/home.php This is my current code:
<div class="tclpad" style="width:90px;"> </div>
<div class="tclpad"><input class="bstd" value="Verify" type="submit"></div>
Also, when someone inserts a wrong information, i can see in the code:
<?php
if(isset($_SESSION['wrong'])) {
echo "<p class='error'>Authentication failed.</p>";
session_destroy();
}
?>
I want the user after he press verify will get a message 'Verified'.
Thank you so much guys for your help.
Check the accepted answer here: How to make a redirect in PHP?
I'm not sure exactly what you're looking for with the 'Verified' statement, but the answer above shows how to redirect the web page with the PHP header() function.
Edit:
I realized the header() function limits how the current page provides feedback. Instead, you could use javascript to redirect:
function redirectOnVerify(myVar) {
if(myVar === 'success'){
window.alert('You have verified successfully');
window.location = 'http://newlocation.com' ;
}
else if (myVar === 'failure') {
window.alert('Failed to verify');
}
else {
console.log('PHP auth error, neither success nor failure')
}
}
And then in your html:
<input onClick="redirectOnVerify('
<?php if(isset($_SESSION['right'])){
echo 'success'
}
else if (isset($_SESSION['wrong']){
echo 'failure'
}
else {}
?>
')">
/j
Use header() like
header("refersh: 2; url=page2url");
Refersh is a delay time here the page will redirect after 2 seconds if u don't want delay then either set refersh to 0 or do this
header("location=pag2url");

Old PHP include code not working anymore, how to fix? [duplicate]

This question already has an answer here:
Enabling php.ini for backward compatibility
(1 answer)
Closed 9 years ago.
Ok so i want to have an include code on my index.php page that will include all of the other html pages within it. I used to do this using an id?=link.html link and this:
<?php
$id = $HTTP_GET_VARS['id'];
if ( !$id || $id == "" )
{
$number=10;
include("news/show_news.php");
}
else
{
include "$id";
}
?>
but apparantly http_get_vars is unrecognized or something? How can I fix this so that it will work? Or if things have changed, why should I not use this kind of thing for includes?
You can use $_GET to fetch the query string parameter by GET request, e.g.
index.php?id=123
The id value can be obtained by $_GET['id'].
p.s. your PHP codes are really old.
solved with the following:
<?php
if (!empty($_GET['id'])) {
$id = $_GET['id'];
$id = basename($id);
include("$id");
} else {
include("news/newsfilename.php");
}
?>
and
<a href="index.php?id=pagename.html">
Page Name
</a>
as the html

Categories