PHP GET Function as NULL - php

I want to use GET in PHP and I know how to. The only problem is when I start my page I dont have a ?var=... on it. For example my site is
http://localhost/q/question.php
and in my code is
$num = $_GET['num'];
And I want to assume that if $num == NULL or has not been defined as in the link above.
I will generate a different page. because as example below just determines I am starting the questions in what ever page.
No num variable = Start Generating questions in array.
http://localhost/q/question.php?num=1

if(isset($_GET['num')){
header('Location: someotherpage.php'); //redirects user to someotherpage.php
//do something
}
else{
//do something else
}

<?php
if(isset($_GET['num'))
{
echo "is not null";
}
else
{
echo "is null";
}
?>

Related

Creating dynamic header links in PHP

In my code i have an if statement, which is return is true, it would header the user to a specific page. But i want to send some data to the next page. So i tried using dynamic links. but it doesn't seem to work. Here's my code;
<?php
$row = mysqli_fetch_object($query);
if($row->Usertype = "General_User")
{
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid= echo $row->Company_ID");
}
else
{
header('Location: http://www.mywebsite.com');
}
?>
but when i'm redirected to the page, i get this;
http://www.mywebsite.com/GeneralUserHome.php?cid=%20echo%20'';
any suggestions?
Why do you have echo in there?
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid={$row->Company_ID}");
You have a typo here:
if($row->Usertype = "General_User")
That's an assignment, and will always be true. You want double-equals for comparison:
if($row->Usertype == "General_User")
As a note, I reverse the two in order to avoid these typos. This will error out and tell you exactly what was wrong, if you typo'd a single equals sign:
if("General_User" = $row->Usertype)

Checking multiple $_ POST words with PHP

I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {

A better way to check if a user were logged in and generate like dislike link

I have a script that takes some 500 rows from a table, and based on whether or not a user is logged in, generates a link to like or dislike the item.
The way it currently goes is like this:
//Select * from table;
//while(){
if($userLogged)
{
echo $row['columnName'].' Like - Dislike';
}else{
echo $row['columnName'];
}
}
This way, it checks if a user is logged at each and every row. $userLogged is set in a file that's included on this page.
What would be a better way to do this instead of checking if a user were logged in inside the loop for each and every row?
Not getting your question quiet well but I think you want to prevent condition each time you loop so you can check the condition first and than loop accordingly, example
if($userLogged) {
while(condition) {
}
} else {
while(condition) {
}
}
This way you don't have to check the condition inside the loop each time it loops
Use sessions here
session_start();
if(isset($_SESSION['userLogged']))
{
echo $row['columnName'].' Like - Dislike';
}else{
echo $row['columnName'];
}
If the code for the like and dislike is identical for each entry then you could do something like this.
$links = ($userLogged)?"Like - Dislike":"";
while (condition){
echo $row['columnName'].$links;
}

Using PHP Get function to show content on webpage, yet still using same PHP file?

I'm new to PHP, and just creating a simple website.
At the moment, I have a header with some links (i.e. blog, faq, home, gaming etc). I'm trying to use GET functions to show new content in a container on the webpage. I've tried having them link to the index and to a specific page, like
Home
and then having some PHP in the html body...
<?php
if ($_GET[page] == "faq") {
$result === 'FAQ';
} else {
$result === 'Non-FAQ';
}
echo $result;
?>
just to see if it would work, and lo and behold, it doesn't.
So, that's basically the gist of what's happening. It's baffled me for the past few hours, and would really appreciate some help
Thanks
You aren't using the assignment operator to assign a value to $result. Use a single equals sign, ie
<?php
if ($_GET['page'] == "faq") {
$result = 'FAQ';
} else {
$result = 'Non-FAQ';
}
echo $result;
?>
<?php
if ($_GET[page] === "faq") {
$result = 'FAQ';
} else {
$result = 'Non-FAQ';
}
echo $result;
?>
If you want to DEFINE a string you use one "=", if you want to compare it you use "===" (or ==) :)

php redirect based on url variable

I want to create a URL redirect based on a URL variable.
so, if student%20gender (student gender) is male then go to www.one.com, if female, go to www.two.com.
couldn't figure this one out yet. any help?
Question could use a little bit of a better explanation. Do you mean that someone is going to
http://www.yoursite.com/yourscript.php?student%20gender=male and you want them to be redirected to http://www.one.com?
If this is the case, PHP has a built in variable known as $_GET which stores the values listed after a ? in a URL. So in the above example, we'd see:
$_GET['student gender'] = male;
You can use this to access any number of parameters separated by &
So the URL http://www.site.com/index.php?val1=a&val2=b&val3=c would give us:
$_GET['val1'] = a;
$_GET['val2'] = b;
$_GET['val3'] = c;
After this, to do a redirect in PHP the easiest way is to send a Location: header. This is done like so:
<?php
header("Location: www.newsite.com");
?>
Combining this with our $_GET variable and some simple logic:
<?php
if($_GET['student gender'] == 'male'){
header("Location: www.one.com");
die();
} else {
header("Location: www.two.com");
die();
}
?>
$var = $_GET['yourvar'];
if($var == 'one'){
header("Location: http://www.one.com/");
}else if ($var == 'two'){
header("Location: http://www.two.com/");
}
then do http://www.yoururl.com?yourvar=one
You also have to make sure you look at the security aspects here, the best way yo accomplish this is
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender'] : false;
switch($gender)
{
default: //The default action
//Send back to the gender select form
break;
case 'male':
//Send to male site!
break;
case 'female':
//Send to female site!
break;
}
This should be sufficient, but please never use $_X['?'] in functions that execute either shell or database queries without sanitation.
Note: _X being (GET,POST,REQUEST,FILES)

Categories