-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
138 lines (110 loc) · 3.65 KB
/
Copy pathfunctions.php
File metadata and controls
138 lines (110 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php // <--- do NOT put anything before this PHP tag
// in PHP we can create our own functions to do whatever we need.
// the benefit of using a function is that we can reduce duplicate code.
// here is a function that will connect the Database
// wherever we need to connect to the database we just call this function.
function goToHome()
{
header('Location: http://localhost:8888/index.php');
exit;
}
function connectToDatabase()
{
// connect to our SQLITE database
// if you had a MYSQL server you could use this instead:
try {
// $dbh = new PDO("sqlite:./database/OnlineShop.db");
$dbh = new PDO("mysql:host=127.0.0.1;port=8889;dbname=English", "root", "root");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch (PDOException $e)
{
echo "Connection error ".$e->getMessage();
exit;
}
// enable errors
//Turn OFF emulated prepared statements.
// return the database handle.
return $dbh;
}
function getSettingsByName($name)
{
$q="select distinct(val) from setting where name = ?";
$db=connectToDatabase();
$p=$db->prepare($q);
$p->bindValue(1,$name);
$p->execute();
$list=array();
while (($row=$p->fetch(PDO::FETCH_ASSOC)))
{
$list[] = $row['val'];
}
return $list;
}
function showErrorMessage($cookieMessage)
{
echo "<h4 class = 'errormsg'>Error: $cookieMessage </h4>";
}
// run this function on untrusted data before we echo it on the page.
function makeOutputSafe($unsafeString)
{
$safeOutput = htmlspecialchars($unsafeString, ENT_QUOTES,"UTF-8");
return $safeOutput;
}
// this function lets you redirect the user to a different web page.
function redirect($newURL)
{
// the header location function will send a user to the specified URL.
// please note that this function MUST be called before any HTML is displayed on the page or it wont work.
header("Location: $newURL");
// we just redirected the user, that means there is no one around to view this page.
// so we can just stop processing this page.
die();
}
// please note that this function MUST be called before any HTML is displayed on the page or it wont work.
function setCookieMessage($cookieMessage)
{
setcookie("CookieMessage", $cookieMessage);
}
// please note that this function MUST be called before any HTML is displayed on the page or it wont work.
function getCookieMessage()
{
if(isset($_COOKIE['CookieMessage']))
{
$message = $_COOKIE['CookieMessage'];
deleteCookie("CookieMessage");
return makeOutputSafe($message);
}
else return "";
}
// please note that this function MUST be called before any HTML is displayed on the page or it wont work.
function deleteCookie($cookieName)
{
// to delete a cookie, you set the expiry date to a date in the past.
// in this case set the expiry date to 1 second past midnight 1st of Jan 1970
setcookie($cookieName,"",1);
}
// this function will return true if $needle is found inside $haystack.
function stringContains($haystack, $needle)
{
return strpos($haystack, $needle) !== false;
}
// I strongly suggest you do not close the PHP tag in this file.
// it can cause issues with setcookie() and header()
function DoesCartContainProduct($ProductID){
if(isset($_COOKIE["ShoppingCart"]))
{
if (stringContains($_COOKIE["ShoppingCart"],$ProductID))
{
return true;
}else
{
return false;
}
}
else
{
//user has no shopping cart.
return false;
}
}