open a link in a different tab - javascript

I have a button that allows authentication from twitter, everything works properly, but the only issue is that i wish to open the login-twitter.php page on a different tab, i tried using window.open in place of header but it didn't work. can anyone tell how it can be done
<?php
ob_start();
session_start();
if (isset($_SESSION['id'])) {
header("location: u_tasks.php");
}
if (array_key_exists("login", $_GET))
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'twitter')
{
header("Location: login-twitter.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<?
echo "<div class='col-md-9'>";
echo "<a href='?login&oauth_provider=twitter'><button style='background-color:#1dcaff; border-color:#1dcaff; color:white; height:30px; border-radius:10px;'>Go To Twitter</button></a>";
echo "</div>";
?>
</body>
</html>

i think it is helpful to you.
<button style='background-color:#1dcaff; border-color:#1dcaff; color:white; height:30px; border-radius:10px;'>Go To Twitter</button>

using header("Location:..."); can only do redirects. As far as I know, you'll have to do some amount of javascript or html to achieve your goal. Another problem is that browsers like to block automatically opened popups because 99% of the time automatic popups are used only by spammers. However, I'll show you a way to attempt to do this anyways:
<?php
$usingTwitter=0;
ob_start();
session_start();
if (isset($_SESSION['id'])) {
header("location: u_tasks.php");
}
if (array_key_exists("login", $_GET))
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'twitter')
{
$usingTwitter=1;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script>
<?php
if($usingTwitter){
echo "window.open('login-twitter.php');";
}
?>
<script>
</head>
<body>
<?
echo "<div class='col-md-9'>";
echo "<a href='?login&oauth_provider=twitter'><button style='background-color:#1dcaff; border-color:#1dcaff; color:white; height:30px; border-radius:10px;'>Go To Twitter</button></a>";
echo "</div>";
?>
</body>
</html>
Make sure you turn off popup blocking in your browser for this to work though!

Related

Using PHP to display images from a directory. I want to display it one by one since there are different size.

This is what I came up with
<?php
<<<EOF
<!DOCTYPE html>
<html>
<body>
EOF;
$images = glob('pfp/*.{jpg,jpeg,gif,png}', GLOB_BRACE);
foreach($images as $resource) {
echo "<img src=\"" . $resource . "\">";
}
<<<EOF
</body>
</html>
EOF;
?>
Would there be a better way than this? Ideally I would like for it to look something like this but I will be satisfied with centered and one by one for a cleaner look. Thank you for your time.
Regards,
NoobPHP
Start with the below code, when populating the img paths from glob function do not put it in the src tag instead put it in other attribute data-src. Then after page load has completed use jquery code to swap the values of data-src into src tag one by one.
This is my approach of doing this, there can be many other ways you can do this.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
$images = glob('pfp/*.{jpg,jpeg,gif,png}', GLOB_BRACE);
$i = 1;
foreach($images as $resource) {
echo "<img style="display:none;" id='imgLoader_".$i."' data-src=\"" . $resource . "\">";
$i++;
}
?>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"><script>
<script type="text/javascript">
$(document).ready(function(){
$("[id^='imgLoader_']").each(function(){
$(this).delay(500).attr('src',$(this).attr('data-src')).delay(500).show();
});
});
</script>
</body>
</html>

php echo javascript alert() not working

I would like to display a popup message when user is logged out, so I use
echo "<script>alert(\"You are logged out\");</script>";
But it doesn't work.
Below is my coding. Is there any logic problem in my coding?
<?php
session_start();
if(isset($_SESSION['Username']) == "admin")
{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#import "../CSS/Style.css";
#import "../CSS/Admin.css";
</style>
<title>Admin Home Page</title>
</head>
<body>
<div class="body"></div>
<?php
if(isset($_GET['id']) == "logout")
{
session_destroy();
echo "<script>alert(\"You are logged out\");</script>";
header("Location: ..\Main.php");
}
else
{
?>
<div class="menu">
Manage Staff
</div>
<div class="menu2">
Manage Account
</div>
<div class="logout">
Logout
</div>
<?php
}
}
else
{
?>
<center>
<p style="font-size:50px; font-weight:bold">Access Denied</p>
<p style="font-size:18px">Your request for this page has been denied because of access control</p>
</center>
<?php
}
?>
</body>
</html>
The session will be destroyed and will also redirect to Main.php, just the alert() will not come out.
You're doing an echo and then writing a relocate header. If you did your relocate in the javascript (after the user clicked the alert), it would probably work the way you expect it to.
echo "<script>alert('You are logged out'); window.location.href='..\Main.php';</script>";
Also, the way that you use isset will cause problems because isset returns true or false (it checks if a value is present), rather than returning the value.
So instead of
if(isset($_SESSION['Username']) == "admin")
You need to do:
if(isset($_SESSION['Username']) && $_SESSION['Username'] == "admin")
header("Location: ..\Main.php"); tells the browser to go to another page before it even shows the page... if you want the user to see the alert, try this:
session_destroy();
echo "<script>";
echo "alert('You are logged out');";
echo "window.location = '../Main.php';"; // redirect with javascript, after page loads
echo "</script>";
use this it will solve your problem!! first change your code from
if(isset($_SESSION['Username']) == "admin")
{
to
if(!empty($_SESSION['Username']) && ($_SESSION['Username']=="admin")){
and than use following code
if(!empty($_GET['id']) && ($_GET['id']=="logout"))
{
session_destroy();?>
<script>
alert("You are logged out");
window.location.href='..\Main.php';
</script>
<?php }?>
Try,
This must work,
And remove php header, replace with the following code.
echo "<script>alert('You are logged out');
location.href='..\Main.php';
</script>";

Infinite scroll isn't working

I Have been going through a tutorial on infinite scrolling and have everything exactly the same except my alert is not being triggered and I cannot work out why. Any possible variations of obtaining the scroll positioning in the IF statement would also be helpful.
<?php
require_once("connect.php");
$results = $connect->query("SELECT * FROM images ORDER BY image_name DESC LIMIT 0,2");
$count = $connect->query("SELECT * FROM images");
$nbr = $count->rowCount();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class='images'>
<?php
while($row = $results->fetch())
{
?>
<p><img src="images/<?php echo $row['image_name'];?>" height="500" width="500"></p>
<?php
}
?>
</div>
<script src='js/jquery.js'></script>
<script>
$(document).ready(function(){
var load = 0;
$(window).scroll(function(){
if($(window).scrollTop() == $(document).height() - $(window).height())
{
alert("test");
}
});
});
</script>
</body>
</html>
Found the solution. The correct library needs to be called which many sites do not stipulate. Here is the code that needs to be posted just before the scroll script.
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

xmlrequest does not return text from php

I need to find some tools to be able to call .php from .html.
Here is a problem:
while this example works, from .php file:
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8">
</head>
<body>
<p>Your IP is </p>
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
<hr />
</body>
</html>
this example doesn't echoes IP in .html file:
---from index.html---
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8">
<script type="text/javascript">
function getIpNumber() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("showIpNumber").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","getip.php",true);
xmlhttp.send();
}
</script>
</head>
<body onload="getIpNumber();">
<p>Your IP is <span id="showIpNumber"></span></p>
</html>
---from getip.php---
<?php
echo "$_SERVER['REMOTE_ADDR']";
?>
what is wrong here?
Change this line
echo "$_SERVER['REMOTE_ADDR']";
to
echo $_SERVER['REMOTE_ADDR'];
having error reporting on would have signaled:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /file.php on line 2
Add error reporting to the top of your file(s) which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
As pointed out by Knossos in a comment:
"You can also use echo "{$_SERVER['REMOTE_ADDR']}"; Although in this case, that would be quite pointless. It can be useful in bigger more complicated strings though."
Just replace this line
echo "$_SERVER['REMOTE_ADDR']";
to this
echo $_SERVER['REMOTE_ADDR'];

repeatedly poll database for pages, then output pages in sequence with slide effect

I have a client who needs to output pages that are referenced by ID in a database. The pages must be output one after another (like a slide show), and there must be a slider transition effect. To make it "fun", the database will be changing as new pages are added, so a portion of the script to do all this must be run repeatedly to check the database.
I've got the pages to load, and I've got the transition working. The problem is, I can't get the polling to work because there's such a mess. That is, if I add a new page, I must manually reload the script - I need the script to detect new pages added to the database. I know that all I really need to do is just re-run the database query to generate the array, but with all the other stuff going on, I'm lost. I've tried setInterval() and even a while loop to no avail. At this point I'm sure I'm just missing something (hopefully) trivial, so I humbly ask for help. My code is VERY sloppy as I test, but here it is:
<?php
$community_id = 89;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
<?php
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// get all the pages that pertain to the community
$uid_query = "SELECT uid FROM pages WHERE pid=" . $community_id;
// build the javascript array by grabbing all results from above query
$result = mysqli_query($con,$uid_query);
$counter = 0;
$baseURL = "http://www.website.com/index.php?id=";
echo "var frames = new Array;";
while($row = mysqli_fetch_array($result)) {
echo "frames[" . $counter . "]='" . $baseURL . $row['uid'] . "';";
$counter++;
echo "frames[" . $counter . "] = 5;";
$counter++;
}
mysqli_close($con);
?>
var i = 0, len = frames.length;
function ChangeSrc()
{
if (i >= len) { i = 0; } // start over
document.getElementById('frame').src = frames[i++];
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
window.onload = ChangeSrc;
</script>
<style type="text/css">
body, html
{
margin: 0; padding: 0; height: 100%; overflow: hidden;
}
#iframe_container
{
position:absolute; left: 0; right: 0; bottom: 0; top: 0px;
}
</style>
</head>
<body>
<div id="iframe_container">
<iframe src="" name="frame" id="frame" width="100%" height="100%" frameborder="0"></iframe>
</div>
</body>
</html>
... and then any given page looks like this:
javascript:
$(document).ready(function(){
$('.hidden').slideDown(1000);
});
css:
.hidden{
display:none;
}
html:
<div class="hidden">
CONTENT GOES HERE
</div>

Categories