Session cookie to play sound with Java Script just once - javascript

I encrypted a directory that contains several folders by directories Admin (Protect Option), for example,memberarea directory.
now if someone opens a page of these directories , he must enter his username and password so that he can see the contents of that directory.
I used the following code to write a welcome text to display the text related to each user. In addition, according to the entered username, an audio file will be played when the page open:
<?php
$user_name = $_SERVER['PHP_AUTH_USER'];
if ($user_name == "admin") {
$name = "Sir Alex Morgan";
echo '<div class="wellcome_message">' . $name . ' ، welcome to your site :)</div>';
echo '<script type="text/javascript">
window.onload=function(){
document.getElementById("admin_audio").play();
}
</script>';
}
elseif ($user_name == "user1") {
$name = "Miss Mary";
echo '<div class="wellcome_message">' . $name . ' ، nice to meet you</div>';
echo '<script type="text/javascript">
window.onload=function(){
document.getElementById("user1_audio").play();
}
</script>';
}
.
.
.
?>
Now I want to make the above process happen only once in each browser session, ie if the user, for example, went from page domain.com/memberarea/part1/index.php to page domain.com/memberarea/part3/page.php just as he does not need to enter the password again until the browser is closed, The welcome message and voice will only be displayed for the first time.

Related

Redirect and popup when sql is done

I want to get my application when a item is deleted pop up a messege and redirect to another page. I used javascipt for the popup and php header for the redirection. Now its only doing or the popup or the redirect depending which one is listed first. how do i fix this?
<?php
session_start();
require_once('../../includes/mysql_config.php');
$id = isset($_SESSION['id']) ? $_SESSION['id'] : header('location: ../../login.php');
$Cursist = mysqli_query($con, "SELECT id FROM users WHERE id =".$_SESSION['id']);
if(!$Cursist){
header('location: ../login.php');
}
$test = $_GET['id'];
$sql = "DELETE FROM cursus WHERE id = $test";
$result = mysqli_query($con, $sql);
if ($result) {
echo "<script type='text/javascript'>alert('Verwijdert!')</script>";
header("Location: ../cursussen.php?destoyed=true&destroyed_id=".$_GET['id']);
}else {
echo "mislukt";
}
?>
If you send sometrhing before header will not work. You can use only header before sending sometrhing to the client.
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
http://php.net/manual/en/function.header.php
You could do with javascript but It not recommended because the user could have javascript disabled:
echo "<script type='text/javascript'>";
echo "alert('Verwijdert!')";
echo "document.location.href='index.html'";
echo "</script>";
The best way is to use session and header, you can save a var in session and show a message when the var is true and when you show the messasge delete the session var
delete.php
$_SESSION['deleted'] = true;
header("Location: index.php);
index.php
<?php if($_SESSION['deleted']){ ?>
<?php unset($_SESSION['deleted']) ?>
<div>Item was deleted</div>
<?php } ?>
Well, the problem is that the redirect immediately moves you to a new page, so any javascript on the old page becomes irrelevant. You might be able to use a delay before redirect so that the javascript alert can display.
Otherwise, introduce a variable that you send to the redirect destination page, and use this variable to trigger a javascript popup there.

javascript alert box not showing on php after header redirect

I am trying to display an alert box before redirecting to another page, here is my code, when I remove the header function it works properly but when it is here it will just redirect to the page without showing the alert box.
<html>
<body>
<?php
include("dbconfig.php");
$tempid = mysqli_real_escape_string($dbconfig, $_POST['tempid']);
$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig, $sql_query);
if ($result) {
echo '<script language="javascript">';
echo 'alert("visit deleted successfully")';
echo '</script>';
header("location:../SearchCountry/search.php");
}
?>
</body>
</html>
PHP is executed at the server side. It renders HTML/JS/CSS and sends it to the web browser, the web browser then parses and executes the JavaScript (In your case, show the alert dialog.)
However, once you call
header ("location:../SearchCountry/search.php");
The browser will be informed to redirect the user to ../SearchCountry/search.php immediately, without a chance to parse and execute the JavaScript. That's why the dialog will not show up.
Solution: redirect your user to another page with JavaScript instead of PHP.
<html>
<?php
include("dbconfig.php");
$tempid = mysqli_real_escape_string($dbconfig,$_POST['tempid']);
$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig,$sql_query);
if($result){
echo '<script language="javascript">';
echo 'alert("visit deleted successfully");\n';
echo 'window.location.href="../SearchCountry/search.php"'; //Redirects the user with JavaScript
echo '</script>';
die(); //Stops PHP from further execution
}
?>
</body>
</html>
echo "<script>
alert('visit deleted successfully');
window.location.href='SearchCountry/search.php';
</script>";
and get rid of redirect line below.
You were mixing up two different worlds.

How do I echo in php a jquery dialog box?

For a php project I am working on, I am trying to echo a popup window if an sql record is successful. I have been able to get this to work with javaScript alert but that’s not what I want, The JavaScript is preventing the rest of the page from loading until OK is clicked plus I would prefer to style a jQuery dialog instead.
Here is what does work in the php but not what I want:
$msg = "Success!";
if ($conn->query($sql) === TRUE) {
echo '<script type="text/javascript">alert("' . $msg . '"); </script>';
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
I have found this below to prevent pop up until page loads, but it does not work:
echo '<script type="text/javascript">window.onload = function(){ alert("' . $msg . ' "));}</script>';
I am trying to get a jquery dialog to popup. Here is the basic jquery dialog from their website not working. It just displays the text in the paragraph tag. (I was planning on customizing it after its working):
echo '<script>
$(function() {
$( "#dialog" ).dialog();
});
</script>'
.'<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the x icon.</p>
</div>';
Thanks to user Wazani, I simply forgot to include the jquery UI library in addition to the core.

Embedding multiline alert box in php code

I would like to embed a JavaScript alert box into php code and would like the text to report variable values. The following code gives multiline but does not enable me to use variables.
?>
<script type="text/javascript">
alert("Total size of all files too large.\nConsider uploading files in smaller sets.\nYou can append to existing sets.");
document.location.href="SomewhereElse.php";
</script>
<?php
The following code lets me use variables but does not give multiline.
$Str="Total size cannot be not more than " . ini_get('post_max_size');
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
When I try this code, no alert box comes up.
$Str="Unable to load (all of) these files because of their (total) size" .
"\nPlease make the upload set conform to the following parameters";
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
I do get an alert box if I leave the \n (before the Please) out but it is not multiline. I am wondering if the \n dismisses the alert box. At any rate, how do I get a multiline alert box with variables?
try something like this:
<?php
$max_size = ini_get('post_max_size');
echo '<script>';
echo 'alert("Total size cannot be more than ' . $max_size . '!\nPlease make sure...");';
echo '</script>';
?>

PHP and Javascript forward pages

ok I've searched stackoverflow and many other sites, I've tried all kinds of solutions for this but nothing seems to work.
I am processing a form in PHP, checking for missed entries and erroring if missed or adding to the SQL DB if ok, the form itself works just fine, the processing works, the form is either error thrown and displayed or added to the Database, I want that page to then display either error or sucess wait a short time and then auto forward either back to the form if there was an error, or to the page that displays the db contents if the add was successful. Nothing I seem to try here works. please help: My code so far:
//If errors present
if ($errormsg) {
echo "<div class=\"box red\">$errormsg</div>";
sleep(2);
echo '<script>' . "\n";
echo 'window.location="blogmake.html?blogid=" + blogid;';
echo '</script>';
}
if ($secim == "3"){ //If all present and correct post comment to DB
if ($valname && $valemail && $valcom){
$con = mysql_connect("xxxx","User","pass");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("dbname", $con);
$fulcom = mysql_real_escape_string ($_POST['comment']);
mysql_query("INSERT INTO tabname(blogid, date, email, name, comment) VALUES ('$blogid', CURRENT_TIMESTAMP(),'$valemail','$valname','$fulcom')") or die('Error: ' . mysql_error());
mysql_close($con);
echo "<div class=\"box green\">Your comment has been submitted</div>";
sleep(2);
echo '<script>' . "\n";
echo 'window.location="blogread.php?blogid=" + blogid;';
echo '</script>';
}
}
?>
</div>
You should probably use either an HTML meta tag:
<meta http-equiv="refresh" content="2;URL='http://yoursite.com/blogread.php?blogid=<?= $blogid ?>'" />
or JavaScript's setTimeout function:
setTimeout(function(){
window.location="blogread.php?blogid=<?= $blogid ?>";
}, 2000);
The meta tag has two important parameters: 2 is the number of seconds after which a redirection happens; and URL=... is the url to which it should redirect.
SetTimeout has two parameters in this case, the first one is the function that will be executed (the whole function part); and the other one is the delay time in milliseconds after which that function will be executed (the number 2000).
Note that I used <?= $blogid ?> in both cases - that's just a short code for this: <?php echo $blogid; ?>. Of course, you can use it however you want, for example using echo to echo the whole code, just like you were doing.
You need:
<meta http-equiv="refresh" content="3;url=http://www.google.com/" />
Change 3 for your seconds and url= by your webpage, ie:
echo '<meta http-equiv="refresh" content="2;url=blogread.php?blogid='.blogid.'" />';

Categories